-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamLocalhostClient.c
73 lines (64 loc) · 1.38 KB
/
streamLocalhostClient.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
/*
* streamLocalhostClient.c
*/
// This program connects to a stream Internet socket
// over localhost
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#define PORT "50000"
#define IP_ADDR "localhost"
int main(int argc, char *argv[])
{
// create the socket
int sockFd = socket(AF_INET, SOCK_STREAM, 0);
if (sockFd < 0)
{
perror("socket");
exit(1);
}
struct addrinfo hints;
struct addrinfo *res;
int rc;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if((rc = getaddrinfo(IP_ADDR, PORT, &hints, &res)) != 0)
{
printf("getaddrinfo failed: %s\n", gai_strerror(rc));
exit(1);
}
// Connect to server
if(connect(sockFd, res->ai_addr, res->ai_addrlen) < 0)
{
perror("connect");
exit(1);
}
int bytesRead = 10;
char buf[100];
// read in from stdin and send
bytesRead = read(0, buf, 100);
if (bytesRead < 0)
{
perror("read");
exit(1);
}
int retVal = send(sockFd, buf, bytesRead, 0);
if (retVal < 0)
{
perror("send");
exit(1);
}
int ret = close(sockFd);
if (ret < 0)
{
perror("close");
exit(1);
}
}