Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Socket beginner

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic