-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPclient.cpp
41 lines (38 loc) · 1.19 KB
/
UDPclient.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
/*************************************************************************
> File Name: UDPclient.cpp
> Author: Soong
> Mail:[email protected]
> Created Time: 2014年05月06日 星期二 22时38分53秒
************************************************************************/
#include "./../include/UDPclient.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
UDPclient::UDPclient(char *ip, char *port)
{
_server_addr.sin_family = AF_INET;
_server_addr.sin_port = htons(atoi(port));
_server_addr.sin_addr.s_addr = inet_addr(ip);
_server_fd = socket(AF_INET, SOCK_DGRAM, 0);
}
UDPclient::~UDPclient(){
}
void UDPclient::start()
{
char send_buf[128] = "" , recv_buf[128] = "";
struct sockaddr_in from_addr;
socklen_t addr_len;
while(std::cin >> send_buf)
{
addr_len = sizeof(struct sockaddr);
bzero(&from_addr, sizeof(from_addr));
sendto(_server_fd, send_buf, strlen(send_buf), 0, (struct sockaddr*)&_server_addr, sizeof(struct sockaddr));
bzero(recv_buf, 128);
recvfrom(_server_fd, recv_buf, 128, 0, (struct sockaddr*)&from_addr, &addr_len);
std::cout << recv_buf << std::endl;
bzero(recv_buf, 128);
}
}