Repository navigation
Expand file tree
/
Copy pathclient.cpp
More file actions
85 lines (60 loc) · 1.74 KB
/
Copy pathclient.cpp
File metadata and controls
85 lines (60 loc) · 1.74 KB
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
/*
Simple udp client
Silver Moon ([email protected])
*/
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#define SERVER "127.0.0.1"
#define BUFLEN 10000 //Max length of buffer
#define PORT 1234 //The port on which to send data
#include <time.h>
int UdpSend(const std::string& host, int port, const char* buffer, size_t length, size_t* sendedLength);
int main(void)
{
srand(time(NULL));
int check = 0;
vector<char> buffer(BUFLEN);
for(size_t i=0; i<buffer.size(); ++i){
buffer[i] = rand();
check ^= buffer[i];
}
cout<<"check="<<check<<endl;
size_t sended = 0;
int errorCode = UdpSend(SERVER, PORT, &(buffer[0]), buffer.size(), &sended);
if(errorCode){
std::cout<<"errorCode="<<errorCode<<std::endl;
}
std::cout<<"sendedLength="<<sended<<std::endl;
return 0;
}
int UdpSend(const std::string& host, int port, const char* buffer, size_t length, size_t* sendedLength){
*sendedLength = 0;
int socketFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( socketFd == -1) {
return __LINE__;
}
struct FdAutoClose{ int fd; FdAutoClose(int r_fd){fd=r_fd;} ~FdAutoClose(){close(this->fd);}}fdAutoClose(socketFd);
struct sockaddr_in addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
int atonResult = inet_aton(host.c_str() , &addr.sin_addr);
if ( atonResult == 0) {
return __LINE__;
}
int sendToRet = sendto(socketFd, &(buffer[0]), length, 0 , (struct sockaddr *) &addr, sizeof(addr));
if( sendToRet < 0){
return sendToRet;
}
*sendedLength = sendToRet;
return 0;
}