如何知道何时从客户端服务器 c++ 发送/接收字节
我的问题是,当您有代理服务器并且需要与客户端发送/接收并与远程服务器发送/接收时,您如何知道在哪一端有数据要发送/接收,以便我可以调用适当的函数。 我需要从网站到客户端(通过代理)以及从客户端到服务器(通过代理)接收/发送字节,但我不知道它们以什么顺序出现,我发现大多数网站都不同。< br> 我当前的实现是这样的:
1) receive from client
2) send to server
//infinite loop here
3) receive from server
4) send to client
// until bytes from server is 0
这只适用于一些站点,并且不能完全加载它们,只有 15-20 KB。
有什么建议吗?
My question is when you have a proxy server and you need to send/recv with client and send/recv with remote server how do you know at what end there is data to be send/recv so I can call the appropriate functions.
I need to recv/send bytes from a website to a client(via proxy) and from client to server (via proxy), but I don't know in what order they are coming,I saw that is different for most sites.
My current implementation is this:
1) receive from client
2) send to server
//infinite loop here
3) receive from server
4) send to client
// until bytes from server is 0
This just works for a few sites,and doesn't load them completely, only 15-20 KB.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的任务是将数据从客户端转发到服务器并返回。由于客户端和服务器可以同时传输数据,因此当您从客户端读取所有内容而不是将其传递到服务器时,反之亦然的方法将不起作用:考虑当您等待客户端开始传输,并且客户端在开始传输之前想要从服务器获取数据时的情况自己的传输。
因此,有以下方法可以使其工作:
select(2)
功能(或来自Windows API的类似系统调用)+非阻塞套接字。这将告诉您何时有数据需要读取或写入。如果您希望每个线程为多个客户端提供服务,则需要非阻塞套接字,这样线程就不会在读/写系统调用中被阻塞。手册页中有
select
示例,互联网上有很多信息。这里是一个致力于服务器开发的著名页面。
Your task will be forwarding data from client to server and back. Since client and server can transmit data simultaneously, approach when you read everything from client than pass it to server and vice versa won't work: consider situation when you waiting for client to start transmission, and client wants to data from server before starting its own transmission.
So, there are following ways to make it work:
select(2)
functionality (or similar sys call from Windows API) + nonblocking sockets. This will tell you when there's data to be read or written. Non-blocking sockets are needed if you want to serve several clients per one thread, so thread won't become blocked in read/write syscall.There's
select
sample in man page and lots of info on internet.One famous page dedicated to servers development is here.
从客户那里收到
发送到服务器
而(真)
{
if(recvfrom 服务器不为零)
发送给客户
if(来自客户端的recv不为零)
发送到服务器
我认为
这可能有用..
receive from client
send to server
while(true)
{
if(recvfrom server is not zero)
send to client
if(recvfrom client is not zero)
send to server
}
I think this might work..
使用
select()
等待任意一侧的数据可用,然后读取并传递它。Use
select()
to wait for data to be available from either side, then read it and pass it over.