Hi,
I have one server that creates thread for each client ,and in the client side each client creates thread to send multiple objects . It works for first requests but then my server wait from the client and the client wait response from the server. I have use synchronized in run( ) in server side and client side but it does not work.
Server code:
class ServerThread extends Thread
{
private Socket socket;
private ObjectInputStream objIn; //to read
private ObjectOutputStream objOut; // to write
public ServerThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout
)throws IOException
{
objIn =objin ; //read object
objOut = objout; //write object
socket =ss;
start(); // Calls run()
}//end constructor
public synchronized void run()
{
try{
while(true)
{ //accept objects and processd
}
}
catch(Exception e){
System.out.println( "Disconnected");}
}//end run
}//class Thread
public class server
{ protected static int PORT ;
static ServerSocket s;
static Socket client;
public static void main(String[] args) throws IOException
{
try {
PORT = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
PORT = 1500;
}
s = new ServerSocket(0);
try
{
while (true)
{
client = s.accept();
System.out.println("Client Accepted");
x++;
try{
ObjectOutputStream outToClient1 = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream inFromClient1 = new ObjectInputStream(client.getInputStream());
new ServerThread(client, inFromClient1, outToClient1);
clients.addElement(client);
}//end try
catch (IOException e)
{System.out.println("\nUnable to set up port!");
client.close();
}//end catch
}//end while
}//end main try
catch (Exception e)
{System.out.println("\nUnable to set up port!");}
finally
{
s.close(); //finally close socket
}//end finally
}//end main
}//end server class
Client code:
class ClientThread extends Thread
{
private Socket socket;
private ObjectOutputStream outToServer;
private ObjectInputStream inFromServer;
public ClientThread(Socket ss,ObjectInputStream objin, ObjectOutputStream objout , int cn
)throws IOException
{
inFromServer =objin ; //read object
outToServer = objout; //write object
socket =ss;
myid =cn;
start(); // Calls run()
}//end constructor
public synchronized void run()
{
for(int u=0;u<=5;u++)
{
//Send objects
}
}
}//end thread
public class client {
public static void main(String[] args) throws IOException
{
String host = "localhost";
int PORT=2911 ,x=1; ;
Socket link=null;
ObjectOutputStream outToServer1;
ObjectInputStream inFromServer1;
if(args.length == 2)
{
host = args[0];
try {
PORT = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1500 (default)");}
PORT = 1500;
} //end if
while( x<=5)
{
try {
link = new Socket(host, PORT);
}//end try
catch (UnknownHostException e)
{ System.out.println(e);}
try{
outToServer1 = new ObjectOutputStream(link.getOutputStream());
inFromServer1 = new ObjectInputStream(link.getInputStream());
new ClientThread(link, inFromServer1, outToServer1, x);
}//end try
catch (IOException e)
{System.out.println("\nUnable to set up port! " +e);
}//end catch
x++;
} //end while
}//end main
}//end client class
thanks
