-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.c
executable file
·83 lines (68 loc) · 1.84 KB
/
tcp_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
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#define PORT 0x1234
/* REPLACE with your server machine name*/
#define HOST "127.0.0.1"
#define DIRSIZE 8192
int main(int argc, char **argv)
{
char hostname[100];
char dir[BUFSIZ];
int sd,i;
struct sockaddr_in sin;
struct sockaddr_in pin;
struct hostent *hp;
strcpy(hostname,HOST);
if (argc>2)
{ strcpy(hostname,argv[2]); }
/* fill in the socket structure with host information */
memset(&pin, 0, sizeof(pin));
pin.sin_family = AF_INET;
// pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
pin.sin_addr.s_addr = inet_addr(hostname);
pin.sin_port = htons(PORT);
/* grab an Internet domain socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* connect to PORT on HOST */
if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
perror("connect");
exit(1);
}
//for()
/* send a message to the server PORT on machine HOST */
if (send(sd, argv[1], strlen(argv[1]), 0) == -1) {
perror("send");
exit(1);
}
/* wait for a message to come back from the server */
if (recv(sd, dir, DIRSIZE, 0) == -1) {
perror("recv");
exit(1);
}
printf("%s\n", dir);
if (send(sd, "send2", strlen("send2"), 0) == -1) {
perror("send");
exit(1);
}
/* wait for a message to come back from the server */
if (recv(sd, dir, DIRSIZE, 0) == -1) {
perror("recv");
exit(1);
}
if (send(sd, "EOM", strlen("EOM"), 0) == -1) {
perror("send");
exit(1);
}
/* spew-out the results and bail out of here! */
printf("%s\n", dir);
close(sd);
return 0;
}