我的Java客户端不适用于我的python服务器

时间:2019-06-05 01:11:58

标签: java python sockets server buffer

我正在构建一个Java客户端和一个python服务器,当我用它们匹配的语言客户端/服务器运行它们时,它们都可以正常工作,但是当我在python服务器上运行java客户端时,套接字会连接,但是java终端会每当我尝试读取python服务器发送的数据时都会卡住

这可能是一个缓冲问题,因为每当我使用从python接收到的数据调用缓冲区时,终端就只会卡在上述行中,而不会让步。

这是python服务器:

import socket
import pyautogui
def main():
    HOST = ""              # Endereco IP do Servidor
    PORT = 1234            # Porta que o Servidor esta
    tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    orig = (HOST, PORT)
    tcp.bind(orig)
    tcp.listen(1)
    while True:
        con, cliente = tcp.accept()
        print ('Concetado por', cliente)
        cone="Conectado".encode()
        con.send(cone)
        while True:
            msg = con.recv(1024).decode()
            if msg[0]=="/" :
                cmsg=''
                for i in range(1,len(msg)):
                    cmsg+=msg[i]
                exec(cmsg)
            if not msg: break
            print (cliente, msg)
        print ("Finalizando conexao do cliente", cliente)
        con.close()
main()

这是Java:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class MyClient {
    public static void main(String[] args) {

        try {
            Socket soc = new Socket("localhost", 1234);
            DataOutputStream dout = new DataOutputStream(soc.getOutputStream());
            OutputStreamWriter osw = new OutputStreamWriter(dout);
            BufferedWriter bw = new BufferedWriter(osw);
            InputStream is = soc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Here");
            System.out.println(br.readLine());
            System.out.println("Here2");
            Scanner scanner = new Scanner(System.in);
            while (!(bw.readLine().equals("exit"))) {
                System.out.println(br.readLine());
                String input = scanner.nextLine();
                bw.write(input);
                bw.flush();
            }
            dout.close();
            soc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

所以我实际上发现了问题所在,问题出在缓冲区,它显然不了解行已结束,因此要解决该问题,我只需要发送 $('#exampleModalCenterTitle').text('The replaced text.');