Results 1 to 10 of 10
  1. #1
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Basics of Networking

    Hey Guys,

    I have two classes: a Server class and Client class. I wrote a simple chat application but it doesn't seem to work. I looked over the code plenty of times but I cant find the problem. Can you guys look over the code?

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class Client_Main {
        
        BufferedReader reader;
        PrintWriter writer;
        
        public static void main(String[] args) {
            
            Client_Main main = new Client_Main();
            main.go();
        }
    
        private void go() {
            
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter an IP Address: ");
            
            try
            {
                Socket socket = new Socket("192.168.1.105", 4444);
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                writer = new PrintWriter(socket.getOutputStream());
            }
            catch (Exception e)
            {
                System.err.println("Failed to connect!");
                System.exit(1);
            }
            
            System.out.println("Network Established!");
            
            Thread readerThread = new Thread(new IncomingReader());
            readerThread.start();
            
            while(true)
            {
                String message = scan.next();
                System.out.println(message);
                writer.print(message);
            }
        }
        
        private class IncomingReader implements Runnable {
    
            @Override
            public void run() {
                
                String message;
                
                try
                {
                    while ((message = reader.readLine()) != null)
                    {
                        System.out.println(message);
                        
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            
            
        }
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    
    public class Server_Main {
        
        ArrayList clientOutputStreams;
        
        public static void main(String[] args) {
            
            Server_Main main = new Server_Main();
            main.go();
        }
    
        private void go() {
            
            clientOutputStreams = new ArrayList();
                    
            try 
            {
                ServerSocket serverSocket = new ServerSocket(4444);
                
                while(true)
                {
                    Socket clientSocket = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
                    clientOutputStreams.add(writer);
                    
                    Thread t = new Thread(new ClientHandler(clientSocket));
                    t.start();
                    
                    System.out.println("Got Connection!");
                }
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }    
        }
            
        private class ClientHandler implements Runnable {
            
            BufferedReader reader;
                    
            public ClientHandler(Socket clientSocket) {
                
                try 
                {                
                    reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            
            @Override
            public void run() {
                
                String message;
                
                try
                {               
                    while ((message = reader.readLine()) != null)
                    {
                        System.out.println("Read: " + message);                    
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }       
        }
    }

    Thanks!
    Tabish Chasmawala

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

    Default Re: Basics of Networking

    As far as I can see your server doesn't send anything back to the client; that means that your IncomingReader object in the client doesn't print anything. Does your server print what it has read from the client?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Re: Basics of Networking

    Yes. What I want to happen for now is the client sends a message to the server, and the server prints that message out in console.

    This is the server code which reads the message and prints it out to console:

    Java Code:
    String message;
                
                try
                {               
                    while ((message = reader.readLine()) != null)
                    {
                        System.out.println("Read: " + message);                    
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
    Tabish Chasmawala

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

    Default Re: Basics of Networking

    You could try a writer.flush() on the client side after it has written some text to the server.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Basics of Networking

    Try adding a few more debugging statements to see what is happening.
    For example use the the InputStream's available() method to see if there are any bytes ready to be read.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Basics of Networking

    For testing I use the localhost IP address to keep it simple:

    Java Code:
    //Socket socket = new Socket("192.168.1.105", 4444);
    Socket socket = new Socket( "127.0.0.1", 4444 );
    In addition to the suggestion by JosAH, I found I needed to use the println() method:
    Java Code:
    //writer.print(message);
    writer.println(message);
    writer.flush();

  7. #7
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Re: Basics of Networking

    Yea i fixed the problem by using this code:

    Java Code:
    while(true)
            {
                String message = scan.next();
                System.out.println(message);
                writer.println(message);
                writer.flush();
            }
    But I do not get why the code does not work using the Printwriter.print() method? Any ideas? I can probably think of a way that I dont need to use the method but I am curious on the reason that it is not working.
    Tabish Chasmawala

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Basics of Networking

    why the code does not work using the Printwriter.print() method?
    That part of the code does work. If you leave in the call to the print method and replace the code that reads as follows you will see that the print did work:
    Java Code:
    /*
                    while ((message = reader.readLine()) != null)
                    {
                        System.out.println("S read: msg=" + message);                    
                    }
    */             
                   // Use read() vs readLine() <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                   int c;
                   System.out.print("S reading:");
                   while((c = reader.read()) != -1)
                      System.out.print((char)c);

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Basics of Networking

    But I do not get why the code does not work using the Printwriter.print() method?
    Your code is blocking on a readLine() method. So the readLine() method can't complete until it receives a newline string.

  10. #10
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Re: Basics of Networking

    Oh okay. That makes perfect sense.

    Thanks for your help everyone!
    Tabish Chasmawala

Similar Threads

  1. Basics
    By avish12 in forum SWT / JFace
    Replies: 2
    Last Post: 06-09-2010, 03:04 PM
  2. What loop? Java Networking Basics
    By JonnySnip3r in forum Networking
    Replies: 0
    Last Post: 01-28-2010, 08:37 PM
  3. Really Basics
    By Taluntain in forum New To Java
    Replies: 16
    Last Post: 10-08-2009, 09:43 AM
  4. Basics of Servlet
    By pcvardhan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 02:07 PM
  5. Basics
    By AKP in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 12:06 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
  •