0

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

请帮助我找出为什么下次服务器没有收到数据。

4

2 回答 2

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

客户端必须在退出前添加刷新,

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();
    }
}
}
于 2013-05-26T14:48:35.903 回答
0

循环后将其放入您的Client程序中:for

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

您需要flush您的以便向前推送数据并清理流。此外,您将需要close您的客户端套接字

我刚刚成功测试了这个。

于 2013-05-26T22:32:01.133 回答