大佬教程收集整理的这篇文章主要介绍了在C网络中,使用select我首先必须listen()和accept()?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是客户端代码:
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);
这是服务器代码:
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);
任何帮助都会很棒!谢谢.
你创建一个套接字并将它绑定到一个侦听()的端口,然后等待客户端敲响它.如果你不介意阻止你可以直接调用它上面的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 socket server = socket(PF_INET,SOCK_STREAM,getprotobyname("tcp")->p_proto); setsockopt(server,&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,&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()?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。