-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv_udp_client.cpp
136 lines (111 loc) · 2.86 KB
/
adv_udp_client.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Advance UDP Client */
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <sys/wait.h>
#include <cstdlib>
#include <string>
#include <vector>
#include <cpuid.h>
#include <syslog.h>
#include <sys/uio.h>
#include <sys/event.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <poll.h>
#include <pthread.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/route.h>
using namespace std;
struct hdr{
int seq;
struct sockaddr_in sa;
};
int send_msg(int sockfd, struct sockaddr_in cliaddr){
int n;
struct msghdr msg = {};
struct iovec iov[2];
struct hdr h, h1;
h.seq = 1;
h.sa = cliaddr;
struct sockaddr_in sa;
char buf[100] = "This is the test message";
char *hostname = "<your_ip_address>";
struct hostent *host;
host = gethostbyname(hostname);
if(host == NULL){
perror("hostent");
exit(1);
}
sa.sin_addr = *(struct in_addr*)host->h_addr;
sa.sin_family = AF_INET;
int port = 3456;
sa.sin_port = htons(port);
iov[0].iov_base = &h;
iov[0].iov_len = sizeof(h);
iov[1].iov_base = buf;
iov[1].iov_len = sizeof(buf);
msg.msg_name = &sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_flags = 0;
// struct msghdr recv_msg = {};
struct iovec recv_iov[1];
recv_iov[0].iov_base = &h1;
recv_iov[0].iov_len = sizeof(h1);
msghdr recv_msg = {NULL,0,recv_iov,1,0,0,0};
sendagain:
n = sendmsg(sockfd,&msg,0);
if(n == -1){
perror("client - sendmsg");
return 0;
}
cout << "Message has been sent" << endl;
cout << "Waiting for ACK..." << endl;
n = recvmsg(sockfd,&recv_msg,0); // *** This is blocking. Use fcntl to create non-blocking socket or other method
if(n == -1){
perror("client - recvmsg");
return 0;
}
if(h.seq == h1.seq){
cout << "ACK received";
return 0;
}
else{
cout << "Wrong ACK" << endl;
goto sendagain;
}
return 0;
}
int create_client(){
int sockfd;
struct sockaddr_in cliaddr;
int port = 3456;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
memset(&cliaddr, 0, sizeof(cliaddr));
cliaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
cout << cliaddr.sin_addr.s_addr << endl;
cliaddr.sin_port = htons(port);
cliaddr.sin_family = AF_INET;
if(bind(sockfd,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1){
perror("bind");
}
send_msg(sockfd,cliaddr);
return 0;
}
int main(){
int x = create_client();
return 0;
}