大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
简介
UDP协议与TCP协议一样用于处理数据包,在OSI模型中,两者都位于传输层,处于IP协议的上一层。UDP有不提供数据包分组、组装和不能对数据包进行排序的缺点,也就是说,当报文发送之后,是无法得知其是否安全完整到达的。
代码实现
#ifndef _SOCKET_HPP_
#define _SOCKET_HPP_
#include <iostream>
#include <sstream>
#include <exception>
#include <string>
#include <stdlib.h>
#include <arpa/inet.h>
#define MAX_BUFFER 1024
using namespace std;
namespace Socket
{
typedef int Socket;
typedef string Ip;
typedef unsigned int Port;
typedef string Data;
typedef struct
{
Ip ip;
Port port;
}Address;
typedef struct
{
Address address;
Data data;
}Datagram;
struct sockaddr_in* to_sockaddr(Address* a)
{
struct sockaddr_in* ret;
ret=(struct sockaddr_in*) malloc (sizeof(struct sockaddr_in));
ret->sin_family = AF_INET;
inet_aton(a->ip.c_str(),&(ret->sin_addr));
ret->sin_port=htons(a->port);
return ret;
}
Address* from_sockaddr(struct sockaddr_in* address)
{
Address* ret;
ret=(Address*)malloc(sizeof(Address));
ret->ip = inet_ntoa(address->sin_addr);
ret->port = ntohs(address->sin_port);
return ret;
}
class Exception
{
private:
string _message;
public:
Exception(string error) {
this->_message = error; }
virtual const char* what() {
return this->_message.c_str(); }
};
class UDP
{
private:
Socket _socket_id;
bool _binded;
public:
UDP(void);
~UDP(void);
void close(void);
void bind(Port port);
void send(Ip ip, Port port, Data data);
Datagram receive();
};
UDP::UDP(void)
{
this->_socket_id = socket(AF_INET, SOCK_DGRAM, 0);
if (this->_socket_id == -1) throw Exception("[Constructor] Cannot create socket");
this->_binded = false;
}
UDP::~UDP(void)
{
}
void UDP::close(void)
{
shutdown(this->_socket_id, SHUT_RDWR);
}
void UDP::bind(Port port)
{
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr=htonl(INADDR_ANY);
address.sin_port=htons(port);
if (this->_binded)
{
this->close();
this->_socket_id = socket(AF_INET, SOCK_DGRAM, 0);
}
// ::bind() calls the function bind() from <arpa/inet.h> (outside the namespace)
if (::bind(this->_socket_id, (struct sockaddr*)&address, sizeof(struct sockaddr_in)) == -1)
{
stringstream error;
error << "[listen_on_port] with [port=" << port << "] Cannot bind socket";
throw Exception(error.str());
}
this->_binded = true;
}
void UDP::send(Ip ip, Port port, Data data)
{
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_aton(ip.c_str(), &address.sin_addr);
if (sendto(this->_socket_id, (void*)data.c_str(), data.length() + 1, 0, (struct sockaddr*)&address, sizeof(struct sockaddr_in)) == -1)
{
stringstream error;
error << "[send] with [ip=" << ip << "] [port=" << port << "] [data=" << data << "] Cannot send";
throw Exception(error.str());
}
}
Datagram UDP::receive()
{
int size = sizeof(struct sockaddr_in);
char *buffer = (char*)malloc(sizeof(char) * MAX_BUFFER);
struct sockaddr_in address;
Datagram ret;
if (recvfrom(this->_socket_id, (void*)buffer, MAX_BUFFER, 0, (struct sockaddr*)&address, (socklen_t*)&size) == -1) throw Exception("[receive] Cannot receive");
ret.data = buffer;
ret.address.ip = inet_ntoa(address.sin_addr);
ret.address.port = ntohs(address.sin_port);
free(buffer);
return ret;
}
}
#endif // _SOCKET_HPP_
使用示例
UDP server
#include <iostream>
#include "Socket.hpp"
using namespace std;
int main(void)
{
try
{
Socket::UDP sock;
sock.bind(2000);
Socket::Datagram received = sock.receive();
cout << received.data << endl;
sock.send("127.0.0.1", 3000, "response");
sock.close();
}
catch (Socket::Exception &e)
{
cout << e.what() << endl;
}
return 0;
}
UDP client
#include <iostream>
#include "Socket.hpp"
using namespace std;
int main(void)
{
try
{
Socket::UDP sock;
sock.bind(3000);
sock.send("127.0.0.1", 2000, "request");
Socket::Datagram received = sock.receive();
cout << received.data << endl;
sock.close();
}
catch (Socket::Exception &e)
{
cout << e.what() << endl;
}
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196172.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...