-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleUDPClient.cpp
More file actions
34 lines (30 loc) · 843 Bytes
/
SimpleUDPClient.cpp
File metadata and controls
34 lines (30 loc) · 843 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
using namespace std;
int main() {
const char* myWords = "hello world!";
int sock = socket(PF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
fprintf(stderr, "-ERR Cannot open socket\n");
exit(1);
}
struct sockaddr_in dest;
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(10000);
inet_pton(AF_INET, "127.0.0.1", &(dest.sin_addr));
sendto(sock, myWords, strlen(myWords), 0, (struct sockaddr*)&dest, sizeof(dest));
char buf[100];
struct sockaddr_in src;
socklen_t srcSize = sizeof(src);
int rlen = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&src, &srcSize);
buf[rlen] = 0;
printf("%s", buf);
close(sock);
return 0;
}