如果我的代码太长,我深表歉意,但我正在尝试创建一个处理多个本地客户端的本地服务器。我什至从 http://www.binarytides.com/multiple-socket-connections-fdset-select-linux/ 导入想法,试图让它工作但没有成功。
我使用 82 作为参数运行它,并看到预期的结果:
Socket made and ready
Accepting 10 users
然后,我使用 CURL 连接到 127.0.0.1:82,curl 停止。在我的程序中,我看到了预期的结果:
CLIENT CONNECTION MADE on socket# 0!
但问题是数据没有从服务器发送到客户端。
我尝试强制中断 CURL(通过 ctrl+c)以查看服务器上是否发生任何情况,但没有任何反应。我什至尝试使用网络浏览器进行连接并收到类似的结果(挂起)。
如果我强制中断服务器(通过 ctrl+c),那么我会得到我所期望的,一条断开连接消息(例如来自 CURL 的“来自服务器的空回复”)。
我期望在浏览器中看到的是:
Error
This is a hack-ed-server
我在这里可能做错了什么?我对 select() 调用有点陌生,所以我不确定我是否正确配置了它们。
这是代码:
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <signal.h>
#include <time.h>
extern errno;
long asock=-1,nsock=-1;
void end_app(int s){
struct sigaction si;
si.sa_handler=SIG_DFL;si.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);
sigaction(SIGTSTP,&si,NULL);
sigaction(SIGTTOU,&si,NULL);
sigaction(SIGTTIN,&si,NULL);
sigaction(SIGSEGV,&si,NULL);
sigaction(SIGTERM,&si,NULL);
sigaction(SIGHUP,&si,NULL);
char v[5000];
sprintf(v,"Abrupt exit detected sig# %d. Closing sockets.\n",s);
write(1,v,strlen(v));
if (asock > -1){close(asock);}
if (nsock > -1){close(nsock);}
}
const long trapsig(){
struct sigaction s,si;
si.sa_handler=SIG_IGN;si.sa_flags=0;
s.sa_handler=end_app;s.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);sigaction(SIGTSTP,&si,NULL);sigaction(SIGTTOU,&si,NULL);sigaction(SIGTTIN,&si,NULL);
if (sigaction(SIGSEGV,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
if (sigaction(SIGTERM,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
if (sigaction(SIGHUP,&s,NULL)==-1){printf("Cant trap signal!\n");return 1;}
}
//getreq params in: req=external buffer for data
// reqsz=size of external buffer. I set 10000
// nsock=valid socket pointer from accept()
//
//getreq params out: reqsz=actual size of data returned
//
void getreq(char* req,unsigned long *reqsz,long nsock){
//bufsize=how many bytes to read at once. High values like 5000 cause a stall.
//buffer=buffer of data from recv call
const unsigned long ibs=*reqsz,bufsize=5000;
char buffer[ibs],*rp=req;
//spacect=# of spaces in data read
//szct=iterator variable
//mysz=total length of returned data
//bufct=buffer counter to prevent segfault
//recvsz=data size returned from recv or
// forced -2 if buffer hits capacity
// or 2nd space in returned data is found
unsigned long spacect=0,szct=0,mysz=0,bufct=0;
long recvsz=1;char *p=buffer;
//
//Expected data: GET /whatever HTTP/x.x but we
// want /whatever
//
//loop until 2nd space is found or
//ibs bytes of data have been processed
while (recvsz > 0 && bufct < ibs){
recvsz=recv(nsock, p, bufsize, 0);
if (recvsz < 1){break;}
for (szct=1;szct<=recvsz;szct++){
if (*p==' '){spacect++;if (spacect > 2){spacect=2;recvsz=-2;break;}}
if (spacect==1 && *p != ' '){mysz++;if (mysz <= *reqsz){*rp++=*p;}}
p++;bufct++;if (bufct > ibs){recvsz=-2;break;}
}
}
// Process rest of data to try to avoid client errors
while (recvsz == -2){
recvsz=recv(nsock, buffer, bufsize, 0);
}
*reqsz=mysz;
}
int main(int argc,char* argv[]){
if (trapsig() < 0){return 1;}
//set maximum users to 10 and allocate space for each
long maxusers=10;long csock[11];memset(csock,0,11);
//do sanity checks and bind local socket
if (!argv[1]){printf("Port # required\n");return 1;}
if ((asock=socket(AF_INET, SOCK_STREAM, 0)) < 1){printf("Can't make socket! %s\n",strerror(errno));return 1;}
struct sockaddr_in a;
memset(&a,0,sizeof(a));
a.sin_family=AF_INET;
a.sin_addr.s_addr=inet_addr("127.0.0.1");
a.sin_port=htons(strtol(argv[1],NULL,10));
if (bind(asock,(struct sockaddr*)&a, sizeof(a))==-1){printf("Can't bind socket! %s\n",strerror(errno));return 1;}
if (listen(asock,10) < 0){printf("Can't listen! %s\n",strerror(errno));return 1;}
printf("Socket made and ready\nAccepting %d users\n",maxusers);
while(1){
usleep(10); //sleep incase processor is overloaded
fd_set SR;long SMAX=asock,n,canadd=0;
FD_ZERO(&SR);FD_SET(asock,&SR);
for (n=0;n<maxusers;n++){
if (csock[n] > 0){FD_SET(csock[n],&SR);}else{canadd=1;}
if (csock[n] > SMAX){SMAX=csock[n];}
}
long act=select(SMAX+1,&SR,0,0,0);
if (act != EINTR && act < 0){printf("Select error\n");}
if (canadd==1 && FD_ISSET(asock,&SR)){
//incoming connection detected
socklen_t alen=sizeof(a);
if (nsock=accept(asock, (struct sockaddr*)&a, &alen)< 0){printf("Can't accept! %s\n",strerror(errno));close(asock);return -1;}
for (n=0;n<maxusers;n++){if (csock[n]==0){csock[n]=nsock;break;}}
printf("CLIENT CONNECTION MADE on socket# %d!\n",n);
fcntl(nsock, F_SETFD, O_NONBLOCK);
//program reaches here when client first connects
}
for (n=0;n<maxusers;n++){
if (csock[n] > 0 && FD_ISSET(csock[n],&SR)){
//this section never seems to execute
unsigned long reqsz=10000;
char req[reqsz];
printf("Checking incoming data...\n",n);
getreq(req,&reqsz,csock[n]);
if (reqsz > 0){
printf("Received %d bytes\nData: %s\n",reqsz,req);
const char buf[10000]={"HTTP/1.0 200 OK\nConnection: close\nContent-type: text/html\n\n<html><head><title>hacked</title></head><body><H1>Error</h1><p>This is a hack-ed-server</p></body></html>\n\n"};
send(csock[n],buf,strlen(buf),0);
}else{
printf("Received no data\n");
}
printf("Closing.\n");
close(csock[n]);
csock[n]=0;
}
}
}
printf("Closing sockets\n");
close(asock);
return 0;
}
注意警告。这个
if(nsock=accept(asock, (struct sockaddr*)&a, &alen)< 0)
未按您的预期进行解析。事实上,它为
nsock
分配了 accept
返回值与 0(即 false
又名 0)的比较结果。你想要一些额外的括号:
if ((nsock = accept(asock, (struct sockaddr*)&a, &alen)) < 0)
这个
long csock[11];memset(csock,0,11);
不完全初始化
csock
——仅初始化前11个字节。你想要memset(csock, 0, sizeof(csock));