-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
54 lines (48 loc) · 1.07 KB
/
client.c
File metadata and controls
54 lines (48 loc) · 1.07 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define BUFSIZE 1024
char buf[BUFSIZE] = "Hello World!";
/* Main program:
* * - Process arguments.
* * - Open socket and establish connection to server.
* * - Read text line by line and send it over this connection.
* * - Close connection at end of entry (Ctrl-D).
* */
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in addr;
char *p;
int n;
if (argc != 3) {
fprintf(stderr, "Usage: %s <address> <port>\n", argv[0]);
exit(1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
addr.sin_addr.s_addr = inet_addr(argv[1]);
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
perror("connect");
exit(1);
}
while (1) {
printf("%s\n", buf);
n = write(s, buf, strlen(buf));
if (n == -1) {
perror("write");
break;
}
printf("%d\n", n);
}
close(s);
exit(0);
}