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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// <br>#g++ -o server.bin UdpSever.cpp -pthread<br> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <netinet/in.h>//for sockaddr_in #include <arpa/inet.h>//for socket #include <string.h> #include <errno.h> #include <pthread.h> #include <string> #if 0 #include "UdpProxy.h" #else class UdpProxy { public : UdpProxy(); virtual ~UdpProxy(); bool init(std::string strServerip, int nServerPost, int nLocalPort); void SendUdpMessagte(std::string strMessage); static void * thread_run( void *); protected : void OnReceive(std::string strMessage); bool DoIniteLocalSocket( int nLocalPort); bool DoIniteToSocket(std::string strServerip, int nServerPost); void destory_local(); void destory_to(); private : pthread_mutex_t m_mutex; int m_local_socket_fd; int m_to_socket_fd; int mnLocalPort; int mnToPort; struct sockaddr_in m_local_addr; struct sockaddr_in m_to_addr; std::string mstrToIp; pthread_t m_thread_run; }; #endif UdpProxy::UdpProxy() { mnLocalPort = -1; mnToPort = -1; m_local_socket_fd = -1; m_to_socket_fd = -1; m_thread_run = -1; mstrToIp = "" ; pthread_mutex_init(&m_mutex, NULL); } UdpProxy::~UdpProxy() { destory_local(); destory_to(); pthread_mutex_destroy(&m_mutex); } bool UdpProxy::init(std::string strServerip, int nServerPost, int nLocalPort) { pthread_mutex_lock(&m_mutex); bool bret = true ; printf ( "UdpProxy init %s %d %d \n" ,strServerip.c_str(),nServerPost,nLocalPort); bret &= DoIniteLocalSocket(nLocalPort); bret &= DoIniteToSocket(strServerip, nServerPost); pthread_mutex_unlock(&m_mutex); return bret; } void UdpProxy::SendUdpMessagte(std::string strMessage) { pthread_mutex_lock(&m_mutex); if (m_to_socket_fd == -1) { printf ( "SendUdpMessagte error send socket is -1 \n" ); if (mnToPort == -1 || mstrToIp == "" ) { printf ( "error! SendUdpMessagte has not set serverip and port ! \n" ); pthread_mutex_unlock(&m_mutex); return ; } if (DoIniteToSocket(mstrToIp, mnToPort) == false ) { printf ( "SendUdpMessagte error re inite socket failed! \n" ); pthread_mutex_unlock(&m_mutex); return ; } } int len = sendto(m_to_socket_fd, strMessage.c_str(), strMessage.length(), 0, ( struct sockaddr*)&m_to_addr, sizeof (m_to_addr)); if (len == -1) { printf ( "SendUdpMessagte error:%s\n" , strerror ( errno )); } else { printf ( "SendUdpMessagte %d bytes have been sended successfully!\n" , len); } pthread_mutex_unlock(&m_mutex); } void UdpProxy::OnReceive(std::string strMessage) { printf ( "OnReceive :%s \n" ,strMessage.c_str()); } bool UdpProxy::DoIniteLocalSocket( int nLocalPort) { if (nLocalPort != mnLocalPort) { printf ( "DoIniteLocalSocket \n" ); if (mnLocalPort != -1) destory_local(); mnLocalPort = nLocalPort; if (pthread_create(&m_thread_run, NULL, thread_run, ( void *) this ) != 0) //创建子线程 { perror ( "UdpProxy pthread_create thread_run failed!\n" ); } } return true ; } bool UdpProxy::DoIniteToSocket(std::string strServerip, int nServerPost) { bool bInitToSocket = false ; if (m_to_socket_fd == -1) { bInitToSocket = true ; } else if (nServerPost != mnToPort || mstrToIp != strServerip) { mstrToIp = strServerip; bInitToSocket = true ; close(m_to_socket_fd); } mnToPort = nServerPost; if (bInitToSocket) { m_to_socket_fd = -1; printf ( "DoIniteToSocket:%s %d\n" , strServerip.c_str(), mnToPort); m_to_socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if (m_to_socket_fd == -1) { perror ( "DoIniteToSocket socket create error!\n" ); return false ; } m_to_addr.sin_family = AF_INET; m_to_addr.sin_port = htons(nServerPost); m_to_addr.sin_addr.s_addr = inet_addr(strServerip.c_str()); } return true ; } void UdpProxy::destory_to() { if (m_to_socket_fd != -1) { close(m_to_socket_fd); m_to_socket_fd = -1; } mnToPort = -1; } void UdpProxy::destory_local() { if (m_local_socket_fd != -1) { close(m_local_socket_fd); m_local_socket_fd = -1; } if (m_thread_run != -1) { pthread_join(m_thread_run, NULL); } mnLocalPort = -1; } void * UdpProxy::thread_run( void * arg) { UdpProxy* pUdpProxyObj = (UdpProxy*)arg; int fd = socket(AF_INET, SOCK_DGRAM, 0); pUdpProxyObj->m_local_socket_fd = fd; if (fd == -1) { perror ( "socket create error!\n" ); exit (-1); } pUdpProxyObj->m_local_addr.sin_family = AF_INET; pUdpProxyObj->m_local_addr.sin_port = htons(pUdpProxyObj->mnLocalPort); pUdpProxyObj->m_local_addr.sin_addr.s_addr = inet_addr( "0.0.0.0" ); int r; r = bind(fd, ( struct sockaddr*)&pUdpProxyObj->m_local_addr, sizeof (m_local_addr)); if (r == -1) { printf ( "Bind error!\n" ); close(fd); exit (-1); } char buf[10 * 1024]; struct sockaddr_in from; socklen_t len; len = sizeof (from); while ( true ) { r = recvfrom(fd, buf, sizeof (buf), 0, ( struct sockaddr*)&from, &len); if (r > 0) { buf[r] = 0; pUdpProxyObj->OnReceive(buf); } else { printf ( "thread_run recv error:%s\n" , strerror ( errno )); break ; } } close(fd); } int main( int argc, char * argv[]) { UdpProxy obj; obj.init(argv[1], 6666, 6666); while ( true ) { obj.SendUdpMessagte( "hello ..." ); sleep(3); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现