We got 3 computers, lets call them A,B and C. A and B can connect and chat without problems. then we try A and C, they can connect but only C gets the messages and A get the error IOException at line 27 in the client code(see below)
this.out = new PrintStream(socket.getOutputStream(), true);
So it seems like the server are connected but it fails to get OutPutStream. We tried this on another computer and it didn't work there either.
The ports are open and there is no firewall blocking the connection.
Any idea on how to solve this?
The client code:
public class Client {
private Socket socket = null;
private PrintStream out = null;
private Thread cThread = null;
private InetAddress address = null;
private SocketAddress SA;
public Client(InetSocketAddress address) {
System.out.println("Connecting client...");
socket = new Socket();
SA = address;
try {
socket.connect(SA, 0);
} catch (IOException e) {
// Communication.Inlist.put(/*new SysMsg*/("Couldn't connect."));
System.out.println("Cant connect.");
}
System.out.println("Client connected.");
send();
}
public void send() {
try {
this.out = new PrintStream(socket.getOutputStream(), true);
System.out.println("Writer.");
} catch (IOException e) {
System.out.println("Couldn't get outputStream");
}
cThread = new Thread(new clientThread());
cThread.start();
}
public void stop() {
cThread.interrupt();
}
private class clientThread implements Runnable{
private String output;
public clientThread() {
}
public void run() {
while(true)
{
while ((output = Communication.Outlist.get()) != null) {
//output = Communication.Outlist.get();
out.println(output);
/*if (output != null) {
out.print(output);
}*/
}
}
}
}
}
The server code:
public class Server {
private ServerSocket serverSocket = null;
private Thread sThread = null;
public Server(int port){
try {
this.serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not listen on port.");
e.printStackTrace();
}
}
public void receive() {
sThread = new Thread(new serverThread());
sThread.start();
}
public void stop() {
sThread.interrupt();
}
private class serverThread implements Runnable{
private String input;
private Socket socket;
private BufferedReader in = null;
public serverThread() {
}
public void run() {
System.out.println("Server is listening.");
try {
this.socket = serverSocket.accept();
System.out.println("Connected.");
} catch (IOException e) {
System.out.println("Could not accept.");
e.printStackTrace();
}
try {
this.in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
System.out.println("Could not create the reader.");
e.printStackTrace();
}
try {
while ((input = in.readLine()) != null) {
//Communication.Inlist.put(input);
Communication.Inlist.put(input);
System.out.println(input);
Mainwindow.temp = input;
}
} catch (IOException e) {
System.out.println("Incoming failed.");
e.printStackTrace();
}
}
}
}

New Topic/Question
Reply




MultiQuote




|