在客户端上创建多个套接字 [英] Creation of multiple sockets on a client

查看:55
本文介绍了在客户端上创建多个套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个客户端需要连接到多个服务器的情况.根据这里回答的问题:从单个客户端套接字连接到多个服务器 C

I have a situation where a client needs to be connected to multiple servers. According to the question answered here : Connecting to multiple servers from a single client socket C

我需要创建一个套接字来连接到每个服务器.我想问的是:除了重复这段代码来创建多个套接字之外,还有没有更有效的方法来解决这个问题?非常感谢!

I would need to create a single socket to connect to every server. What I'm trying to ask is: is there a more efficient way to go about this apart from repeating this piece of code to create multiple sockets? many thanks!

//Create a socket for the client
//If sockfd<0 there was an error in the creation of the socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
    perror("Problem in creating the socket");
    exit(2);
}

//Creation of the socket
memset(&client1addr, 0, sizeof(client1addr));
client1addr.sin_family = AF_INET;
client1addr.sin_addr.s_addr= inet_addr(argv[1]);
client1addr.sin_port =  htons(SERV_PORT); //convert to big-endian order

//Connection of the client to the socket
if (connect(sockfd, (struct sockaddr *) &client1addr, sizeof(client1addr))<0) {
    perror("Problem in connecting to the server");
    exit(3);
}

推荐答案

尝试为不同的连接重用同一个套接字的基本问题是套接字连接必须不仅存储连接的本地部分(主机和端口号)) 以及远程的(远程主机和远程端口) 此信息的目的是在某些数据包到达主机时到达正确的套接字,必须仅根据数据包内容选择正确的套接字,而别无其他.因此,仅使用一个套接字就无法尝试将同一个套接字连接到不同的远程位置.通常这里的问题是你认为套接字是一种理想的东西,它可能是我们的需求所决定的,而不是必须遵守协议规则的东西.一个连接的套接字必须定义这四个数量,并且一旦分配(这意味着一旦连接)你就不能让它重叠更多的连接.

The basic problem in trying to reuse the same socket for different connections is that a socket connection must store not only the local part of the connection (host and port numbers) but also the remote ones(remote host and remote port) The purpose of this info is to reach the proper socket when some data packet arrives to a host, is must select the right socket based only on the packet contents and nothing else. So trying to connect the same socket to different remote places is not possible with only one socket. Normally the problem here is that you think on a socket as something ideal that could be how our needs dictate instead of something that must obey protocol rules. A connected socket must define this four quantities and once assigned (this meaning once connected) you cannot make it overlap more connections.

顺便说一下,套接字的创建已经过优化,因此获得新的套接字在资源方面很便宜.

By the way, socket creation is something that has been optimized so getting a new socket is cheap in terms of resources.

如果即使这个解决方案也负担不起,您有机会切换到无连接协议(如 udp),在该协议中,只要您将套接字绑定到主机地址和客户端端口,就可以立即接收数据包,因此它可以接收任何定向到该 IP 地址和端口的数据包.这样,您可以仅使用一个套接字来管理来自不同来源的数据(例如,这已在许多 p2p 协议中使用),但您必须自己处理传输数据包中的丢弃、重复和更改.

If even this solution is not affordable, you have the chance of switching to a connectionless protocol (like udp) where a socket is ready to receive packets as soon as you have bound it to a host address and client port, so it can receive any packet directed to that ip address and port. This way, you can manage data arriving from different sources with only a socket (for example, this has been used in many p2p protocols) but you'll have to cope with dropped, repeated and changed in transit packets yourself.

这篇关于在客户端上创建多个套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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