TCPClient - 发送&收到回复

时间:2014-11-03 20:35:05

标签: java c# tcp tcpclient tcplistener

我连接到接收字符串的远程TCP侦听器,并以响应进行响应。

从我的Windows 8手机应用程序转到Java Jar。 Jar IS 收到邮件,但Windows 8 Phone App没有收到回复。

C#代码

outputClient.Connect (/IP ADDRESS/, /Port/);

        using (Socket sock = outputClient.Client) {
            sock.Send (UTF8Encoding.ASCII.GetBytes (broadcastMessage));

            var response = new byte[100];

            sock.Receive (response);
            var str = Encoding.ASCII.GetString (response).Replace ("\0", "");
            Console.WriteLine ("[RECV] {0}", str);
        } <-- JAVA CODE DOESN'T GET HIT UNTIL THIS LINE IS COMPLETED

Java代码

String clientSentence;
        ServerSocket socketServer = new ServerSocket(/* PORT */);

        while (true)
        {
            Socket connectionSocket = socketServer.accept();
            connectionSocket.setKeepAlive(true);

            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            clientSentence = inFromClient.readLine();

            BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(connectionSocket.getOutputStream()));

            if (clientSentence != null)
            {
                try
                {
                    JsonObject json = new JsonParser().parse(clientSentence).getAsJsonObject();
                    String un = json.get("Username").toString();
                    String uuid = "2c9c79a096ef4d869fb1d1e07469bb41".replaceAll(                                            
                            "(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",                            
                            "$1-$2-$3-$4-$5"); 
                    var val = /* Get val */

                    String response = gson.toJson(val);
                    outToClient.write(response);
                   outToClient.newLine();
                   outToClient.flush();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                    outToClient.write(response);
                    outToClient.newLine();
                    outToClient.flush();
                }
            }

            connectionSocket.close();
        }

更多解释: JAVA CODE DOESN&#39; TET HIT直到此行已完成表示套接字在使用之前似乎没有发送(Socket sock = outputClient.Client )不再使用。

1 个答案:

答案 0 :(得分:0)

我通过用以下代码替换C#代码来修复它:

using (TcpClient client = new TcpClient (/IP ADDRESS/, /PORT/))
        using (NetworkStream stream = client.GetStream ())
        using (StreamReader reader = new StreamReader (stream))
        using (StreamWriter writer = new StreamWriter (stream)) {
            writer.AutoFlush = true;

            foreach (string lineToSend in linesToSend) {
                Console.WriteLine ("Sending to server: {0}", lineToSend);
                writer.WriteLine (lineToSend);
                string lineWeRead = reader.ReadLine ();
                Console.WriteLine ("Received from server: {0}", lineWeRead);
                Thread.Sleep (2000); // just for effect
            }
            Console.WriteLine ("Client is disconnecting from server");
        }