Opi*_*eus 7 c++ sockets networking select
我正在尝试允许多个客户端使用select连接到主机.我是否必须连接每一个,告诉他们移动到另一个端口,然后重新连接新端口?或者会选择允许我将多个客户端连接到同一个端口?
这是客户端代码:
int rv;
int sockfd, numbytes;
if ((rv = getaddrinfo(hostName, hostPort, &hints, &servinfo)) != 0) {
cout << "Could not get server address.\n";
exit(1);
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("Client: no socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("Client: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "Unable to connect to server.\n");
exit(2);
}
FD_SET(sockfd, &masterSet);
Run Code Online (Sandbox Code Playgroud)
这是服务器代码:
int rv = getaddrinfo(NULL, port, &hints, &res);
int yes = 1;//Not sure what this is for, found it in Beej's
if(rv != 0){
cout<< "Error, nothing matches criteria for file descriptor.\n";
exit(1);
}
int fdInit;
for(temp = res; temp != NULL; temp = temp->ai_next){
if((fdInit = socket(temp->ai_family, temp->ai_socktype, temp->ai_protocol)) == -1){
cout << "This is not the fd you're looking for. Move along.\n";
continue; //This is not the fd you're looking for, move along.
}
if(setsockopt(fdInit, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
cout << "Doom has fallen upon this set socket.\n";
perror("setsockopt");
exit(1); //Unable to set socket, exit program with code 1
}
if(bind(fdInit, temp->ai_addr, temp->ai_addrlen) == -1){
cout << "Could not bind fd\n";
close(fdInit);
continue; //Could not bind fd, continue looking for valid fd
}
break; //If a valid fd has been found, stop checking the list
}
if(temp==NULL){
cout<<"Server failed to bind a socket\n";
exit(2);
}
cout << fdInit << endl;
//Setup the file descriptor for initial connections on specified port
freeaddrinfo(res);
FD_SET(fdInit, &masterSet);
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒!谢谢.
Kie*_*ron 13
TCP连接通过的IP地址和端口号识别的两个连接的端部.因此,有很多客户端(通常会随机分配端口号)连接到单个服务器端口.
您创建一个套接字并将bind()其创建到一个端口listen(),然后等待客户端敲击它.如果你不介意阻止你可以直接调用accept()它,但你不会做任何超时循环或任何事情.否则,您可以select()在侦听套接字上,当客户端尝试连接时,它将变为可读,然后调用accept().
accept()将返回一个新创建的套接字,这是与客户端通信的实际套接字.原始侦听套接字继续侦听,并且可以接受更多连接.
通常使用select()循环来查找侦听套接字和任何连接套接字的可读性.然后select()返回时,只需检查侦听套接字是否可读,如果是,则accept(); 否则寻找一个可读的连接套接字并处理它.
fd_set fds;
int max = 0, reuse = 1;
struct timeval tv;
int server;
std::vector<int> connected;
// create server listening socket
server = socket(PF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto);
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)); // optional, but recommended
if (bind(server, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
// error, could not bind server socket
}
if (listen(server, 8) < 0) {
// error, could not listen on server port
}
// loop looking for connections / data to handle
while (running) {
FD_ZERO(&fds);
FD_SET(server, &fds);
if (server >= max) max = server + 1;
for (std::vector<int>::iterator it = connected.begin(); it != connected.end(); ++it) {
FD_SET(*it, &fds);
if (*it >= max) max = *it + 1;
}
tv.tv_sec = 2; tv.tv_usec = 0;
if (select(max, &fds, NULL, NULL, &tv) > 0) {
// something is readable
if (FD_ISSET(server, &fds)) {
// it's the listener
connected.push_back(accept(server, (struct sockaddr *)&addr));
}
for (std::vector<int>::iterator it = connected.begin(); it != connected.end(); ++it) {
if (FD_ISSET(*it, &fds)) {
// handle data on this connection
}
}
}
}
Run Code Online (Sandbox Code Playgroud)