简体   繁体   English

Java C# 套接字通信

[英]Java C# socket communication

I am working on a Java-C# socket communication and I would like to send the coordinates of a C# object to java periodically.我正在处理 Java-C# 套接字通信,我想定期将 C# object 的坐标发送到 java。 The problem is that the java client Stream only reads the coordinates (20 mile long buffer) when I close the connection.问题是当我关闭连接时,java 客户端 Stream 只读取坐标(20 英里长的缓冲区)。 I would like the connection to remain open and the coordinates to update without having to open and close this connection all the time.我希望连接保持打开状态并更新坐标,而不必一直打开和关闭此连接。 PS This was working but I somehow deleted the previous C# script I was using now I cannot figure it out. PS这是有效的,但我以某种方式删除了我现在使用的以前的 C# 脚本,我无法弄清楚。

The Java LocationRequester , will connect to the server and then periodically call getline() and pass it to coordinates. Java LocationRequesterconnect到服务器,然后定期调用getline()并将其传递给坐标。 The connect part works and getline() only completes if I close the connection, otherwise it hangs.连接部分起作用,只有当我关闭连接时getline()才会完成,否则它会挂起。 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. C# 代码似乎是问题所在。

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

The java code is reading a line. java 代码正在读取一行。 That means it will block until it gets a line feed character '\n'.这意味着它将阻塞,直到它得到一个换行符'\n'。 And I guess your C# code is not adding a line feed.而且我猜您的 C# 代码没有添加换行符。 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.在我看来,如果你最后添加一个换行符,到你的 C# 消息有效负载中,java 代码应该得到信息并退出等待。 Give a try.试一下。

@ferosekhanj has already said very well. @ferosekhanj 已经说得很好。 I add that the function of BufferedReader.readLine() will stop reading at'\n' and also at EOF.我补充说BufferedReader.readLine()的 function 将在'\n'和 EOF 处停止读取。 This is why when your C# program close the Socket, and your java program will receive a super long row of coordinates.这就是为什么当你的 C# 程序关闭 Socket 时,你的 java 程序会收到一排超长的坐标。

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

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