0

I'm connecting to a remote TCP Listener that receives a string, and responds with a response.

Going from my Windows 8 Phone App, to a Java Jar. The Jar IS receiving the message, but the Windows 8 Phone App is not getting the response.

C# Code

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 Code

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();
        }

A little more explanation: JAVA CODE DOESN'T GET HIT UNTIL THIS LINE IS COMPLETED means that the socket appears to not be sending until using (Socket sock = outputClient.Client) is no longer being used.

7
  • What does this mean: "JAVA CODE DOESN'T GET HIT UNTIL THIS LINE IS COMPLETED" ? How could your C# program receive any data, if no Java code has executed at that point? And why does your question seem to otherwise indicate that the C# receive does not complete? This question needs a lot of cleaning up, so that it makes some sense. As far as the C# code posted, the one major error you've made is to not check the byte count for the receive. More to the point, if you're using TcpClient (as it appears you are), why not get the NetworkStream from it and wrap in a StreamReader, as in the Java code? Commented Nov 3, 2014 at 21:25
  • I made a quick edit to explain it further. Commented Nov 3, 2014 at 21:29
  • Sorry, that doesn't help. Unless you set the socket to non-blocking, you can't get to the closing brace of the using block until some data has been received, which can't happen until some data has been sent. It's not possible that the Java side doesn't send until reaching the closing block of the using statement. Commented Nov 3, 2014 at 21:31
  • That's how it's been--if I remove sock.Receive (response); it sends it fine, if not it just hangs. Commented Nov 3, 2014 at 21:59
  • So when the sock.Receive(response) is there, what data is received? Commented Nov 3, 2014 at 22:07

1 Answer 1

1

I fixed it by replacing the C# code with:

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");
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.