I'm not sure what's going on here. I've got a TCP java server that waits for incoming connections.
In my TCP java client, I'm trying to make it so when I press the "connect" button on my GUI form, (when connect() is called in TCPClient.java), it opens up a new socket to the server.
This works, the server correctly displays that a new player joins when I press "connect" on the client.
Now I need the client to be able to have an input stream and an output stream to send/receive data on. I will do this on a separate thread. However when I create these 2 streams, the program completely freezes.
I don't want my program to freeze when I create the stream, because then it can't get to the point where I make my ListenFromServer thread:
package client;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient extends Thread {
UIClient ui;
public boolean connected = false;
Socket socket;
ObjectInputStream sInput; // to read from the socket
ObjectOutputStream sOutput; // to write on the socket
public TCPClient(UIClient ui) throws UnknownHostException, IOException{
this.ui = ui;
System.out.println("client start");
ui.console.println("CLIENT TEST");
}
public void connect() throws UnknownHostException, IOException{
try{
socket = new Socket("localhost", 2232);
}
catch(Exception e){
ui.console.println("Could not connect to server");
return;
}
try{
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException ioE){
ui.console.println("Exception creating new input/output streams");
return;
}
//Thread that listens for messages from the server
new ListenFromServer(this).start();
}
}
It freezes at this try statement:
try{
sInput = new ObjectInputStream(socket.getInputStream());
sOutput = new ObjectOutputStream(socket.getOutputStream());
}