简体   繁体   English

Java Tcp Server和.Net Tcp Client发送和接收问题

[英]Java Tcp Server and .Net Tcp Client Problems with sending and receiving

i plan to write a Java TCP Server and a Client in C# .NET. 我计划在C#.NET中编写Java TCP Server和客户端。 I take Java for the Server because i have the ability to run the Server on Linux. 我将Java用作服务器,因为我能够在Linux上运行服务器。 My Problem is that the .NET Client can Connect to the Server but if i send something to the Server, the Server doesn´t receive anything. 我的问题是.NET客户端可以连接到服务器,但是如果我将某些内容发送到服务器,则服务器什么都收不到。

Here is my Client: 这是我的客户:

    static void Main(string[] args)
    {
        try
        {
            System.Net.Sockets.TcpClient tcpclnt = new System.Net.Sockets.TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("192.168.178.26", 1337);

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);
            stm.Flush();

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            tcpclnt.Close();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }

And here is my Server: 这是我的服务器:

public static void main(String argv[]) throws Exception
{
    String clientSentence;
    String capitalizedSentence;

    ServerSocket socket = new ServerSocket(1337);

    while(true)
    {
        System.out.println("... Server gestartet");
        Socket connectionSocket = socket.accept();
        BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        System.out.println(connectionSocket.getRemoteSocketAddress());
        DataOutputStream outputStream = new DataOutputStream(connectionSocket.getOutputStream());
        clientSentence = inputFromClient.readLine();
        System.out.println("Received: " + clientSentence);
        capitalizedSentence = clientSentence.toUpperCase() + "\n";
        outputStream.writeBytes(capitalizedSentence);
    }
}

The code i tested is only a Prototyp for Testing the connection to the Java Server. 我测试的代码只是用于测试与Java Server的连接的原型。 I also plan to communicate with the Java Tcp Server with iOS and maybe Windows Phone. 我还计划与带有iOS的Java Tcp Server和Windows Phone进行通信。 So i hope anyone of you have an answer for me. 所以我希望你们中的每个人都能为我解答。

Your client sends the bytes of a line, and waits for a response, keeping the stream open. 您的客户端发送一行字节,然后等待响应,从而保持流打开。

Your server waits for a line break (or the end of the stream, ie the connection being closed) before ReadLine returns. 您的服务器在ReadLine返回之前等待换 (或流的末尾,即连接被关闭)。 So both sides are waiting for the other. 因此,双方都在等待对方。

I would suggest that you use an OutputStreamWriter wrapped around the stream on the client side - then you can use WriteLine very simply (rather than messing around with the encoding yourself). 我建议您使用在客户端的流周围包裹的OutputStreamWriter然后可以非常简单地使用WriteLine (而不是自己弄乱编码)。 When the server sees the line break, it will respond. 当服务器看到换行符时,它将响应。 I'd also use an InputStreamReader on the client rather than calling Convert.ToChar on each byte. 我还将在客户端上使用InputStreamReader ,而不是在每个字节上调用Convert.ToChar Fundamentally, if you're only interested in transferring text and you're happy to use "a line" as the unit of messaging, using a writer/reader pair on both sides is the simplest approach. 从根本上讲,如果您仅对传输文本感兴趣,并且很乐意使用“一行”作为消息传递单位,则在两边同时使用写入器/读取器对是最简单的方法。

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

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