-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
55 lines (47 loc) · 942 Bytes
/
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define DATA "Hello World of socket"
int main (int argc, char *argv[])
{
int sock;
struct sockaddr_in server;
struct hostent *hp;
char buff[1024];
sock = socket (AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket failed");
exit(1);
}
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if(hp == 0)
{
perror("gethostbyname failed");
close(sock);
exit(1);
}
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1026);
if(connect(sock, (struct sockaddr *) &server, sizeof (server))< 0)
{
perror("connect failed");
close(sock);
exit(1);
}
if(send(sock, DATA, sizeof(DATA), 0) < 0)
{
perror("sent failed");
close(sock);
exit(1);
}
printf("Sent %s\n", DATA);
close(sock);
return 0;
}