forked from IpovsOperatingSystems/os_lab_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpclient.c
executable file
·54 lines (45 loc) · 1.03 KB
/
tcpclient.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define BUFSIZE 100
#define SADDR struct sockaddr
#define SIZE sizeof(struct sockaddr_in)
int main(int argc, char *argv[]) {
int fd;
int nread;
char buf[BUFSIZE];
struct sockaddr_in servaddr;
if (argc < 3) {
printf("Too few arguments \n");
exit(1);
}
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket creating");
exit(1);
}
memset(&servaddr, 0, SIZE);
servaddr.sin_family = AF_INET;
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
perror("bad address");
exit(1);
}
servaddr.sin_port = htons(atoi(argv[2]));
if (connect(fd, (SADDR *)&servaddr, SIZE) < 0) {
perror("connect");
exit(1);
}
write(1, "Input message to send\n", 22);
while ((nread = read(0, buf, BUFSIZE)) > 0) {
if (write(fd, buf, nread) < 0) {
perror("write");
exit(1);
}
}
close(fd);
exit(0);
}