Results 1 to 6 of 6
  1. #1
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Simple Chat Program

    so im making a simple instant messaging program. My goal is to have it so you can send messages to each other from seperate computers, i have this working except that if you try and send more than one message the second message you send is received as null and i cannot figure out why. Heres my code.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package webchat;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    /**
     *
     * @author Anthony
     */
    public class WebChat {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            boolean chatOver = false; 
            final int Chat_Send_Port = 1234;
       
             ServerSocket server = new ServerSocket(Chat_Send_Port);
           
            System.out.println("Waiting for clients to connect");
           
            Socket s = new Socket("Their Ip Address", Chat_Send_Port); //ip address of other computer. 
    
            Socket s2 = server.accept();
    
            System.out.println("Client connected.");
                
           
    
            final int Chat_Listen_Port = 1235;
               
            ServerSocket server1 = new ServerSocket(Chat_Listen_Port);
               
            System.out.println("Waiting for Clients to connect");
               
            Socket s1 = new Socket("My Ip Address", Chat_Listen_Port); //ip address of your computer
               
             s1 = server1.accept();
     
             System.out.println("Client connected");
    
             BufferedReader in1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
             
             PrintWriter out = new PrintWriter(s2.getOutputStream(), true);   
                  
    while (!chatOver)
                {
                   Scanner in = new Scanner(System.in);
                  
                  System.out.println("Please type the message you want to send");
                 
                  String data = in.nextLine();
                  
                  out.print(data);
           
                 out.close();
          
                 System.out.print("Received string: ");
    
            
             System.out.println(in1.readLine()); // Read one line and output it
    
             System.out.print("\n");
          
                }
            s2.close();
           
           server.close();
        }
    }
    Last edited by IceNine; 04-20-2013 at 04:25 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Chat Program

    Why are you closing your 'out' stream in the body of your while loop?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Simple Chat Program

    because if i don't close it there no string gets received

    Quote Originally Posted by JosAH View Post
    Why are you closing your 'out' stream in the body of your while loop?

    kind regards,

    Jos

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Chat Program

    Quote Originally Posted by IceNine View Post
    because if i don't close it there no string gets received
    Flush the stream then, because if you close it you can't write to it anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Simple Chat Program

    i have tried replacing .close() with .flush() but when i do that it hangs at line 73 and nothing happens.

    Quote Originally Posted by JosAH View Post
    Flush the stream then, because if you close it you can't write to it anymore.

    kind regards,

    Jos

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Chat Program

    Quote Originally Posted by IceNine View Post
    i have tried replacing .close() with .flush() but when i do that it hangs at line 73 and nothing happens.
    That happens because you have both a 'client' and a 'server', i.e. a sender and receiver in one single program and in one single thread. It is never going to work this way. Read up about threads.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple RMI chat application
    By usmangt in forum New To Java
    Replies: 3
    Last Post: 09-27-2010, 07:37 AM
  2. simple chat program
    By munishmhr in forum Networking
    Replies: 2
    Last Post: 03-25-2009, 04:00 PM
  3. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 02:30 AM
  4. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  5. Simple serverless chat solution
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 01-07-2008, 03:25 PM

Posting Permissions

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