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);

}

}

}

[5514 byte] By [ss_powai] at [2007-9-27 22:43:17]
# 1

There are some parts of the program i don't understand, try this:

Are you sure that you want to write the object back to the socket after reading it ?

import java.io.*;

import java.net.*;

public class Server

{

public Server()

{

}

public void go(Socket clnt)

{

ServerX server = new ServerX(clnt);

server.start();

}

public class ServerX extends Thread

{

Socketclnt = null;

OutputStreamout = null;

InputStreamins = null;

ObjectOutputStream oos;

ObjectInputStream ois;

public ServerX(Socket soc)

{

clnt = soc;

}

public void run()

{

try

{

ins = clnt.getInputStream();

out = clnt.getOutputStream();

oos = new ObjectOutputStream(out);

while (true)

{

ois = new ObjectInputStream(ins);

Object input = ois.readObject();

oos.writeObject(input);

oos.flush();

}

}

catch (InterruptedIOException e){}

catch (IOException e){}

catch(ClassNotFoundException e) {}

closeIt();

}

private void closeIt()

{

try

{

ins.close();

out.close();

clnt.close();

}

catch (IOException e){}

}

}

public static void main (String[] args)

{

Servermserv = new Server();

ServerSocket servS = null;

Socketclnt= null;

try

{

servS = new ServerSocket(4444);

while (true)

{

clnt = servS.accept();

mserv.go(clnt);

}

}

catch (IOException e)

{

}

}

}

Noah

noah.w at 2007-7-7 13:39:38 > top of Java-index,Core,Core APIs...
# 2

Noah,

After looking at your easy to understand code, I can see how easy it is to connect multiple client to the server.

Their was a small change I did to your code, I pulled ObjectInputStream(ins) outside the while loop.

The code that you replied to is my proto-type for class assignment.

Appreciate your help.

Sandeep.

ss_powai at 2007-7-7 13:39:38 > top of Java-index,Core,Core APIs...
# 3
From experience leave the "ObjectInputStream(ins)" in the loop, a new object must be created for each read, Noah
noah.w at 2007-7-7 13:39:38 > top of Java-index,Core,Core APIs...
# 4

Hi Sandeep - I didn't notice that you'd cross-posted this question. I posted a reply to the same question on the 'Distributed Computing General' forum with some working example code.

Heres a link:

http://forums.java.sun.com/thread.jsp?forum=11&thread=313805

Hope this helps,

Kevin Hooke

khooke at 2007-7-7 13:39:38 > top of Java-index,Core,Core APIs...