0

I am working on a Java-C# socket communication and I would like to send the coordinates of a C# object to java periodically. The problem is that the java client Stream only reads the coordinates (20 mile long buffer) when I close the connection. I would like the connection to remain open and the coordinates to update without having to open and close this connection all the time. P.S. This was working but I somehow deleted the previous C# script I was using now I cannot figure it out.

The Java LocationRequester, will connect to the server and then periodically call getline() and pass it to coordinates. The connect part works and getline() only completes if I close the connection, otherwise it hangs. When I close the connection I get a super long row of coordinates.

public Socket clientSocket;
BufferedReader inputBuff;
String hostName;
int hostPort;

public LocationListener(String host, int port) {
    hostName = host;
    hostPort = port;
}

public void connect()
{           
    try {
         clientSocket = new Socket(hostName, hostPort);
         System.out.println("Connected to"+clientSocket.toString());
        InputStream input = clientSocket.getInputStream();
        InputStreamReader reader = new InputStreamReader(input);
        inputBuff = new BufferedReader(reader);
        String str;
    }
     catch(IOException e)
    {
        e.printStackTrace();
    }
}

public String getLine() {           
    String rstring = "";
    try {
        rstring = inputBuff.readLine();
        System.out.println(rstring);
    }
    catch(IOException e) {
         e.printStackTrace();
    }
    return rstring;
}

C# code seems to be where the problem is.

    private void Start()
    {
        IPAddress address = IPAddress.Any;
        server = new TcpListener(address, 9999);
        server.Start();

        client = server.AcceptTcpClient();
        StartCoroutine(SendCords());
    }

    private IEnumerator SendCords()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.5f);
            NetworkStream stream = client.GetStream();
            byte[] msg = System.Text.Encoding.ASCII.GetBytes(transform.position.ToString());
            stream.Write(msg, 0, msg.Length);
            stream.Flush();
            // client.Close();
            Debug.Log("Sending "+transform.position);
            
        }
    }

2 Answers 2

1

The java code is reading a line. That means it will block until it gets a line feed character '\n'. And I guess your C# code is not adding a line feed. In my opinion, if you add a line feed character in the end, to your C# message payload, the java code should get the information and come out of the wait. Give a try.

Sign up to request clarification or add additional context in comments.

Comments

1

@ferosekhanj has already said very well. I add that the function ofBufferedReader.readLine() will stop reading at'\n' and also at EOF. This is why when your C# program close the Socket, and your java program will receive a super long row of coordinates.

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.