这是客户端代码:
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 canfor(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);
这是服务器代码:
int rv = getaddrinfo(NulL,port,&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);
任何帮助都会很棒!谢谢.
解决方法 TCP连接由连接两端的IP地址和端口号标识.因此,有很多客户端(通常会随机分配端口号)连接到单个服务器端口.你创建一个套接字并将它绑定到一个侦听()的端口,然后等待客户端敲响它.如果你不介意阻止你可以直接调用它上面的accept(),但你不会做任何超时循环或任何事情.否则,您可以在侦听套接字上选择(),当客户端尝试连接时,它将变为可读,然后调用accept().
accept()将返回一个新创建的套接字,这是与客户端通信的实际套接字.原始侦听套接字继续侦听,并且可以接受更多连接.
通常使用select()循环来查找侦听套接字和任何连接套接字的可读性.然后当select()返回时,只需检查监听套接字是否可读,如果是,则接受();否则寻找一个可读的连接套接字并处理它.
fd_set fds;int max = 0,reuse = 1;struct timeval tv;int server;std::vector<int> connected;// create server Listening socketserver = socket(PF_INET,SOCK_STREAM,getprotobyname("tcp")->p_proto);setsockopt(server,&reuse,sizeof(int)); // optional,but recommendedif (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 handlewhile (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,&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 } } }}总结
以上是内存溢出为你收集整理的在C网络中,使用select我首先必须listen()和accept()?全部内容,希望文章能够帮你解决在C网络中,使用select我首先必须listen()和accept()?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)