-1

I have TCP client/server. Client is running two thread C1 and C2. And server is running three threads S1, S2, and S3 threads. C2 should receive results after server finishes S3. I have problem in the last step. The client and server look like this. Please suggest.

Public class client {
    public static void main (String[] args)
    {
    ClientThread c1= new ClientThread(); // send task to server
    ClientThread c2= new ClientThread(); // result receive
    c1.start();
    c1.join();
    c2.start();
    c2.join();
    }
    }

Server looks like this:

Public class server {
    public static void main (String[] args)
    {
    ServerThread s1= new ServerThread(); // receive from client
    ServerThread s2= new serverThread(); // calculate
    ServerThread s3= new serverThread(); // send to client
    s1.start();
    s1.join();
    s2.start();
    s2.join();
    s3.start();
    s3.join();
    }
    }

The order of the output should look like this:

c1 sends task
s1 receives task
s2 calculates
s3 sends result to client
c2 receives final result
peter
  • 17
  • 3
  • 1
    Why? All the server threads run sequentially, so they don't need to be threads at all. Your problem is that you have start/join/start/join, which makes no sense: start followed by join isn't multithreading at all really, it is sequential execution. It should be start/start/join/join. – user207421 Feb 26 '17 at 00:59
  • I have changed like start/start/join/join in both server and client. However, C2 still starts (i.e., reading on socket) before S3 (i.e., writing result in socket). How to ensure that C2 only starts receiving result after S3 finishes writing result? – peter Feb 26 '17 at 17:05

1 Answers1

0

s1.join may wait, so s2 may not be started or maybe started later than you want. so do all the starts first.

same goes for the clients, so do all the starts first, then do the joins.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • I have changed like start/start/join/join in both server and client. However, C2 still starts (i.e., reading on socket) before S3 (i.e., writing result in socket). How to ensure that C2 only starts receiving result after S3 finishes writing result? – peter Feb 26 '17 at 21:06
  • it looks like you have too many clients and servers. clients normally send some request and wait for a response. servers normally wait for a request then send a response. the server may spin off a new thread to process the clients request. – Ray Tayek Feb 28 '17 at 04:21
  • Here is the scenario: There is one only client and one server. Server receives request (file) in one thread (S1). Server starts another thread (S2) and processes that file. Now, server starts another thread (S3) to send the file (that was saved in directory by previous thread S2). Client C1 thread sends request (file) to server. Client C2 thread will receive file and write in directory. When server S3 thread tries to send file to client, S3 thread throws socket.exception: socket is closed, server :null. In client also, socket.exception: socket is closed, server :null. What could be the reason – peter Feb 28 '17 at 19:34
  • ok, then your original server start/join order may be correct. and c2 needs to start and wait for a connection. you probably need to post more code. you may be trying to use the same socket or something. – Ray Tayek Feb 28 '17 at 20:07