Results 1 to 5 of 5
  1. #1
    the_flirt is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Question Need help with chat client

    I came up with the following code for sending a simple message from a server to a client and getting a reply back.

    Java Code:
    //SERVER
    import java.io.*;
    import java.net.*;
    
    class ServerTest
    {
        public static void main(String args[]) throws IOException
        {
            String ostr = "Hi this is the server!";
            String istr;
            BufferedReader br;
            BufferedWriter bw;
            ServerSocket ss = new ServerSocket(9999);
            System.out.println("Waiting for connection...");
            Socket s = ss.accept();
            System.out.println("Connection accepted!");
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(ostr);
            while((istr = br.readLine())!= null)
            {
                System.out.println(istr);
            }
            br.close();
            bw.close();
            s.close();
        }
    }
    
    //CLIENT
    import java.io.*;
    import java.net.*;
    
    class ClientTest
    {
        public static void main(String args[]) throws IOException
        {
            String ostr = "Hi this is the client!";
            String istr;
            BufferedReader br;
            BufferedWriter bw;
            Socket s = new Socket("127.0.0.1", 9999);
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            while((istr = br.readLine())!= null)
            {
                System.out.println(istr);
            }
            bw.write(ostr);
            br.close();
            bw.close();
            s.close();
        }
    }
    The connection is working. I get the connection accepted message. But the messages are not being displayed.
    What am I doing wrong? Either there is a problem with sending and receiving. Or with displaying the received text.
    Please help...

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Your code looks good and should work. try flushing your writer:

    Java Code:
    bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(ostr);
            // insert this:
            bw.flush ();
            // done inserting
            while((istr = br.readLine())!= null)
    cause otherwise what may be happening, you're writing to the writer, it caches it in memory and not actually sends anything out (BufferedWriters are all about doing that), then on the client you never get anything to read, so you're not writing anything on the client, so on the server you get nothing to read...

  3. #3
    the_flirt is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Question

    Hi,
    Thank you for replying.

    I tried what you told me, but it didn't work. :(
    I'm still not getting the messages displayed. Just the "Waiting for connection..." and "Connection accepted!" text.

    Please suggest some other solution. I really need to get this to work to complete my project. :(

    PS: Maybe I'm doing something wrong in printing the messages?As in they're being sent and received, but not being displayed? I just thought my printing part of the code was kind of sloppy:
    Java Code:
    while((istr = br.readLine())!= null)
    		{
    			System.out.println(istr);
    		}
    Last edited by the_flirt; 04-17-2010 at 08:43 PM.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    try using DataInputStream and DataOutputStream instead:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    class ServerTest
    {
        public static void main(String args[]) throws IOException
        {
            String ostr = "Hi this is the server!";
            String istr;
            DataInputStream din;
            DataOutputStream dout;
            ServerSocket ss = new ServerSocket(9999);
            System.out.println("Waiting for connection...");
            Socket s = ss.accept();
            System.out.println("Connection accepted!");
            din = new DataInputStream(s.getInputStream());
            dout = new DataOutputStream(s.getOutputStream());
            dout.writeUTF(ostr);
            istr=din.readUTF();
            try {
    			Thread.sleep(2222);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            System.out.println(istr);
            din.close();
            dout.close();
            s.close();
        }
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    
    class ClientTest
    {
        public static void main(String args[]) throws IOException
        {
            String ostr = "Hi this is the client!";
            String istr;
            DataInputStream din;
            DataOutputStream dout;
            Socket s = new Socket("127.0.0.1", 9999);
            din = new DataInputStream(s.getInputStream());
            dout = new DataOutputStream(s.getOutputStream());
            istr = din.readUTF();
            
            dout.writeUTF(ostr);
            din.close();
            dout.close();
            s.close();
            System.out.println(istr);
        }
    }

  5. #5
    the_flirt is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Can you explain why you used a DataInput and Output stream? Whats the difference between DataInputStream and Output streams and Buffered reader and writer? I know that the streams transfer bytes while reader, writers transfer characters. But why did you specifically use DataInput and Output stream?
    Why not any other stream?
    And is it a hit and trial method to find out what works? How did you know that these streams would work?

Similar Threads

  1. Chat Client
    By Jessi34 in forum New To Java
    Replies: 5
    Last Post: 12-09-2009, 03:49 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. Help with chat client program
    By srivigneshwar in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 06:32 PM
  5. Multi Client/Server Chat Question
    By Kodak07 in forum Networking
    Replies: 3
    Last Post: 03-29-2009, 10:50 PM

Tags for this Thread

Posting Permissions

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