4

This is my code for Socket Programming...

import java.io.*;
import java.net.*;

public class EchoClient
{
    public static void main(String[] args)
    {
        try
        {
            Socket s = new Socket("127.0.0.1",9999);
            BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter w = new PrintWriter(s.getOutputStream(),true);
            BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
            String line;

            do
            {
                line = r.readLine();

                if (line != null)
                {   System.out.println(line);   }

                line = con.readLine();
                w.println(line);
            }
            while(!line.trim().equals("bye"));
        }

        catch(Exception err)
        {   System.err.println(err);    }
    }
}

Code for Server:

import java.io.*;
import java.net.*;

public class EchoServer
{
    public EchoServer(int portnum)
    {
        try
        {    server = new ServerSocket(portnum);    }

        catch(Exception err)
        {    System.out.println(err);   }
    }

    public void serve()
    {
        try
        {
            while(true)
            {
                Socket client;
                client = server.accept();

                BufferedReader r;
                r = new BufferedReader(new InputStreamReader(client.getInputStream()));

                PrintWriter w = new PrintWriter(client.getOutputStream(),true);
                w.println("Welcome to Java EchoServer. Type 'bye' to close.");

                String line;

                do
                {
                    line = r.readLine();

                    if (line != null)
                    {   w.println("Got: " + line);  }
                }
                while(!line.trim().equals("bye"));

                client.close();
            }
        }

        catch(Exception err)
        {   System.err.println(err);    }
    }

    public static void main(String[] args)
    {
        EchoServer s = new EchoServer(9999);
        s.serve();
    }

    private ServerSocket server;
}

The program keeps on taking input in an infinite loop and giving no response even when I type "bye". I think the main problem lies in Server.accept() that seems to be not working. What might be wrong?

10
  • 2
    127.0.0.1 is localhost. Are you sure there is a server running in port 999? Commented Nov 15, 2013 at 10:42
  • This question appears to be off-topic because it is about basic networking. Commented Nov 15, 2013 at 10:43
  • Are you sure ServerSocket serverSocket = new ServerSocket(9999); is running in localhost. Commented Nov 15, 2013 at 10:44
  • Please learn basic networking. Especially, when you connect to a specific port on some host, it is not guaranteed that there is someone listening. If there is no server lstening, you get "Connection refused". Commented Nov 15, 2013 at 10:45
  • it's working now. I changed the IP. But there's one other problem now. The program seems to be in an infinite loop. It's keep taking input and not exiting even if I type "bye". Commented Nov 15, 2013 at 10:46

1 Answer 1

1

Change client side code

line = r.readLine();

to

line = con.readLine();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.