-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
74 lines (60 loc) · 1.7 KB
/
client.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* A simple, TCP based echo client */
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
void str_cli(int, char**);
int main(int argc, char **argv) {
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2) {
printf("\nUsage: tcp-client <IPaddress>");
exit(0);
}
/* creating a socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n%s: Error in socket", argv[0]);
exit(0);
}
/* configuring server address structure */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(9945);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("\n%s: Error in inet_pton", argv[0]);
exit(0);
}
/* connecting to the server */
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
printf("\n%s: Error in connect", argv[0]);
exit(0);
}
str_cli(sockfd, argv);
}
void str_cli (int sockfd, char **argv) {
char sendline[1024], recvline[1024];
int n;
/* read a line from stdin */
if (fgets(sendline, 1024, stdin) == NULL) {
printf("\n%s: Error in fgets", argv[0]);
exit(0);
}
/* send it to echo server */
if (write(sockfd, (const void*) sendline, strlen(sendline)) < 0) {
printf("\n%s: Error in write", argv[0]);
exit(0);
}
/* get the echo from the echo server and display it on the terminal*/
while ( (n = read(sockfd, recvline, 1024)) > 0) {
recvline[n] = 0; /* null terminate the string */
fputs(recvline, stdout);
}
close(sockfd);
}