-
Notifications
You must be signed in to change notification settings - Fork 1
/
qn-1-server.c
52 lines (46 loc) · 835 Bytes
/
qn-1-server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include"header.h"
int main(int argc ,char **argv)
{
if(argc !=2)
{
printf("Usage:./server port_number\n");
return;
}
int sfd,nsfd,r,len;
struct sockaddr_in v,v1;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket");
return;
}
v.sin_family=AF_INET;
v.sin_port=htons(atoi(argv[1]));
v.sin_addr.s_addr=inet_addr("0.0.0.0");
len=sizeof(v);
if(bind(sfd,(struct sockaddr*)&v,len)==-1)
{
perror("bind");
return ;
}
if(listen(sfd,5)==-1)
{
perror("listen");
return ;
}
char s[50];
nsfd=accept(sfd,(struct sockaddr*)&v1,&len);
read(nsfd,s,sizeof(s));
int start,end,i,j;
for(start=end=0;end<=strlen(s);end++)
{
if(s[end]!=' ' && s[end]!='\0')
continue;
i=start;
j=end-1;
for(;i<j;i++,j--)
s[i]=s[i]+s[j]-(s[j]=s[i]);
start=end+1;
}
write(nsfd,s,strlen(s)+1);
return 0;
}