我想弄清楚什么是阻止我的程序。我正在运行一个使用POSIX线程的服务器。我必须为我的计算机编程实验室。主函数监听新的连接。一旦它接受了连接,它就会通过将FD传递给该线程来创建一个新线程。我能够使用多个telnet /客户端连接成功连接到服务器。我可以成功地将数据发送到服务器一次,但如果我尝试再次发送服务器将不会执行任何操作。主要功能使用Unix和C/C++的块套接字帮助
int active_thread = 0;
//The Running loop
while(running)
{
if(active_thread > NUMBTHREADS)
{
printf("Unable to accept client connection! Threads are all used up");
running = false;
}
else
{
if(FD_ISSET(sockfd, &readfds))
{
if((bindfd[active_thread] = accept(sockfd, (struct sockaddr *) &client_addr, &client_sock_size)) == -1)
{
fprintf(stderr, "Unable to accept client \n");
perror("What");
break;
}
activethreads[active_thread] = pthread_create(&threads[active_thread], NULL, server_handler, (void*) &bindfd[active_thread]);
//close(bindfd[active_thread]);
//pthread_join(threads[active_thread], NULL);
active_thread++;
//running = false;
}
}
}
close(sockfd);
return 0;
}
POSIX线程代码
void *server_handler(void *sockfd)
{
int bindfd = *((int *) sockfd);
char buffer[MESSAGELENGTH];
bool running = true;
printf("Thread was created successfully\n");
char intro[] = "Successfully Connected to server!\n";
struct pollfd pfd;
pfd.fd = bindfd;
pfd.events = POLLIN;
if ((send(bindfd, intro, strlen(intro), 0)) < 0)
{
perror("Unable to send");
}
while(running){
char msg[] = "\nYou have the following options!\n1) Insert an integer: insert <integer>\n2) Remove An Integer: remove <integer>\n3) Get number of integers in list: get_count\n4) Get first integer: get_first\n5) Get last integer: get_last\n6) Quit program: quit\n ";
if ((send(bindfd, msg, strlen(msg), 0)) < 0)
{
perror("Unable to send");
}
memset(&buffer, 0, MESSAGELENGTH);
if (recv(bindfd, buffer, MESSAGELENGTH, 0) > 0)
{
//SOme other code
}
}
的部分
部分,我认为它在阻止无论是接受或recv。我听说过select()和其他各种方法,但我很难实现它们。谢谢!
你真的需要使用select(或轮询,而是选择更容易IMO),以确保您的套接字fd的准备读或写。对不起,我没有时间atm提供一个真正的答案,但谷歌周围的例子与选择,我相信你会最好的。 – 2011-03-31 01:01:33
在几个打印语句中添加应该知道它阻塞的位置。如果您在拨打'recv'时阻止,则可能等待您发送数据。作为一个方面说明,看起来你在'server_handler'中的'while'循环中有大括号的不匹配 – Jeff 2011-03-31 01:17:11