Gerko Ford
Gerko Ford

Reputation: 51

Why TCP/IP server with listening port in java not working

I have written a code in java to interface my computer with a transmitter a transmitter device, with a communication board already implemented and ready to connect via TCP/IP to any server with a specific address IP (say 192.168.2.2) and listening to a specific port number (say 4000).

I followed the exact strep how to create a server side application in Java offering a that listening port, so that I can connect to that transmitter. I don't understand why when I try to debug the code, it blocks a the line clientSocket = serverSocket.accept(), and throws a timeout exception.

Could someone help me find out where the error might be in my code? Any help would be greatly appreciated. Thanks.

Here is the code:

public class Server {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


            //Declares server and client socket, as well as the input and the output stream
            ServerSocket serverSocket = null;
            Socket clientSocket = null;
            PrintWriter out;
            //BufferedReader in;
            BufferedReader in;


            try{

                InetAddress addr = InetAddress.getByName("192.168.2.2");

                //Opens a server socket on port 4000
                serverSocket = new ServerSocket(4000) ;

                //Sets the timeout
                serverSocket.setSoTimeout(30000);

                System.out.println("Server has connected"); 

                //Create a connection to server
                System.out.println("Server listening connection from client ....");

                //Listens and waits for client's connection to the server
                clientSocket = serverSocket.accept();

                // Creates input and output streams to socket
                out = new PrintWriter(clientSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                //Reads response from socket
                while((in.readLine())!= null  ){
                    System.out.println ( in.readLine() );
                }

                System.out.println ( "Closing connection ....");

            //Terminates connection
            clientSocket.close();   
            serverSocket.close();

            System.out.println("Connecton successfully closed");                
            }
            catch(IOException e){
                System.out.println(e);
            }           
    }
}

Upvotes: 0

Views: 2583

Answers (2)

user207421
user207421

Reputation: 310860

Could someone help me find out where the error might be in my code?

There is no error in your code that could cause this problem. Clearly you haven't configured the device to connect to this server correctly, or the device isn't running, or it isn't connecting, or there is a firewall in the way. Investigate that.

However:

InetAddress addr = InetAddress.getByName("192.168.2.2");

What is this for? It isn't used.

System.out.println("Server has connected"); 

This is simply not true. The server hasn't connected. At this point all it has done is create a listening socket.

while((in.readLine())!= null  ){

Here you are reading a line and throwing it away.

    System.out.println ( in.readLine() );

Here you are printing every second line, having thrown every odd line away. The correct way to write this loop is:

String line;
while ((line = in.readLine()) != null)
{
    System.out.println(line);
}

Note also that this server will service exactly one client and then exit. There should be a loop around everything from accept() to clientSocket.close(), and if there are multiple devices it should start a new thread per accepted socket to handle the I/O.

Upvotes: 3

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

You specified timeout of 30 seconds, didn't you? :

serverSocket.setSoTimeout(30000);

So after 30 seconds, no matter whether stopped in debugger or running, this will timeout and throw exception.

Upvotes: 1

Related Questions