-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEchoClient.c
More file actions
43 lines (37 loc) · 926 Bytes
/
EchoClient.c
File metadata and controls
43 lines (37 loc) · 926 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h> // perror
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
#define ERROR -1
#define BUFFER 1024
int main(int argc,char **argv){
struct sockaddr_in remote_server;
int sock;
char input[BUFFER], output[BUFFER];
int len;
if((sock = socket(AF_INET,SOCK_STREAM,0))== ERROR){
perror("socket: ");
exit(-1);
}
remote_server.sin_family = AF_INET;
remote_server.sin_port = htons(atoi(argv[2]));
remote_server.sin_addr.s_addr = inet_addr(argv[1]);
bzero(&remote_server.sin_zero,0);
if(connect(sock,(struct sockaddr *)&remote_server,sizeof(struct sockaddr_in))==ERROR){
perror("connect ");
exit(-1);
}
while(1){
fgets(input,BUFFER,stdin);
send(sock,input,strlen(input),0);
len = recv(sock,output,BUFFER,0);
output[len]='\0';
printf("%s\n",output);
}
close(sock);
}