Results 1 to 2 of 2
  1. #1
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default Simple client and server chat system

    Hi Guys,

    I'm adding network functionality to my Chess game so that users can play over the net and chat whilst 'in game'.

    At the moment I'm using a very simple client & server to enable connectivity (will only send a message one way). I'm not having trouble with sending messages however on the server side that game becomes unresponsive when the server instance is run.

    The code for the server looks like this :

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class SimpleServer {
    
    public SimpleServer() {
    
    ServerSocket s = null;
    Socket s1;
    String sendString ="Hello network world!!!!";
    OutputStream os;
    DataOutputStream dos;
    
    try {
    s = new ServerSocket(5432);
    } catch (IOException e) { }
    while (true) {
    try {
    // wait here and listen for a connection
    s1 = s.accept();
    //Get a communication stream associated with the socket
    os = s1.getOutputStream();
    dos = new DataOutputStream (os);
    //Send your string! (UTF provides machine independence)
    dos.writeUTF(sendString);
    // close the connection, but not the server socket
    dos.close();
    os.close();
    s1.close();
    } catch (IOException e) {
    // ignore 
      } } }
    }
    As I said its very simple. I'm thinking that the problem is due to threading... well to lack of it! But I don't know for certain...

    Any help?

    Thanks chaps!:cool:

  2. #2
    danborgir is offline Member
    Join Date
    Jan 2011
    Posts
    26
    Rep Power
    0

    Default

    Found the issue, the Simple Server needed to be ran in its own thread:

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class SimpleServer extends Thread {
    
    public void run() {
    
    ServerSocket s = null;
    Socket s1;
    String sendString ="Hello network world!!!!";
    OutputStream os;
    DataOutputStream dos;
    
    try {
    s = new ServerSocket(5432);
    } catch (IOException e) { }
    while (true) {
    try {
    // wait here and listen for a connection
    s1 = s.accept();
    //Get a communication stream associated with the socket
    os = s1.getOutputStream();
    dos = new DataOutputStream (os);
    //Send your string! (UTF provides machine independence)
    dos.writeUTF(sendString);
    // close the connection, but not the server socket
    dos.close();
    os.close();
    s1.close();
    } catch (IOException e) {
    // ignore 
      } } }
    }
    Then to start it:

    Java Code:
    Thread thread = new SimpleServer();
    				thread.start();

Similar Threads

  1. Multithreaded Client/Server Chat program
    By f0ns in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-21-2009, 05:26 PM
  2. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 12:22 AM
  3. [SOLVED] UDP chat client server
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 04-25-2009, 01:51 AM
  4. Multi Client/Server Chat Question
    By Kodak07 in forum Networking
    Replies: 3
    Last Post: 03-29-2009, 10:50 PM
  5. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •