Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:03:58+00:00 2026-05-26T22:03:58+00:00

I am writing a auction server and client and using a class called BidHandler

  • 0

I am writing a auction server and client and using a class called BidHandler to deal with the bids another class AuctionItem to deal with the items for auction. The main problem I am having is little synchroization problem.

Screen output of client server

as can see from the image at 1st it takes the new bid and changes the value of the time to it, but when one the user enters 1.0 the item seems to be changed to that. But later on when the bid changes again to 15.0 it seems to stay at that price. Is there any reason for that. I have included my code below. Sorry if didnt explain this well.

This is the auction client

import java.io.*;
import java.net.*;

public class AuctionClient
{

private AuctionGui gui;

private Socket socket;
private DataInputStream dataIn;
private DataOutputStream dataOut;

//Auction Client constructor  String name used as identifier for each client to allow server to pick the winning bidder
public AuctionClient(String name,String server, int port)
{

    gui = new AuctionGui("Bidomatic 5000");
    gui.input.addKeyListener (new EnterListener(this,gui));
    gui.addWindowListener(new ExitListener(this));

    try
    {
        socket = new Socket(server, port);
        dataIn = new DataInputStream(socket.getInputStream());
        dataOut = new DataOutputStream(socket.getOutputStream());
        dataOut.writeUTF(name);
          while (true) 
          {
        gui.output.append("\n"+dataIn.readUTF());
        }
  } 
  catch (Exception e)   
  {
     e.printStackTrace();
  }

}




public void sentBid(String bid)
{
    try
    {
        dataOut.writeUTF(bid);
    }

    catch(IOException e)
    {
        e.printStackTrace();
    }

}

public void disconnect()
{
    try
    {
        socket.close();
    }

    catch(IOException e)
    {
        e.printStackTrace();
    }
}

public static void main (String args[]) throws IOException
{
    if(args.length!=3)
    {
        throw new RuntimeException ("Syntax: java AuctionClient <name> <serverhost> <port>");
    }

    int port = Integer.parseInt(args[2]);
    AuctionClient a = new AuctionClient(args[0],args[1],port);

}
}

The Auction Server

import java.io.*;
import java.net.*;
import java.util.*;

public class AuctionServer
{


public AuctionServer(int port) throws IOException
{
    ServerSocket server = new ServerSocket(port);

    while(true)
    {
        Socket client = server.accept();
        DataInputStream in = new DataInputStream(client.getInputStream());
        String name = in.readUTF();
        System.out.println("New client "+name+" from " +client.getInetAddress());

        BidHandler b = new BidHandler (name, client);
        b.start();
    }

}


public static void main(String args[]) throws IOException
{
    if(args.length != 1)
        throw new RuntimeException("Syntax: java AuctionServer <port>");

    new AuctionServer(Integer.parseInt(args[0]));

}


}

The BidHandler

import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.Float;

public class BidHandler extends Thread
{
Socket socket;
DataInputStream in;
DataOutputStream out;
String name;
float currentBid = 0.0f;
AuctionItem paper = new AuctionItem(" News Paper ", " Free newspaper from 1990 ", 1.0f, false);
protected static Vector handlers = new Vector();

public BidHandler(String name, Socket socket) throws IOException
{
    this.name = name;
    this.socket = socket;

    in = new DataInputStream (new BufferedInputStream (socket.getInputStream()));
    out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}

public synchronized void run()
{
    try
    {
        broadcast("New bidder has entered the room");
        handlers.addElement(this);


        while(true)
        {
            broadcast(paper.getName() + paper.getDescription()+" for sale at: " +paper.getPrice());

            while(paper.getStatus() == false)
            {


                String message = in.readUTF();
                currentBid = Float.parseFloat(message);
                broadcast("Bidder entered " +currentBid);

                if(currentBid > paper.getPrice())
                {
                    paper.setPrice(currentBid);
                    broadcast("New Higgest Bid is "+paper.getPrice());
                }

                else if(currentBid < paper.getPrice())
                {
                    broadcast("Higgest Bid is "+paper.getPrice());

                }

                else if(currentBid == paper.getPrice())
                {
                    broadcast("Higgest Bid is "+paper.getPrice());

                }

            }
        }

    }

    catch(IOException ex)
    {
        System.out.println("-- Connection to user lost.");
    }

    finally
    {
        handlers.removeElement(this);
        broadcast(name+" left");

        try
        {
            socket.close();
        }
        catch(IOException ex)
        {
            System.out.println("-- Socket to user already closed ?");

        }

    }

}

protected static void broadcast (String message)
{
    synchronized(handlers)
    {
        Enumeration e = handlers.elements();

        while(e.hasMoreElements())
        {
            BidHandler handler = (BidHandler) e.nextElement();

            try
            {

                handler.out.writeUTF(message);
                handler.out.flush();
            }

            catch(IOException ex)
            {
                handler = null;
            }
        }
    }

}



}

The AuctionItem Class

class AuctionItem
{
String itemName;
String itemDescription;
float itemPrice;
boolean itemStatus;

//Create a new auction item with name, description, price and status
public AuctionItem(String name, String description, float price, boolean status)
{
    itemName = name;
    itemDescription = description;
    itemPrice = price;
    itemStatus = status;
}

//return the price of the item.
public synchronized float getPrice()
{
    return itemPrice;   
}

//Set the price of the item.
public synchronized void setPrice(float newPrice)
{
    itemPrice = newPrice;
}

//Get the status of the item
public synchronized boolean getStatus()
{
    return itemStatus;

}

//Set the status of the item
public synchronized void setStatus(boolean newStatus)
{

    itemStatus = newStatus;

}

//Get the name of the item
public String getName()
{

    return itemName;

}

//Get the description of the item
public String getDescription()
{

    return itemDescription;

}


}

There is also simple GUI to go with this that seems to be working fine. If anyone wants it will include the GUI code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:03:58+00:00Added an answer on May 26, 2026 at 10:03 pm

    Your methods AuctionItem.getPrice and AuctionItem.setPrice are synchronized, but it’s really not sufficient here.

    In BitHandler.run you often have some if that checks the price and then you change the price. You need to synchronize these two operations together since another thread could change the price between those two operations.
    For example, if two thread try to set the new highest bid to 12.0 and 15.0, they will both determine that they are higher than 1.0 and they’ll both set the price, put you might end up with setPrice(15.0) followed by setPrice(12.0), which leaves the high bid at 12.0.

    You either can use an explicit lock to synchronize a series of operations on the price, or you can define a new synchronized method changePriceIfHigherThanHighest(float newPrice).

    You might have other synchronization bugs; I didn’t check thoroughly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a UDP client-server pair for a networks class, and I have hit
I am in the process of writing a TCP server using Berkely SOCKET API
I am writing an addin into Sql Server Management Studio, using the Visual Studio
I am writing a web application using Codeigniter for a client and am using
I'm writing some code to sync objects between a client and a server. It
I am writing a server, and I send each action of into a separate
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing a python program, and I came up with this error while using the
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using
I am writing a installer using WiX that will create a website and virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.