简体   繁体   English

从Java服务器套接字在C#客户端套接字中接收数据时出错

[英]Error in receiving data in C# client socket from java server socket

I am creating a Socket connection with C# client socket and Java Server Socket. 我正在使用C#客户端套接字和Java Server套接字创建套接字连接。 When i am sending data from client socket,the server socket is properly receiving that data. 当我从客户端套接字发送数据时,服务器套接字正在正确接收该数据。 But when i am trying to send data back to Client socket from Server socket it is getting hanged on client side in receiving data. 但是,当我尝试将数据从服务器套接字发送回客户端套接字时,它在接收数据时就挂在了客户端上。

Client Side Code(In C#.net) 客户端代码(在C#.net中)

           clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            string hostName = System.Net.Dns.GetHostName();
            System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(hostName);
            System.Net.IPAddress[] ipAddresses = hostEntry.AddressList;
            System.Net.IPEndPoint remoteEP =
                new System.Net.IPEndPoint(ipAddresses[ipAddresses.Length - 1], port);
            clientSocket.Connect(remoteEP);
             string sendData = inputFilePath;
                    byte[] byteDataSend = System.Text.Encoding.ASCII.GetBytes(sendData);
                    clientSocket.Send(byteDataSend);

                    int receivedBufferSize = clientSocket.ReceiveBufferSize;
                    byte[] recivedData = new Byte[receivedBufferSize];
                    int receivedDataLength = clientSocket.Receive(recivedData);
                    string stringData = Encoding.ASCII.GetString(recivedData, 0, receivedDataLength);
                    textFilePath = stringData;
                    Console.Write(stringData);
                    clientSocket.Close();

Server Socket Code (In Java) 服务器套接字代码(在Java中)

           Socket connection = server.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
            fileName = in.readLine();
            convertedFile =runConverter.convertDocumet(fileName);
            byte[] sendingData = convertedFile.getBytes("US-ASCII");
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            dos.write(sendingData, 0, sendingData.length);

Tell me what is problem?? 告诉我是什么问题?? Please help... 请帮忙...

The usual problem with that kind of c# code is the synchronous receive. 这种C#代码的常见问题是同步接收。
I always recommend doing an asynchronous read, as in this answer . 我总是建议进行异步读取,如该答案所示

I'm not positive that that's the source of your problem, but if you implemented the asynchronous receive with a bit of logging there's a good chance that that will either fix your problem or make it much more obvious as to what your problem is. 我不太肯定这是问题的根源,但是如果您通过记录一些日志来实现异步接收,则很有可能解决问题或使问题更明显。

A hang on synchronous receive does suggest that the Java isn't sending data to the same socket that the c# is listening on, so double-checking those endpoints can also be a good idea. 同步接收挂起确实表明Java没有将数据发送到c#正在侦听的同一套接字,因此仔细检查这些端点也可能是一个好主意。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM