Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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
使用相同的网络流和套接字连接从C#客户端向Java服务器发送多字节[]时挂起_C#_Java_Tcp_Bytearray_Networkstream - Fatal编程技术网

使用相同的网络流和套接字连接从C#客户端向Java服务器发送多字节[]时挂起

使用相同的网络流和套接字连接从C#客户端向Java服务器发送多字节[]时挂起,c#,java,tcp,bytearray,networkstream,C#,Java,Tcp,Bytearray,Networkstream,我编写了一个文件上传程序,用于将程序代码文件从C#client发送到Java服务器。服务器要求在接受文件上载之前提交有效的用户名和密码 安全性(用户名和密码)和文件上传程序都可以独立正常工作。但是,当我尝试将这两种方法结合起来时,在从服务器收到“true”布尔响应(指示正确的用户名和密码)后,代码在C#client上冻结。随附来自客户端和服务器的相关代码 客户 Java服务器 public void run() { try { // Get username and password

我编写了一个文件上传程序,用于将程序代码文件从C#client发送到Java服务器。服务器要求在接受文件上载之前提交有效的用户名和密码

安全性(用户名和密码)和文件上传程序都可以独立正常工作。但是,当我尝试将这两种方法结合起来时,在从服务器收到“true”布尔响应(指示正确的用户名和密码)后,代码在C#client上冻结。随附来自客户端和服务器的相关代码

客户

Java服务器

public void run() {

try {

  // Get username and password
    byte[] byteArrayJAR = new byte[filesize];
    byte[] byteArraySecurity = new byte[filesize];
    InputStream input = socket.getInputStream();
    PrintWriter output = new PrintWriter(socket.getOutputStream());

    int bytesSecurity = input.read(byteArraySecurity, 0, byteArraySecurity.length);
    int currentByte1 = bytesSecurity;

    if (input.available() > 0) {

        do {

            bytesSecurity = input.read(
                    byteArraySecurity,
                    currentByte1,
                    (byteArraySecurity.length - currentByte1));

            if (bytesSecurity >= 0) {

                currentByte1 += bytesSecurity;

            }
        }

        while (bytesSecurity > -1);
    }

    String securityString = new String(byteArraySecurity).trim();
    String[] authenticationString = securityString.split("_");
    String username = authenticationString[0];
    String password = authenticationString[1];

  // Validate the username and password with a stored database
    if (security.validateUser(username, password)) {

       // Inforn the client their username and password were accepted
        output.println(true);
        output.flush();

      // Get the program code file
        int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);
        int currentByte = bytesRead;

       if (input.available() > 0) {

                do {

                   bytesRead = input.read(
                     byteArrayJAR,
                            currentByte,
                            (byteArrayJAR.length - currentByte));

                if (bytesRead >= 0) {

                        currentByte += bytesRead;

                    }
             }

                 while (bytesRead > -1);

     }

      // Inform the client that the code was received
        output.println("Success");
        output.flush();

    }

    else {

      // Inform the client their username or password was incorrect
        output.println(false);
        output.flush();

    }

  // Disconnect from client
    output.flush();
    output.close();
    input.close();
    socket.close();

}

catch (IOException e) {

    e.printStackTrace();

}
}

有人能看到上面的错误吗?这可能会导致我的代码挂起?C#client在这两个实例上传输的数据肯定是有的。在C#中,两字节数组不能从同一网络流发送有什么原因吗

我非常感谢任何人能提供的任何帮助,让上述代码在不挂起的情况下工作

问候,


Midavi。

您正在使用input.ReadToEnd()两次。一条小溪只有一端;我怀疑其中的第一个已经不合适了,应该用更直接的阅读来代替——或者最好是一个ReadLine()


如果此处的对话依赖于客户端和服务器之间的多条消息,您可能还需要检查nagle是否已启用,因为其中一条消息可能仍处于缓冲状态且尚未实际发送(Flush()在网络流上不执行任何操作)-您可能必须为套接字设置节点延迟(或等效项)。默认行为是缓冲小消息以防止网络中出现大量的小数据包。

在C代码中,请考虑使用“使用”。这些对象中的大多数都是可识别的,如果发生意外情况,大多数将无法正确处理。谢谢您的建议。我会给你一些提示,然后再报告。很好。问题是第一个输入。ReadToEnd()。我把它改为input.ReadLine();现在一切都很好。事实上,我不需要和唠叨的人混在一起。谢谢你的帮助。
public void run() {

try {

  // Get username and password
    byte[] byteArrayJAR = new byte[filesize];
    byte[] byteArraySecurity = new byte[filesize];
    InputStream input = socket.getInputStream();
    PrintWriter output = new PrintWriter(socket.getOutputStream());

    int bytesSecurity = input.read(byteArraySecurity, 0, byteArraySecurity.length);
    int currentByte1 = bytesSecurity;

    if (input.available() > 0) {

        do {

            bytesSecurity = input.read(
                    byteArraySecurity,
                    currentByte1,
                    (byteArraySecurity.length - currentByte1));

            if (bytesSecurity >= 0) {

                currentByte1 += bytesSecurity;

            }
        }

        while (bytesSecurity > -1);
    }

    String securityString = new String(byteArraySecurity).trim();
    String[] authenticationString = securityString.split("_");
    String username = authenticationString[0];
    String password = authenticationString[1];

  // Validate the username and password with a stored database
    if (security.validateUser(username, password)) {

       // Inforn the client their username and password were accepted
        output.println(true);
        output.flush();

      // Get the program code file
        int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);
        int currentByte = bytesRead;

       if (input.available() > 0) {

                do {

                   bytesRead = input.read(
                     byteArrayJAR,
                            currentByte,
                            (byteArrayJAR.length - currentByte));

                if (bytesRead >= 0) {

                        currentByte += bytesRead;

                    }
             }

                 while (bytesRead > -1);

     }

      // Inform the client that the code was received
        output.println("Success");
        output.flush();

    }

    else {

      // Inform the client their username or password was incorrect
        output.println(false);
        output.flush();

    }

  // Disconnect from client
    output.flush();
    output.close();
    input.close();
    socket.close();

}

catch (IOException e) {

    e.printStackTrace();

}