0

I wrote a small client server program in Java. Server creates a ServerSocket on some port and keeps listening to it. Client sends some sample info to this server. When I run Client the first time connection is accepted by server and info is printed by server. Then Client program exits. When I run Client again, connection is accepted however, data is not printed. Please check following code.

Server Program

package javadaemon;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MyDaemon {
    public static void main(String [] args) throws IOException {
            ServerSocket ss = new ServerSocket(9879);
            ss.setSoTimeout(0);
        while(true) {
            Socket s = ss.accept();
            System.out.println("socket is connected? "+s.isConnected());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println("Input stream has "+dis.available()+" bytes available");
            while(dis.available() > 0) {
                System.out.println(dis.readByte());
            }
        }
    }
}

Client Program

package javadaemon;

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

public class Client {
public static void main(String [] args) {
    try {
        System.out.println("Connecting to " + "127.0.0.1"
                            + " on port " + 9879);
        Socket client = new Socket("127.0.0.1", 9879);
        System.out.println("Just connected to "
                    + client.getRemoteSocketAddress());
        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);

        for(int i=0; i<100; i++ ) {
            out.writeUTF("Syn "+i);
        }

    } catch(IOException e) {
    }
}
}

Please help me in finding why next time no data is received by server.

3
  • call out.flush() and out.close() after for loop. Commented May 26, 2013 at 14:14
  • Avoid using empty catch blocks , they defeat the purpose of Exception Handling Commented May 26, 2013 at 14:33
  • By the way eating exceptions isn't doing any good specially when your program isn't working correctly and you want to debug it. Commented May 26, 2013 at 14:33

2 Answers 2

1

The reason is before server side receive data, the Client already exits, I just debug it. (Can't explain whey it works at the first time.) Just change your code like the below, and it works.

package javadaemon;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



public class MyDaemon {
    public static void main(String [] args) throws IOException {
            ServerSocket ss = new ServerSocket(9879);
            ss.setSoTimeout(0);
        while(true) {
            Socket s = ss.accept();
            System.out.println("socket is connected? "+s.isConnected());
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println("Input stream has "+dis.available()+" bytes available");
            while(true) {
                try {
                    System.out.println(dis.readByte());
                } catch (Exception e) {
                    break;
                }
            }
        }
    }
}

The Client must add flush before exits,

package javadaemon;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
public static void main(String [] args) {
    try {
        System.out.println("Connecting to " + "127.0.0.1"
                            + " on port " + 9879);
        Socket client = new Socket("127.0.0.1", 9879);
        System.out.println("Just connected to "
                    + client.getRemoteSocketAddress());
        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);

        for(int i=0; i<100; i++ ) {
            out.writeUTF("Syn "+i);
        }
        out.flush();
        try {
             Thread.sleep(1000L);
        } catch (InterruptedException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
    } catch(IOException e) {
        e.printStackTrace();
    }
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am seeing from couple of other sources like these stackoverflow.com/questions/11982693/…, that .available() method of DataInputStream is not reliable when used with Sockets. Maybe that's what was causing the problem
He is not flushing the stream and closing the client socket. That's his problem.
0

Place this in your Client program after for loop:

out.flush();
out.close();
client.close();

You need to flush your stream in order to push the data forward and clean the stream. Also, you will need to close your client socket.

I just tested this successfully.

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.