我想弄清楚什麼是阻止我的程序。我正在運行一個使用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