我正在尝试找到我之前发布的有关同步聊天消息的问题的解决方案,一位成员指出了我在select()函数的方向。我阅读了Beej网络指南下的this部分,并尝试编写在windows下给出的示例。它编译得很好但是当我去测试它时,程序崩溃并显示我的错误消息"-Select error"
- 在输入程序后输入端口号。我不确定如何使这个工作,请告知。
server.cpp
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
const int winsockver = 2;
int PORT;
int fdmax;
char buffer[80];
int main(void){
//*********************************************
fd_set master;
fd_set temp;
SOCKET newfd;
struct sockaddr_in connected_client;
//*********************************************
WSADATA wsadata;
if (WSAStartup(MAKEWORD(winsockver,0),&wsadata) == 0 ){
cout<<"-Winsock Initialized." << endl;
cout<<"Enter PORT:";
cin>> PORT;
//--------------------------------------------------------------------
struct sockaddr_in server_info;
server_info.sin_family = AF_INET;
server_info.sin_port = htons(PORT);
server_info.sin_addr.s_addr = INADDR_ANY;
SOCKET serv_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if ( serv_sock != INVALID_SOCKET){
if ( bind(serv_sock,(sockaddr*)&server_info,sizeof(server_info)) != -1 ){
char yes = '1';
if ( setsockopt(serv_sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) != SOCKET_ERROR){
cout<<"-Binding Successful." << endl;
}
}
if ( listen(serv_sock,5) != -1 ){
cout<<"-Listening for incoming connections." << endl;
}
FD_SET(serv_sock,&master);
fdmax = serv_sock; // keeping track of the largers sockfd, at this moment its serv_sock
//#########################################################
for(;;){
temp = master; // copying to temp the master set
if (select(fdmax+1,&temp,NULL,NULL,NULL) == -1 ){
cout<<"-select error." << endl;
}
//run through existing connections looking for data to read
for (int i = 0; i <= fdmax; i++){
if (FD_ISSET(i,&temp)){//we have one
if (i == serv_sock){//handle new connections
int size =sizeof(connected_client);
newfd = accept(serv_sock,(sockaddr*)&connected_client,&size);
if ( newfd == -1 ){
cout<<"-Accepted an invalid socket from newfd." << endl;
}else{//accept has returned a valid socket descriptor and we add it to the master
FD_SET(newfd,&master);
if (newfd > fdmax ){
fdmax = newfd;
}
char *connected_ip= inet_ntoa(connected_client.sin_addr);
cout<<"-Select server new connection from " << connected_ip << " " << endl;
}
}else{
//handle data from a client
int bytes_in;
bytes_in = recv(i,buffer,sizeof(buffer),0);
if ( bytes_in <= 0 ){
if (bytes_in == 0 ){
cout<<"-Connected socket " << i << ",disconnected " << endl;
}else{
cout<<"-Socket error." << endl;
}
closesocket(i);
FD_CLR(i,&master); //remove from master set.
}else{
//we get data from a client
for (int j=0; j <= fdmax; j++ ){
//send to everyone
if (FD_ISSET(j,&master)){
//except the listener and ourself
if ( (j != serv_sock) && (j != i) ){
if ( send(j,buffer,sizeof(buffer),0) == -1 ){
cout<<"-Sending error" << endl;
}
}
}
}
}
}
}
}
}
//#########################################################
}
//--------------------------------------------------------------------
}else{
cout<<"-Unable to Initialize." << endl;
}
if ( WSACleanup() != -1 ){
cout<<"-Cleanup Successful." << endl;
}
return 0;
}
答案 0 :(得分:1)
您的文件描述符集未正确初始化,因此当您在其上调用FD_SET()
时,它们仍然包含垃圾。
在开始使用之前,您应该致电FD_ZERO()
进行初始化:
FD_ZERO(&master);
FD_SET(serv_sock, &master);
fdmax = serv_sock;