繁体   English   中英

为什么我重新运行程序时,我的客户端没有通过 sockets 向服务器发送数据?

[英]Why isn't my Client sending data to server through sockets when I re-run the program?

我正在使用 sockets 将消息从 python 程序发送到 Java 程序。 当我最初运行程序时,服务器接收到消息,但是当我重新运行客户端(不重新运行服务器)时,没有发送任何消息,服务器接收到 null。 请帮忙?

python 客户端:

import socket

HOST = "localhost"
PORT = 8080

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.connect((HOST, PORT))

#sock.sendall(b'\x00\x0dHello, world!\n')
sock.send(bytes("welcome hi\n","utf8"))

data = sock.recv(1024)
print(data.decode("utf-8"))


if data == (bytes("olleH\n","utf-8")):
    sock.send(bytes("Bye\n","utf8"))
    data = sock.recv(1024)
    print(data.decode("utf-8"))
   }
  }
}

Java服务器


import java.net.*;
import java.util.ArrayList;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Javaserver {
    private static ArrayList<ClientHandler> clients = new ArrayList<>();
    private static ExecutorService pool = Executors.newFixedThreadPool(2);
    private static final int PORT = 8080;
    private ServerSocket server= null;





    public static void main(String args[]) throws Exception {

        System.out.println("wait for connection on port 8080");
        ServerSocket server = new ServerSocket(PORT);
        Socket client = server.accept();

        while (true) {




            System.out.println("got connection on port 8080");
            ClientHandler clientThread = new ClientHandler(client);
            clients.add(clientThread);
            pool.execute(clientThread);

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import java.net.Socket;

public class ClientHandler implements Runnable {
    private Socket client;
    private BufferedReader in;
    private PrintWriter out;
    private String fromClient;
    private String toClient;
    private int id;






    public ClientHandler(Socket client) throws IOException {
        this.client = client;

        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(), true);


    }

    @Override
    public void run() {
        boolean run = true;
        try {
            while (run) {




                fromClient = in.readLine();
                System.out.println("received: " + fromClient );


                if (fromClient.equals("welcome hi")) {
                    toClient = "olleH";
                    System.out.println("send olleH");
                    out.println(toClient);
                    fromClient = in.readLine();
                    System.out.println("received: " + fromClient);

                }
                if(fromClient.equals("ho")){
                    toClient = "olleH";
                    System.out.println("send olleH");
                    out.println(toClient);
                    fromClient = in.readLine();
                    System.out.println("received: " + fromClient);


                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

要监听新的套接字连接,您必须使用:

ServerSocket server = new ServerSocket(PORT);
Socket client = server.accept();

您在任何类型的循环之外的主要方法上使用它,因此如果客户端关闭连接,服务器将不会监听新连接,除非您再次运行该部分代码(因此应该重新运行服务器)。

如果您不想重新运行服务器,您可以创建一个侦听新套接字的方法,然后在客户端关闭当前套接字连接时调用它。

public Socket waitForClient(ServerSocket server) {
    return server.accept();
}

要检查套接字是否已关闭,您可以使用read() == -1readLine == null如果客户端已使用 close() 方法关闭了套接字。 如果没有,您还可以通过捕获IOException来确定它是否已关闭。 有关更多信息,请参阅此帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM