Incomplete messages (C# TCP/IP Client)

前端 未结 3 1988
独厮守ぢ
独厮守ぢ 2020-12-20 00:05

First of all, I\'m absolutely not a network programmer. What I try to do, is a very simple TCP/IP communication between a Java server and a C# client.

Java server:

相关标签:
3条回答
  • 2020-12-20 00:10

    TCP is a stream-oriented connection, not message-oriented. It has no concept of a message. When you write out your serialized string, it only sees a meaningless sequence of bytes. TCP is free to break up that stream up into multiple fragments and they will be received at the client in those fragment-sized chunks. It is up to you to reconstruct the entire message on the other end.

    In your scenario, one would typically send a message length prefix. This way, the client first reads the length prefix so it can then know how large the incoming message is supposed to be.

    I would seriously consider using something like Google's Protocol Buffers as a good way of declaring your messages and then streaming them with the size prefix option. The nice thing is that you define your set of messages once and then use the available tools to automatically generate C++, Java, C#, etc code from the message definitions. This will help in having a consistent messaging set that works between languages.

    0 讨论(0)
  • 2020-12-20 00:15

    A message (any data, I mean), when sent through a socket, is divided into several packets. When printing each received packet, you don't see your whole message.

    You should define an end of message string (something like ".#."). Until you receive this sequence, you keep concatenating the messages you receive.

    This is what session protocols (that is, protocols that run on the top of TCP) do.

    Hope this helps.

    Regards, Calil

    0 讨论(0)
  • 2020-12-20 00:28

    Take a look at this example...

    Java TCP Server...

    import java.net.*;
    import java.io.*;
    
    public class TcpServer
    {
        public static void main(String h[])
        {
            try
            {
                ServerSocket serverSocket = new ServerSocket(1100);
                Socket socket = serverSocket.accept();
                System.out.println("Client Accepted");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println("Received: " + bufferedReader.readLine());
                PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                printWriter.println("Hello Theo. Welcome to socket programming.");
            } catch (Exception e)
            {
                System.out.println(e);
            }
        }
    }
    

    C# TCP Client...

    using System;
    using System.IO;
    using System.Net.Sockets;
    
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var client = new TcpClient("localhost", 1100);
                var stream = client.GetStream();
                var streamWriter = new StreamWriter(stream);
                streamWriter.WriteLine("My name is Theo");
                streamWriter.Flush();
                var streamReader = new StreamReader(stream);
                Console.WriteLine("Received: " + streamReader.ReadLine());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
    
            Console.WriteLine("Press a key to continue.");
            Console.ReadKey();
        }
    }
    
    0 讨论(0)
提交回复
热议问题