Delphi:通过一个套接字多线程发送数据 [英] Delphi: Send data through one socket multithreading

查看:186
本文介绍了Delphi:通过一个套接字多线程发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何在多线程中通过一个连接发送和接收数据.

Can some one give me idea how to send and receive data thru one connection in multithreading.

模型如下:

我所知道的是,如果所有三个客户端都同时发送数据,则客户端X"将接收到所有接收到的数据的合并,并且客户端X"无法分离该数据以标识来自哪个部分哪个客户.

What I know is that if all three clients are sending data at the same time, "client X" will receive a merge of all received data, and "client X" can't separate that data to identify which part is from which client.

Delphi 2010,Indy,Win7.

Delphi 2010, Indy, Win7.

对不起,如果我的英语不好,希望您能理解.

推荐答案

您需要实现一种锁定机制,例如临界区或互斥体,以防止多个线程完全同时写入套接字.

You need to implement a locking mechanism, such as a critical section or mutex, to prevent multiple threads from writing to the socket at exactly the same time.

当接收发往多个线程的数据时,您只需要在一个线程中进行读取,然后根据需要将其传递给其他线程即可.

When receiving data that is destined for multiple threads, you need to do the reading in one thread only, and have it pass on the data to the other threads as needed.

无论哪种方式,您都需要对数据进行框架化,以便接收方知道一条消息的结束位置,下一条消息的开始位置.在发送消息内容之前先发送消息的长度,或者在消息之间发送一个唯一的定界符,这些定界符将永远不会出现在消息本身中.

Either way, you need to frame your data so the receiver knows where one message ends and the next begins. Either be sending a message's length before sending the message contents, or by sending a unique delimiter in between messages that will never appear in the messages themselves.

这篇关于Delphi:通过一个套接字多线程发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆