0

I have a socket programming code that goes..

Server Program

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

public class GreetingServer extends Thread
{
private static int port;
  private ServerSocket serverSocket;

  public GreetingServer(int port) throws IOException
   {
  serverSocket = new ServerSocket(port);
  serverSocket.setSoTimeout(50000);
   }

 public void run()
  {
     while(true)
     {
        try
         {
           System.out.println("Waiting for client on port " +
           serverSocket.getLocalPort() + "...");
           Socket server = serverSocket.accept();
           System.out.println("Just connected to "
                + server.getRemoteSocketAddress());
           DataInputStream in =
              new DataInputStream(server.getInputStream());
           System.out.println(in.readUTF());
           DataOutputStream out =
             new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
             + server.getLocalSocketAddress() + "\nGoodbye!");
        server.close();
     }catch(SocketTimeoutException s)
     {
        System.out.println("Socket timed out!");
        break;
     }catch(IOException e)
     {
        e.printStackTrace();
        break;
       }
    }
 }
     public static void main(String [] args)
     {
       port=9000;
       try
     {
       Thread t = new GreetingServer(port);
       t.start();
     }catch(IOException e)
    {
      e.printStackTrace();
    }
   }
 }

And the client side code that is...

import java.io.*;
import java.net.*;
public class GreetingClient
 {
   private static String serverName;
   public static void main(String [] args)
    {
      String sName = "MyServerName";
      int port = 9000;
      try
       {
        System.out.println("Connecting to " + sName
                         + " on port " + port);
     Socket client = new Socket(sName, port);
     System.out.println("Just connected to "
                  + client.getRemoteSocketAddress());
     OutputStream outToServer = client.getOutputStream();
     DataOutputStream out =
                   new DataOutputStream(outToServer);

     out.writeUTF("Hello from "
                  + client.getLocalSocketAddress());
     InputStream inFromServer = client.getInputStream();
     DataInputStream in =
                    new DataInputStream(inFromServer);
     System.out.println("Server says " + in.readUTF());
     client.close();
  }catch(IOException e)
    {
    }
  }
 }

The code compiles fine. But when i run the program, first the server and then the client, the server displays that

Waiting for client on port 9000...
Socket timed out!

and the client shows

Connecting to MyServerName on port 9000

What is wrong with the code?? I have tried increasing and decreasing the timeout values but it gives the same output.

1
  • Are you expecting client to connect to server? Commented Nov 26, 2015 at 10:55

2 Answers 2

5

What is wrong with the code?

There's nothing wrong with your code. You set a 50 second accept timeout, accept blocked for 50 seconds without a client trying to connect, so a SocketTimeoutException was thrown. Everything here is working as designed.

Of course it's possible that:

  • your accept timeout is too short
  • you don't want to abort your server just because of an accept timeout
  • you don't want an accept timeout at all.

All of these are design decisions that depend on your requirements.

It's also possible that you got the host name wrong in the client, but as you're ignoring exceptions in the client:

catch(IOException e)
{
}

there is no way you will ever find out. Never ignore IOExceptions.

Sign up to request clarification or add additional context in comments.

Comments

0

Above code is perfectly working with sName in client as 'localhost', since I am running both client and server in my local:

String sName = "localhost";

Verify your server-name if you are expecting client to connect to server.

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.