开发者社区> 问答> 正文

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

我正在创建一个程序来通过套接字下棋。我的客户端由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);
            }

展开
收起
几许相思几点泪 2019-12-23 17:40:33 622 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载