client/server Multi threading
Hi All,
I need help with two things,
I have a server (multithreaded) and I want multiple client to connect to this server. In the code ( see below), when client1 connects to the server (starts thread1) everything works correct but as soon as client2 (starts thread2)connects to the server, Client2 takes control of the server for processing. Now if I want the client1 to do some processing on the server it does not work correctly.
I do not understand how I should make the thread2 wait on the server when the thread1 is trying to execute and vice-versa. Who should I say wait() and who should I say notify()/notifyALL().
Also if client connection is closed how will the server understand and destroy the thread on the server ?
Please advice.
Sandeep.
****************** Server Code **********************
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;
public class netServermulti
{
private ServerSocket server;
private Socket connection;
private ObjectOutputStream out;
private ObjectInputStream in;
private serverThread[] serverthread;
Thread executingThread = null;
public static void main(String[] args)
{
netServermulti nServ = new netServermulti();
nServ.runServer();
}
private void runServer()
{
boolean keeprunning = true;
serverthread = new serverThread[10];
try
{
server = new ServerSocket(7000);
while (true)
{
System.out.println("Waiting for client connection");
for(int i = 0; i < 10; i++)
{
serverthread = new serverThread(server.accept(),i);
serverthread.start();
}
}
}
catch (EOFException eofException)
{
System.out.println("Client terminated the connection.");
}
catch (IOException ioException)
{
System.out.println("Network error: " + ioException.getMessage());
}
}
private void closeConnection() throws IOException
{
out.close();
in.close();
connection.close();
}
private class serverThread extends Thread
{
int num = 0;
int cthread = 0;
public serverThread(Socket socket, int num)
{
connection = socket;
System.out.println("connection received from : " +
connection.getInetAddress().getHostName());
this.num = num;
}
public void run()
{
boolean flag = false;
boolean done = false;
try
{
out = new ObjectOutputStream(connection.getOutputStream());
in = new ObjectInputStream(connection.getInputStream());
while (!done)
{
Object input = in.readObject();
out.writeObject(input);
out.flush();
}
closeConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
} // run
} //serverThread
}
*********** Client Code *****************
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class netClientmulti
{
public static char readCharacter(String input)
{
char ch;
try
{
System.out.print(input);
ch = (char)System.in.read();
for(;System.in.available() > 0; System.in.read());
}
catch(java.io.IOException e)
{
return (char) 0;
}
return ch;
}
public static Socket openSocket()
{
Socket s = null;
try
{
s = new Socket(InetAddress.getLocalHost(), 7000);
if (s == null)
{
//s = new Socket(getHostAddress(), 7000);
//if ( s == null)
{
System.out.println("Network Error : Cannot open sockets");
System.exit(0);
}
}
System.out.println("sockets opened");
}
catch (Exception e)
{
System.out.println(e.toString());
}
return s;
}
public static String readSocket(ObjectInputStream in) throws Exception
{
System.out.println("Inside ReadSocket method");
Object msg = in.readObject();
return (String)msg;
}
public static void writeSocket(ObjectOutputStream out, String Msg ) throws IOException
{
out.writeObject(Msg);
out.flush();
}
public static void buildMsg(ObjectOutputStream out, String Msg) throws Exception
{
writeSocket(out, Msg);
}
public static void askinput(String msg)
{
System.out.println(msg);
}
public static void main(String[] args)
{
try
{
Socket cSocket = openSocket();
//Input and Output readers.
ObjectInputStream in = new ObjectInputStream(cSocket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(cSocket.getOutputStream());
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
boolean more = true;
while (more)
{
askinput("Enter data :");
String data = br.readLine();
buildMsg(out, data);
String dataread = readSocket(in);
System.out.println("Data read is : " + dataread);
}
in.close();
out.close();
br.close();
cSocket.close();
}
catch (Exception E)
{
System.out.println("Error : " + E);
}
}
}

