• Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket beginner  RSS feed

 
Matt Hyde
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the system freezes or have you not written anything to display that the connection is gone.
button1.setText("loggedoff");
just wondering
roba
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!