-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagramLocalhostClient.c
66 lines (55 loc) · 1.29 KB
/
datagramLocalhostClient.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
/*
* datagramLocalhostClient.c
*/
// This program connects to a datagram 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 "127.0.0.1"
int main(int argc, char *argv[])
{
// create the socket
int sockFd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockFd < 0)
{
perror("socket");
exit(1);
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(atoi(PORT));
inet_pton(AF_INET, IP_ADDR, &servaddr.sin_addr);
struct sockaddr *ptr = (struct sockaddr *) &servaddr;
int size = sizeof(struct sockaddr);
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 = sendto(sockFd, buf, bytesRead, 0, ptr, size);
if (retVal < 0)
{
perror("sendto");
exit(1);
}
int ret = close(sockFd);
if (ret < 0)
{
perror("close");
exit(1);
}
}