-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpcli2.c
More file actions
65 lines (56 loc) · 1.48 KB
/
tcpcli2.c
File metadata and controls
65 lines (56 loc) · 1.48 KB
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#define SRVPORT 49152
main()
{
int s, cnt;
char lbuf[80], msg[80];
struct sockaddr_in skt;
struct in_addr ipaddr;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
printf("server IP address: ");
fgets(lbuf, sizeof lbuf, stdin);
if (inet_aton(lbuf, &ipaddr) < 0) {
perror("inet_aton");
exit(1);
}
memset(&skt, 0, sizeof skt);
skt.sin_family = AF_INET;
skt.sin_port = htons(SRVPORT);
skt.sin_addr.s_addr = htonl(ipaddr.s_addr);
if (connect(s, (struct sockaddr *)&skt, sizeof skt) < 0) {
perror("connect");
exit(1);
}
for (;;) {
printf("message to be sent: ");
if (fgets(msg, sizeof msg, stdin) == NULL) {
break;
}
msg[strlen(msg) - 1] = '\0';
if ((cnt = send(s, msg, sizeof msg, 0)) < 0) {
perror("send");
exit(1);
}
printf("%d bytes sent\n", cnt);
if ((cnt = recv(s, msg, sizeof msg, 0)) < 0) {
perror("recv");
exit(1);
}
msg[cnt] = '\0';
printf("%d bytes recved: IP %s, port %d, msg %s\n",
cnt, inet_ntoa(skt.sin_addr),
ntohs(skt.sin_port), msg);
}
printf("EOF received from keyboard\n");
close(s);
}