我是这个网站的新手,所以如果我之前已经回答了这个问题,我会提前道歉,虽然我已经搜索过这个问题,然后再向大家开放。
我在我的一个C程序中使用套接字编程。它具有服务器和客户端模块,其中服务器和客户端可以双向通信。程序工作正常,因为我能够向两个方向发送文件和消息。
我的服务器程序正在使用端口3873
,我已使用netstat -anp | grep 3873
我观察到套接字有一种奇怪的行为,特别是当我尝试使用http://localhost:3873
或telnet localhost 3873
等浏览器连接套接字时。它立即关闭套接字和随后的'netstat -anp | grep 3873'输出确认localhost不再侦听端口3873
。
如果有人能说清楚这种行为,我真的很感激。这是预期的行为吗?
以下是服务器代码的相关部分:主程序启动专用线程并调用startFileServerMT,后续调用handleClient来为连接到套接字上的服务器的每个客户端提供服务
int handleClient(void *ptr){
DEBUG("Inside the %s %s() \n",__FILE__,__func__);
int connectSOCKET;
connectSOCKET = (int ) ptr;
char recvBUFF[4096],sendBUFF[4096];
char *filename;
FILE * recvFILE;
char *header[4096];
while(1){
if( recv(connectSOCKET, recvBUFF, sizeof(recvBUFF), 0) > 0){
if(!strncmp(recvBUFF,"FBEGIN",6)) {
recvBUFF[strlen(recvBUFF) - 2] = 0;
parseArgs(header,recvBUFF);
filename = (char*) strngDup(header[1]);
DEBUG(" About to receive file: %s\n", filename);
}
char *rfile = ALLOC(sizeof(char) * (strlen(This.uploadDIR) + strlen(filename) + 35));
strcpy(rfile,This.uploadDIR);
if (strngLastChar(rfile) == '/'){
strcat(rfile,filename);
}else{
strcat(rfile,"/");
strcat(rfile,filename);
}
DEBUG(" Absolute file is : %s\n", rfile);
recvBUFF[0] = 0;
if ((recvFILE = fopen (rfile,"w" )) == NULL){
LogError("Server could not create file %s on the shared location %s.\n",filename,This.uploadDIR);
}else{
bzero(recvBUFF,4096);
int fr_block_sz, write_sz;
while((fr_block_sz = recv(connectSOCKET, recvBUFF, 512, 0)) > 0 ){
write_sz = fwrite (recvBUFF , sizeof(recvBUFF[0]) , fr_block_sz , recvFILE );
DEBUG(" Received buffer is : %s\n", recvBUFF);
if(write_sz < fr_block_sz){
LogError("Failed writing file %s on the Server shared location.\n",filename);
break;
}
bzero(recvBUFF,4096);
recvBUFF[0] = 0;
if(write_sz == 0 || fr_block_sz != 512 ){
break;
}
}
if(fr_block_sz < 0){
if(errno == EAGAIN){
LogError("Server collection file %s receive timed out.\n",filename);
}else{
LogError("Failed file %s transfer due to error %d\n",filename,errno);
fclose(recvFILE);
FREE(rfile);
// Start - Following code send failed status to client
sprintf(sendBUFF,"FSTATUS:FAILED\r\n");
if (send(connectSOCKET, sendBUFF, sizeof(sendBUFF), 0) >= 0){
DEBUG("File transfer status for file %s sent\n",filename);
}else{
DEBUG("Failed sending transfer status for file %s\n",filename);
return FALSE;
}
// End
close(connectSOCKET);
return FALSE;
}
}
DEBUG("File %s received on OM Server successfully.\n",filename);
// Start - Following code send failed status to client
sprintf(sendBUFF,"FSTATUS:SUCCESS\r\n");
if (send(connectSOCKET, sendBUFF, sizeof(sendBUFF), 0) >= 0){
DEBUG("File transfer status for file %s sent\n",filename);
}else{
DEBUG("Failed sending transfer status for file %s\n",filename);
return FALSE;
}
// End
fclose(recvFILE);
updateTargets(rfile);
FREE(rfile);
close(connectSOCKET);
break;
}
}
else {
LogInfo("Client dropped connection\n");
}
/** End*/
return TRUE;
}
}
int startFileServerMT(){
DEBUG("Inside %s %s() \n",__FILE__,__func__);
int listenSOCKET, connectSOCKET[512],thread_status;
int socketINDEX = 0;
pthread_t clientFileThread[512];
socklen_t clientADDRESSLENGTH[512];
struct sockaddr_in clientADDRESS[512], serverADDRESS;
if((listenSOCKET = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
LogAbortError("File server could not create socket.\n");
close(listenSOCKET);
return FALSE;
}
serverADDRESS.sin_family = AF_INET;
serverADDRESS.sin_addr.s_addr = htonl(INADDR_ANY);
serverADDRESS.sin_port = htons(This.serverport);
if (bind(listenSOCKET, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0) {
LogAbortError("File server could not bind socket and will stop server now.\n");
close(listenSOCKET);
This.stopped = TRUE;
return FALSE;
}
if(listen(listenSOCKET, 5) == -1){
LogAbortError("Server failed to listen on port %d and will stop server now.\n",This.serverport);
close(listenSOCKET);
This.stopped = TRUE;
return FALSE;
}else{
LogInfo("Server listening on port %d successfully.\n",This.serverport);
}
clientADDRESSLENGTH[socketINDEX] = sizeof(clientADDRESS[socketINDEX]);
while(TRUE){
// DEBUG(" Inside the file server main loop index[%d].\n",socketINDEX);
connectSOCKET[socketINDEX] = accept(listenSOCKET, (struct sockaddr *) &clientADDRESS[socketINDEX], &clientADDRESSLENGTH[socketINDEX]);
if(connectSOCKET[socketINDEX] < 0){
LogError("Server could not accept connection.\n");
close(listenSOCKET);
return FALSE;
}else
DEBUG(" Another client connected to server socket.\n");
ThreadCreateDetached( &clientFileThread[socketINDEX], handleClient, connectSOCKET[socketINDEX]);
if(socketINDEX=512) {
socketINDEX = 0;
} else {
socketINDEX++;
}
if (This.stopped == TRUE){
close(listenSOCKET);
return TRUE;
}
/** End*/
// return TRUE;
}
close(listenSOCKET);
return TRUE;
}
答案 0 :(得分:0)
这是您的问题 - 或者至少是您问题的一部分。它不仅是一个一个错误的错误(connectSOCKET接受0-511 包含的索引,所以如果你在512,你已经写过了connectSOCKET数组的末尾)但是你做了分配而不是比较:
if(socketINDEX=512) { // Error: you are *setting* socketINDEX to 512,
// then immediately *resetting* it back to 0. So
// every new connection overrides the existing
// socket.
socketINDEX = 0;
} else {
socketINDEX++;
}
作为旁注,你真的应该重构你的代码并尝试清理它。