-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.c
55 lines (46 loc) · 1.13 KB
/
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
//
// client.c
// simple_client
//
// Created by Injae Lee on 2014. 10. 10.
// Modified by Minsuk Lee,
// Copyright (c) 2014. Injae Lee All rights reserved.
// see LICENSE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define IP "127.0.0.1"
#define PORT 3000
#define WRITE_DATA "Hello World!"
int main()
{
int ret = -1;
int clientSock, wsize;
struct sockaddr_in serverAddr;
if ((clientSock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
goto leave;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(IP);
serverAddr.sin_port = htons(PORT);
if ((ret = connect(clientSock,(struct sockaddr*)&serverAddr,
sizeof(serverAddr)))) {
perror("connect");
goto error;
}
if ((wsize = send(clientSock, WRITE_DATA, sizeof(WRITE_DATA), 0)) <= 0) {
perror("send");
ret = -1;
} else {
printf("Wrote '%s' (%d Bytes)\n", WRITE_DATA, wsize);
ret = 0;
}
error:
close(clientSock);
leave:
return ret;
}