简体   繁体   English

有关套接字通信的问题

[英]Problem about socket communication

I have two separate socket projects in VS.NET. 我在VS.NET中有两个单独的套接字项目。 One of them is sender, other one is receiver. 其中一个是发送者,另一个是接收者。 After starting receiver, i send data from sender. 启动接收器后,我从发送器发送数据。 Although send method returns 13 bytes as successfully transferred, the receiver receives 0 (zero). 尽管send方法成功传输后返回13个字节,但接收方接收到0(零)。 The receiver accepts sender socket and listens to it. 接收方接受发送方套接字并进行侦听。 But cannot receive data. 但是无法接收数据。 Why? 为什么?

PS : If sender code is put in receiver project, receiver can get data, as well. PS:如果将发送方代码放在接收方项目中,则接收方也可以获取数据。

I think the problem is that the send application is ending before the receiver can read the data. 我认为问题在于发送应用程序在接收者可以读取数据之前结束。 If you put a Thread.Sleep(1000); 如果放置Thread.Sleep(1000); after the Send call, the receive application will read the data (at least when I tested it). Send调用之后,接收应用程序将读取数据(至少在我测试时)。

Most likely this is a bug in your code but without seeing the code it's impossible to tell. 这很可能是代码中的错误,但是如果看不到代码,就无法分辨。

However be aware that TCP/IP has no concept of messages, only a stream of data. 但是请注意,TCP / IP没有消息的概念,只有数据流。 So it's posisble to send 13 bytes and receive 6 in one read and 7 in the next. 因此,发送13个字节并在一次读取中接收6个字节,在下一次读取中接收7个字节是可能的。 Or conversely to send 13 bytes then later send 7 more and on the receiveing side to receive them as a single block of 20 bytes. 或者相反,先发送13个字节,然后再发送7个字节,然后在接收端将其作为20个字节的单个块接收。

Thank you so much for your interest. 非常感谢您的关注。 Here is the code: 这是代码:

/*********************************** Receiver **********************/ / ****************************************接收器************* ********* /

class Program { static void Main(string[] args) { Receiver recv = new Receiver(); 类程序{static void Main(string [] args){Receiver recv = new Receiver(); recv.Start(); recv.Start();
} } }}

public class Receiver
{
    public void Start()
    {
        Socket gateSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        gateSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8999));
        gateSocket.Listen(12);

        Thread thGateListen = new Thread(new ParameterizedThreadStart(GateListener));
        thGateListen.Start(gateSocket);
    }

    public void GateListener(object obj)
    {

        Socket gateSocket = (Socket)obj;

        for (; ; )
        {
            Socket newRequest = gateSocket.Accept();
            Console.WriteLine("New Connection Request");
            Thread thReadData = new Thread(new ParameterizedThreadStart(ReadFromSocket));
            thReadData.Start(newRequest);
        }
    }


    public void ReadFromSocket(object obj)
    {
        Socket s = (Socket)obj;
        for (; ; )
        {
            if (s.Available > 0)
            {
                byte[] buffer = new byte[s.Available];
                s.Receive(buffer);
                Console.WriteLine(System.Text.Encoding.ASCII.GetString(buffer));
            }
        }
    }

}

/*********************************** Sender **********************/ / ****************************************发件人************* ********* /

class Program { static void Main(string[] args) { Sender s = new Sender(); 类Program {静态void Main(string [] args){Sender s = new Sender(); s.Send("Hello Socket!"); s.Send(“ Hello Socket!”); } } }}

class Sender
{
    public void Send(string s)
    {
        Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sendSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8999));

        sendSocket.Send(System.Text.Encoding.ASCII.GetBytes(s));
    }

}

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

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