Java服务器程序在第二次运行客户端时不接受来自客户端的输入

时间:2013-05-26 14:08:41

标签: java sockets serversocket

我用Java编写了一个小型客户端服务器程序。服务器在某个端口上创建ServerSocket并继续监听它。 Client将一些示例信息发送到此服务器。 当我运行Client第一次连接被服务器接受并且信息由服务器打印时。然后Client程序退出。当我再次运行Client时,接受连接,但不打印数据。 请检查以下代码。

服务器程序

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

客户计划

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) {
    }
}
}

请帮助我找出下次服务器没有收到数据的原因。

2 个答案:

答案 0 :(得分:1)

原因是在服务器端接收数据之前,客户端已经退出,我只是调试它。 (不能解释它第一次运作时的情况。) 只需更改下面的代码,就可以了。

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;
                }
            }
        }
    }
}

客户必须在退出前添加flush,

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

答案 1 :(得分:0)

Client循环后

将此放在for程序中:

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

您需要flush 才能推送数据并清理流。此外,您需要close 客户端套接字

我刚刚成功测试了