Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用套接字时json文件未完成_Java_Python_Sockets - Fatal编程技术网

Java 使用套接字时json文件未完成

Java 使用套接字时json文件未完成,java,python,sockets,Java,Python,Sockets,我有一个视频流。每一帧我都需要发送一个包含该帧数据的小json文件,速度至关重要。最好的方法是什么 我的服务器是这样的。等待json文件,然后必须将该json文件发送到python应用程序 public class ServerClass { public static void main(String[] args) { Marcoserver mimarco=new Marcoserver(); } } clas

我有一个视频流。每一帧我都需要发送一个包含该帧数据的小json文件,速度至关重要。最好的方法是什么

我的服务器是这样的。等待json文件,然后必须将该json文件发送到python应用程序

public class ServerClass  {

    public static void main(String[] args) {
        Marcoserver mimarco=new Marcoserver();                  
    }   
}

class Marcoserver implements Runnable {

    public Marcoserver(){               
        Thread miHilo = new Thread(this);       
        miHilo.start();     
    }   

    public void run() {
        try {
            ServerSocket server = new ServerSocket(7777);

            while (true) {
                Socket miSocket = server.accept(); 

                BufferedReader entrada = new BufferedReader(new InputStreamReader(miSocket.getInputStream(), "UTF8"));
                String mensaje = entrada.readLine();

                JSONObject obj;
                obj = new JSONObject(mensaje);
                System.out.println(obj);

                ConectorSocket cntor = new ConectorSocket("localhost", 6363);   
                cntor.Conectar();
                cntor.Send(mensaje);
                cntor.Close();

                miSocket.close();
            }   

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

}


public class ConectorSocket{

        private String host;
        private int port;
        Socket sockSend;

        public ConnectClass(String hst, int prt ) {
            this.host = hst;
            this.port = prt;
        }

        public void Conectar() {
            this.sockSend = new Socket(this.host, this.port);
        }
        public void Send(String mensaje) {
            DataOutputStream flujo_salida = new DataOutputStream(sockSend.getOutputStream());
            flujo_salida.writeBytes(mensaje);
            flujo_salida.close();
        }
        public boolean Close() {
            this.sockSend.close();
        }

}
这是简化的python应用程序:

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 6666))
serversocket.listen()

while True:
    connection, address = serversocket.accept()
    buf = connection.recv(2048)
    if len(buf) > 0:
        print(buf.decode())
我的问题是python应用程序打印不完整的信息,例如:

{"keyword"
{"keyword": [[14, 1, -1]]}
{"keyword": [[14, 
而不是:

{"keyword":[]}
{"keyword":[[14,1,-1]]}
{"keyword":[[14,2,-1]]}

Java代码没有问题,但Python代码不好:

while True:
    connection, address = serversocket.accept()
    buf = connection.recv(2048)
    if len(buf) > 0:
        print(buf.decode())
这将只打印每个连接的第一个接收到的TCP数据包

您需要继续调用
recv
,直到它返回
0

while True:
    connection, address = serversocket.accept()
    msg = []
    while True:
        buf = connection.recv(65536)
        if (len(buf) == 0):
            break
        msg.append(buf)
    print(''.join(msg))
    connection.close()

您还需要关闭每个连接。

这是我完整的codeConectorSocket==conctorClass?是的!很抱歉,如果连接是本地的,并且数据量很大,那么在列表中收集块,然后加入(…)会更快。在这种情况下,2048也过于保守了。@9000我相信在现阶段,性能不是OP的问题。但是好的,更新了。