C++ 使用c++;套接字-使用accept()和文件描述符会导致严重错误
我已经使用这个网站有一段时间了,并且(在这个学期之后)期待着回馈!然而,我想最后一次得到你的帮助,而其他答案似乎没有任何帮助 背景:使用C++、socket和客户机/服务器体系结构设计一个基本的聊天室,最多十个客户端与服务器对话。一个客户机所说的任何话都会在所有客户机和服务器之间得到响应(发送客户机除外) 问题:错误发生在recv调用时,作为clientTalk函数中的while循环条件。我得到了一个“接收失败:错误的文件描述符”,但服务器和客户端都没有完全“崩溃”,行为也没有明显的变化,尽管接收完全崩溃 试图解决:C++ 使用c++;套接字-使用accept()和文件描述符会导致严重错误,c++,sockets,client-server,C++,Sockets,Client Server,我已经使用这个网站有一段时间了,并且(在这个学期之后)期待着回馈!然而,我想最后一次得到你的帮助,而其他答案似乎没有任何帮助 背景:使用C++、socket和客户机/服务器体系结构设计一个基本的聊天室,最多十个客户端与服务器对话。一个客户机所说的任何话都会在所有客户机和服务器之间得到响应(发送客户机除外) 问题:错误发生在recv调用时,作为clientTalk函数中的while循环条件。我得到了一个“接收失败:错误的文件描述符”,但服务器和客户端都没有完全“崩溃”,行为也没有明显的变化,尽管接
- 谷歌和软件。两个小时
- 移动变量、使用套接字设置和打开/关闭
==773== Warning: invalid file descriptor 96600128 in syscall read()
Recieve failed: Bad file descriptor
==773== Thread 2:
==773== Conditional jump or move depends on uninitialised value(s)
==773== at 0x4015A0: ??? (in /nethome/users/ghm455/CS284/ChatServer/server)
==773== by 0x4E39E99: start_thread (pthread_create.c:308)
==773== by 0x5442CBC: clone (clone.S:112)
==773==
==773== Conditional jump or move depends on uninitialised value(s)
==773== at 0x401614: ??? (in /nethome/users/ghm455/CS284/ChatServer/server)
==773== by 0x4E39E99: start_thread (pthread_create.c:308)
==773== by 0x5442CBC: clone (clone.S:112)
==773==
==773== Warning: invalid file descriptor 96600128 in syscall close()
第一个警告出现在我的accept语句中。接收失败发生在recv上,最后一次警告发生在尝试关闭时。这是由贯穿整个代码的cout语句决定的
代码:见下文。如果您认为错误就在那里,我将发布客户端,但所有这些都表明这是服务器端的问题
`#define SERVER_PORT 9999 /* define a server port number */
using std::cout;
using std::endl;
using std::string;
//Globals - descriptorArray holds client FDs. arraySize is its size.
//soc holds the information on the server's socket.
//m is the global mutex shared among the server and all clients
const int MAX_CLIENT = 10;
int descriptorArray[MAX_CLIENT];
int arraySize = 0;
int soc;
pthread_mutex_t m;
struct thread_info
{
pthread_t threadID; //Stores the ID number returned by pthread_create
int threadNumber; //We have to number incoming threads correctly
char *messageSent; //Message taken in from command line
};
int main()
{
void exitHandler(int sig); // Function that handles the control-C
void* clientTalk(void *arg); // Reads and writes with clients
struct sockaddr_in server_addr, client_addr;
int option = 1;
unsigned int clientCount;
uint8_t *new_socket;
//Initialize the socket
soc = socket(AF_INET, SOCK_STREAM, 0);
if (soc < 0)
{
cout << "ERROR : problem opening socket" <<endl;
return 1;
}
//Create socket structure
bzero((char *) &server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(SERVER_PORT);
//Binding host address
if (bind(soc, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
{
cout << "ERROR : problem occured while binding" <<endl;
close(soc);
exit(1);
}
if(setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, (char*) &option, sizeof(option)) < 0)
{
printf("setsockopt failed\n");
close(soc);
exit(1);
}
signal(SIGINT, exitHandler);
listen(soc, MAX_CLIENT);
clientCount = sizeof(struct sockaddr_in);
int clientID;
//cout << "Z " <<endl;
while(clientID = accept(soc, (struct sockaddr *) &client_addr, (socklen_t*)&clientCount))
{
//printf( "A");
if (clientID < 0 )
{
perror("ERROR ON ACCEPT");
exit(1);
}
else
{
pthread_t newClient;
new_socket = (uint8_t*)malloc(1);
*new_socket = clientID;
pthread_mutex_lock(&m);
if (arraySize < 10)
{
descriptorArray[arraySize++] = newClient;
}
else
{
cout << "There are already 10 clients connected!" << endl;
}
pthread_mutex_unlock(&m);
if( pthread_create(&newClient, NULL, clientTalk, &new_socket) < 0)
{
perror("ERROR Creating thread");
return 1;
}
cout << "Assigned!" <<endl;
pthread_join(newClient, NULL);
}
}
close(soc);
return 0;
}
void exitHandler(int sig)
{
sig = sig + 0; //Removing the warning. A clean compile looks a lot nicer.
for (int i = 0; i < arraySize; i++)
{
write(descriptorArray[i], "WARNING: Server exiting in ten seconds. End your conversation!", 1000);
}
cout << "WARNING: Server exiting in ten seconds. ";
cout << "End your conversation!" << endl;
sleep(10f);
for (int i = 0; i < arraySize; i++)
{
close(descriptorArray[i]);
}
close(soc);
exit(1);
}
void* clientTalk(void* arg)
{
int* myFD = (int*) arg;
char buffer[2000];
read(*myFD, buffer, 20);
char username[20];
//strcpy(username, buffer); // Takes in the username and stores it
char movableString[2000];
int amount_read;
// for (int i = 0; i < arraySize; i++)
// {
//strcpy(movableString, username);
// strcat(movableString, " has joined the room!");
//if (descriptorArray[0] != *myFD)
//{
// write(descriptorArray[0], movableString, 2000);
//}
//}
cout << "x" << endl;
int arrayLocation;
while ( (amount_read = recv(*myFD, buffer, 2000, MSG_WAITALL)) > 0)
{
cout << " Um" << endl;
pthread_mutex_lock(&m);
for (int i = 0; i < arraySize; i++)
{
if (descriptorArray[i] == *myFD)
{
arrayLocation = i;
break;
}
}
strcpy(movableString, username);
strcat(movableString, ": ");
strcat(movableString, buffer);
for (int i = 0; i < arraySize; i++)
{
//if (i != arrayLocation)
//{
write(*myFD, movableString, 2000);
//}
}
pthread_mutex_unlock(&m);
}
if (amount_read == 0)
{
cout << username << "disconnected unexpectedly" <<endl;
fflush(stdout);
}
if (amount_read == -1)
{
perror("Recieve failed");
}
pthread_mutex_lock(&m);
for (int i = 0; i < arraySize; i++)
{
if (descriptorArray[i] == *myFD)
{
arrayLocation = i;
break;
}
}
for (int i = arrayLocation; i < arraySize - 1; i++)
{
descriptorArray[i] = descriptorArray[i + 1];
}
arraySize--;
pthread_mutex_unlock(&m);
close(*myFD);
pthread_exit(NULL);
free(arg);
}
`#定义服务器_端口9999/*定义服务器端口号*/
使用std::cout;
使用std::endl;
使用std::string;
//Globals-描述符raray保存客户端FDs。arraySize是它的大小。
//soc保存服务器套接字上的信息。
//m是服务器和所有客户端之间共享的全局互斥体
const int MAX_CLIENT=10;
int descriptorArray[MAX_CLIENT];
int-arraySize=0;
int soc;
pthread_mutex_t m;
结构线程信息
{
pthread\u t threadID;//存储pthread\u create返回的ID号
int threadNumber;//我们必须正确地为传入线程编号
char*messageSent;//从命令行接收的消息
};
int main()
{
void exitHandler(int sig);//处理控件-C的函数
void*clientTalk(void*arg);//与客户端一起读写
服务器地址、客户端地址中的结构sockaddr\u;
int选项=1;
无符号整数clientCount;
uint8_t*新的_插座;
//初始化套接字
soc=套接字(AF_INET,SOCK_STREAM,0);
如果(soc<0)
{
cout该错误意味着传递给read函数的文件描述符不是有效的文件描述符,因此调试时要做的第一件事是确保ClientTalk函数中文件描述符的值与main中的值相同
@注释中J.N.是正确的。它们将不一样,因为FD是一个int,您只将第一个字节传递给函数(并将其转换为int*指针)
你可能想在C中编写这个程序,因为它看起来不像C++代码。
将ClientID和新的_套接字更改为int/int*。
使用一致的类型,并使用函数定义中的类型accept返回一个int,因此对所有内容都使用int/int*
调用malloc时,尽可能使用sizeof而不是字节数
可能还有其他问题。这里至少有两个问题。首先,您从未调用pthread\u mutex\u init
,因此您的互斥体从未被创建为正常状态(尽管如果处于全局范围,它将被置零)
其次,您将&new\u socket
作为void*
传递给线程。这是uint8\u t**
类型,而在clientTalk
函数中,您用C风格将其转换为int*
,这是一种完全不同的指针类型,当您传递套接字时肯定不会提供您想要的结果。在线程中使用int8\u t
,它应该是size\u t
(指针的大小)在C++中,你应该使用<代码>新的<代码> >而不是<代码> MalOC 。INT作为接受的POSIX定义,返回INTHORD,但是我在“代码”中停止阅读。尽管接收到完全的< /代码>,但在行为上没有明显的变化,这对我来说毫无意义,FYI,2个小时不是很长的时间。试图解决一个编程问题!