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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#ifndef SUDP_HPP #define SUDP_HPP
#include <stdio.h> #include <string> #include <WS2tcpip.h>
#pragma comment (lib, "ws2_32.lib")
class UdpCom { private: SOCKET m_in; sockaddr_in m_local; const char* m_local_ip; int m_local_port; int m_local_length; sockaddr_in m_dest; const char* m_dest_ip; int m_dest_port; int m_dest_length; char m_buffer[4096]; public:
UdpCom() { m_in = NULL; m_local = sockaddr_in(); m_local_length = sizeof(m_local); m_local_ip = "127.0.0.1"; m_local_port = 0; m_dest = sockaddr_in(); m_dest_length = sizeof(m_dest); m_dest_ip = "0.0.0.0"; m_dest_port = 0; memset(m_buffer, 0, sizeof(m_buffer)); ZeroMemory(&m_dest, sizeof(m_dest)); }
~UdpCom() {Close();}
int Open() { int return_code = 0; WSADATA data; WORD version = MAKEWORD(2, 2); int wsOK = WSAStartup(version, &data); if (wsOK != 0) { printf("can't start Winsock. Error: %d.\n", wsOK); return_code = 1; } m_in = socket(AF_INET, SOCK_DGRAM, 0); m_local.sin_family = AF_INET; m_local.sin_port = htons(m_local_port); inet_pton(AF_INET, m_local_ip, &m_local.sin_addr); if (bind(m_in, (sockaddr*)&m_local, sizeof(m_local)) == SOCKET_ERROR) { printf("Can't bind socket! Error: %d.\n", WSAGetLastError()); return_code = 2; } m_dest.sin_family = AF_INET; inet_pton(AF_INET, m_dest_ip, &m_dest.sin_addr); m_dest.sin_port = htons(m_dest_port); printf("UDP is open.\n"); return return_code; }
int Open(const char* local_ip, const int local_port, const char* dest_ip, const int dest_port) { SetLocal(local_ip, local_port); SetDest(dest_ip, dest_port); Open(); return 0; }
int Receive() { ZeroMemory(m_buffer, 4096); int byte_in = recvfrom(m_in, m_buffer, 4096, 0, (sockaddr*)&m_dest, &m_dest_length);
if (byte_in == SOCKET_ERROR) { printf("Error receiving from client. Error code: %d", WSAGetLastError()); } printf("%s \n",m_buffer); return 0; }
int Send(char* message_send, int len) { int return_code = 0; int send_ok = sendto(m_in, message_send, len, 0, (sockaddr*)&m_dest, sizeof(m_dest)); if (send_ok == SOCKET_ERROR) { printf("Send error.\n"); return_code = 1; } else { }
return return_code; }
int Close() { closesocket(m_in); WSACleanup(); return 0; }
int SetDest(const char* dest_ip, const int dest_port) { m_dest_ip = dest_ip; m_dest_port = dest_port; return 0; }
int SetLocal(const char* local_ip, const int local_port) { m_local_ip = local_ip; m_local_port = local_port; return 0; }
char* GetContent() { return m_buffer; } };
#endif
|