-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTCPclient.c
58 lines (46 loc) · 1.22 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
55
56
57
58
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int n, s, len;
char buf[1024];
char hostname[64];
struct hostent *hp;
struct sockaddr_in name;
unsigned int portNum;
switch(argc) {
case 3:
portNum = atoi(argv[2]);
strcpy(hostname, argv[1]);
break;
default:
printf("Usage: %s [server_name] [port_num]\n",argv[0]);
exit(-1);
}
/* Look up our host's network address.*/
hp = gethostbyname(hostname);
/* Create a socket in the INET domain.*/
s = socket(AF_INET, SOCK_STREAM, 0);
/* Create the address of the server. */
name.sin_family = AF_INET;
name.sin_port = htons(portNum);
memcpy(&name.sin_addr, hp->h_addr_list[0], hp->h_length);
len = sizeof(struct sockaddr_in);
/* Connect to the server. */
connect(s, (struct sockaddr *) &name, len);
/* Read from standard input, and copy the
* data to the socket. */
while ((n = read(0, buf, sizeof(buf))) > 0) {
if (send(s, buf, n, 0) < 0) {
perror("send");
exit(1);
}
}
close(s);
exit(0);
}