-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientint.c
50 lines (49 loc) · 1016 Bytes
/
clientint.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
#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");
int buff;int status;
puts("enter number for server");
scanf("%d",&buff);
status=send(fd,&buff,4,0);
if(status<0)
{perror("send");}
puts("waiting for echoes");
//memset(buff,0,4);
int rev;
status=recv(fd,&rev,4,0);
if(status<0)
{perror("recv");}
else if(status==0)
{puts("server abruptly terminated");
}
else
{
printf("client received:%d\n",rev);
}
/*if(strcmp(buff,"quit")==0)
{
puts("server formally terminating connection with client");
}*/
close(fd);
}