编译gcc -Wall -o ss ss.c出现问题如下:
ss.c:1: parse error before "buffer"
ss.c:1:36: missing terminating ' character
ss.c:1:36: warning: character constant too long
ss.c:2: parse error before "you"
ss.c:2: warning: type defaults to `int' in declaration of `file'
ss.c:2: warning: type defaults to `int' in declaration of `first'
ss.c:2: parse error before "visit"
ss.c:3: parse error before "enter"
ss.c:3:36: missing terminating ' character
ss.c:3:36: warning: character constant too long
In file included from ss.c:5:
/usr/include/string.h:39: parse error before "size_t"
/usr/include/string.h:42: parse error before "size_t"
/usr/include/string.h:51: parse error before "size_t"
/usr/include/string.h:58: parse error before "size_t"
/usr/include/string.h:61: parse error before "size_t"
/usr/include/string.h:65: parse error before "size_t"
/usr/include/string.h:86: parse error before "size_t"
/usr/include/string.h:93: parse error before "size_t"
/usr/include/string.h:99: parse error before "size_t"
/usr/include/string.h:106: parse error before "strxfrm"
/usr/include/string.h:107: parse error before "size_t"
/usr/include/string.h:176: parse error before "strcspn"
/usr/include/string.h:180: parse error before "strspn"
/usr/include/string.h:230: parse error before "strlen"
/usr/include/string.h:248: parse error before "size_t"
/usr/include/string.h:253: parse error before "size_t"
/usr/include/string.h:257: parse error before "size_t"
/usr/include/string.h:260: parse error before "size_t"
/usr/include/string.h:263: parse error before "size_t"
/usr/include/string.h:291: parse error before "size_t"
In file included from /usr/include/sys/types.h:266,
from ss.c:6:
/usr/include/bits/pthreadtypes.h:48: parse error before "size_t"
/usr/include/bits/pthreadtypes.h:51: parse error before "__stacksize"
In file included from /usr/include/sys/uio.h:29,
from /usr/include/sys/socket.h:27,
from ss.c:7:
/usr/include/bits/uio.h:45: parse error before "size_t"
In file included from /usr/include/sys/socket.h:35,
from ss.c:7:
/usr/include/bits/socket.h:221: parse error before "size_t"
/usr/include/bits/socket.h:224: parse error before "msg_controllen"
/usr/include/bits/socket.h:227: parse error before '}' token
/usr/include/bits/socket.h:232: parse error before "size_t"
/usr/include/bits/socket.h:239: parse error before '}' token
In file included from ss.c:7:
/usr/include/sys/socket.h:131: parse error before "size_t"
/usr/include/sys/socket.h:136: parse error before "size_t"
/usr/include/sys/socket.h:141: parse error before "size_t"
/usr/include/sys/socket.h:149: parse error before "size_t"
In file included from /usr/include/_G_config.h:44,
from /usr/include/libio.h:32,
from /usr/include/stdio.h:72,
from ss.c:9:
/usr/include/gconv.h:72: parse error before "size_t"
/usr/include/gconv.h:88: parse error before "size_t"
/usr/include/gconv.h:97: parse error before "size_t"
/usr/include/gconv.h:174: parse error before "size_t"
/usr/include/gconv.h:177: parse error before '}' token
In file included from /usr/include/libio.h:32,
from /usr/include/stdio.h:72,
from ss.c:9:
/usr/include/_G_config.h:47: field `__cd' has incomplete type
/usr/include/_G_config.h:50: field `__cd' has incomplete type
/usr/include/_G_config.h:52: confused by earlier errors, bailing out
#define MAX_BUF_SIZE 1024
#define SERVER_PORT 8080
#define BACKLOG 5
int main(int argc,char *argv[])
{
int listenfd,connfd;
struct sockaddr_in servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0); //use the function socket to create a socket
if (listenfd<0) {
fprintf(stderr,"Socket error\n");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr= htonl(INADDR_ANY); //the server's ip is INADDR_ANY,that is it can accept the request of client
//from any net device.
servaddr.sin_port=htons(SERVER_PORT);
if (bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0) {
fprintf(stderr,"Bind error\n");
exit(1);
}
if (listen(listenfd,BACKLOG)<0) { //BACKLOG is the biggest unaccepted number which client request.
fprintf(stderr,"Listen error\n");
exit(1);
}
for (;;) {
connfd=accept(listenfd,NULL,NULL);
if (connfd<0) {
fprintf(stderr,"Accept error\n");
exit(1);
}
serv_respon(connfd);
close(connfd);
}
close(listenfd);
}
void serv_respon(int sockfd)
{
int nbytes;
char buf[1024];
for (;;) {
nbytes=read_requ(sockfd,buf,1024);
if (nbytes==0)
return ;
else if(nbytes<0) {
fprintf(stderr,"read error: %s\n",strerror(errno));
return ;
}
if (write_all(sockfd,buf,nbytes)<0)
fprintf(stderr,"write error:%s\n",strerror(errno));
}
}
int read_requ(int sockfd,char *buf,int size)
{
char inbuf[256];
int n;
int i;
i=read_line(sockfd,inbuf,256);
if(i<0)
return(i);
else if(i==0)
return(0); //the customer has closed the connect
if(strncmp(inbuf,"Length",6)==0)
sscanf(buf+6,"%d",&n);
else {
spintf(buf,"Invalid option",14);
return(14);
}
return(read_all(sockfd,buf,n));
}
int get_char(int fd,char *ch)
{
static int offset=0;
static int size=0;
static char buf[MAX_BUF_SIZE];
for (;size<=0||offset==size;){
size=read(fd,buf,MAX_BUF_SIZE);
if(size<0)
{
if(errno==EINTR)
{
size=0;
continue;
}
else
return(-1);
}
offset=0;
}
*ch=buf[offset++];
return(1);
}