Hi
I'm working on a connect 4 game which is must be applied on a network (server and client).
The first thing, I wrote a server connection class. This class is for chatting between two people.
First when the two people connect to the server, the server will send number 1 to the first person and number 2 to the second person. Also, I must allow the person who gets number 1 chatting first and the second one can not chat until the first one done. Also, when the person number 1 sends any thing, it must be appeared in the screen of person number 2 and vise versa.
The problems are:
1) when the person number 1 sends any thing, it show up in the screen of the second person but the problem when the second person sends any thing, it doesn't show up in the screen of the first one.
2) I couldn't make the person who gets number 1 to play first, that mean the person who gets number 2 can start chatting before the first one and this is forbidden.
This is my code
Code:import java.net.*;
import java.io.*;
public class ConnectFourServer {
public static void main(String[] args) throws IOException {
//ConnectProtocol board = new ConnectProtocol();
ServerSocket ss = null;
try {
ss = new ServerSocket(1550);
} catch (IOException e) {
System.err.println("Could not listen on port: 1550.");
System.exit(1);
}
Socket cs1 = null;
Socket cs2 = null;
try {
cs1 = ss.accept();
cs2 = ss.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
BufferedReader in = null;
PrintWriter out = null;
BufferedReader in1 = null;
PrintWriter out1 = null;
try {
in = new BufferedReader
(new InputStreamReader(cs1.getInputStream()));
out = new PrintWriter
(cs1.getOutputStream(), true /* autoFlush */);
in1 = new BufferedReader
(new InputStreamReader(cs2.getInputStream()));
out1 = new PrintWriter
(cs2.getOutputStream(), true /* autoFlush */);
out.println( "1" );
out1.println( "2" );
boolean done = false;
String line="";
String line1="";
while (!done) {
line = in.readLine();
if (line == null ) { done = true; }
else {
out.println("Echo: " + line);
out1.println("Echo: " + line);
if (line.trim().equals("BYE")) {
done = true;
}
}
}//while
while (!done) {
line1 = in1.readLine();
if ( line1 == null) { done = true; }
else {
out.println("Echo: " + line1);
out1.println("Echo: " + line1);
if (line1.trim().equals("BYE")) {
done = true;
}
}
}//while
}
catch (IOException e) {
System.err.println("Unable to read from or write to the client: "
+ e.getMessage());
}
cs1.close();
ss.close();
}
}

