Matt Hyde

Greenhorn
+ Follow
since Mar 25, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Matt Hyde

I'm having a few problems using sockets and connecting to a server.

I've got a server.jar file that runs on my machine and I have a client interface that needs to connect to the server and send and recieve messages using output/input streams.

Firstly, the socket constructor is Socket(remoteHost, port). I've got the port number, but as the server is running on my own machine should I use my hostname/ip address as the remoteHost as this is what I'm currently doing? This method does connect to the server (which is confirmed in the terminal window running the server), which in turn creates a GUI.

Once connected, I'm able to send messages to the server using a BufferedWriter. There is a logout command which is called by a button on the GUI. To logout, a message is sent to the server and log's the user off correctly. However, I also need to close the socket. This is the code I'm using >

try {
client.getSocket().close();
} catch (Exception e) {
System.err.println(e);
System.exit(1);
}

When this code is inserted and the logout button is clicked the system freezes and doesn't logout, despite this code appearing after the logout command has been sent to the server. Is there any obvious reason why this would happen?