-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientasciisum.c
49 lines (48 loc) · 1007 Bytes
/
clientasciisum.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
#include"myheaders.h"
int main(int argc,char **argv)
{
if(argc<3)
{
puts("input format:client_exe well_known port_no server ip");
return;
}
int fd;
puts("creating server socket");
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd<0)
{perror("socket");return;
}
puts("client socket created");
struct sockaddr_in addr;
int len=sizeof(addr);
puts("binding...");
addr.sin_family=AF_INET;
addr.sin_port=atoi(argv[1]);
addr.sin_addr.s_addr=inet_addr(argv[2]);
if(connect(fd,(const struct sockaddr *)&addr,len)<0)
{perror("client");close(fd);return;}
puts("connect success");
char buff[100];int status;
puts("enter message for server");
gets(buff);
status=send(fd,buff,100,0);
if(status<0)
{perror("send");}
puts("waiting for echoes");
memset(buff,0,100);
status=recv(fd,buff,100,0);
if(status<0)
{perror("recv");}
else if(status==0)
{puts("server abruptly terminated");
}
else
{
printf("client received:%s\n",buff);
}
if(strcmp(buff,"quit")==0)
{
puts("server formally terminating connection with client");
}
close(fd);
}