-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_cli.c
43 lines (35 loc) · 931 Bytes
/
echo_cli.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
/*Socket : Day Time Client*/
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<time.h>
#include<arpa/inet.h>
void str_cli(int sockfd);
int main(int argc, char *argv[])
{
int sockfd,n,conn,len;
char recvline[1024];
struct sockaddr_in servaddr;
len=sizeof(struct sockaddr_in);
sockfd=socket(AF_INET,SOCK_STREAM,0);
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);//get ip from server
servaddr.sin_port=htons(atoi(argv[2])); // Get the port from the server
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
str_cli(sockfd);
exit(0);
}
void str_cli(int sockfd){
char sendline[1024], recvline[1024];
while(fgets(sendline,1024,stdin)!=NULL)
{
write(sockfd, sendline,strlen(sendline));
if(read(sockfd,recvline,1024)==0)
perror("error");
fputs(recvline, stdout);
}
}