Python Socket仅在关闭套接字时获取信息

时间:2019-12-23 09:14:11

标签: java python python-3.x sockets

我正在创建一个程序来通过套接字下棋。我的客户端是用Python编写的,它使用套接字将数据发送到服务器。我仅在客户端程序关闭时才收到信息。下面提到的是客户端代码。我正在使用python套接字https://docs.python.org/3/library/socket.html

def youSecond(board):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('192.168.11.46', 9999))
        run = True
        turn = 1
        new_msg = True
        while run:
                renderMap(board)
                move = s.recv(1024).decode("utf-8")
                if new_msg:
                        new_msg = False
                print("SERVER: ", move)
                players[0].play(board, move)
                new_msg = True
                turn +=1
                renderMap(board)
                print("Black machine is thinking.....")
                myTurn = players[1].play(board, turn).encode("utf-8")
                s.send(myTurn)
                turn += 1

和我的使用Java的服务器

  public class ClientHandler implements Runnable {

        BufferedReader reader;
        Socket sock;
        PrintWriter client;

        public ClientHandler(Socket clientSocket, PrintWriter user) {
            client = user;
            try {
                sock = clientSocket;
                InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
                reader = new BufferedReader(isReader);
                System.out.println("tren helllo");

            } catch (Exception ex) {
                ta_chat.append("Unexpected error... \n");
            }

        }

        @Override
        public void run() {
            String message, connect = "Connect", disconnect = "Disconnect", chat = "Chat";
            String[] data;

            try {
                while ((message = reader.readLine()) != null) {
                    System.out.println("duoi helllo");
                    ta_chat.append("Received: " + message + "\n");
                    data = message.split(":");

                    for (String token : data) {
                        ta_chat.append(token + "\n");
                    }

                    if (data[2].equals(connect)) {
                        tellEveryone((data[0] + ":" + data[1] + ":" + chat));
                        userAdd(data[0]);
                    } else if (data[2].equals(disconnect)) {
                        tellEveryone((data[0] + ":has disconnected." + ":" + chat));
                        userRemove(data[0]);
                    } else if (data[2].equals(chat)) {
                        tellEveryone(message);

                        try {
                            FileWriter fw = new FileWriter("C:\\Users\\Admin\\Desktop\\FixCoTuong\\moves.txt");
                            fw.write(data[1]);
                            fw.close();
                        } catch (Exception e) {
                            System.out.println(e);
                        }
                        System.out.println("sucess");
                    } else {
                        ta_chat.append("No Conditions were met. \n");
                    }
                }
            } catch (Exception ex) {
                ta_chat.append("Lost a connection. \n");
                ex.printStackTrace();
                clientOutputStreams.remove(client);
            }

0 个答案:

没有答案
相关问题