Search code examples
javasocketsserversocket

Java server program doesn't accept the input from client at second run of client


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.


Solution

  • 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();
        }
    }
    }