linux-udp-客户端服务器结合体
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

#g++ -o server.bin UdpSever.cpp  -pthread
#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; }

  

posted on 2022-07-19 15:12  DuoRuaiMi4567  阅读(14)  评论(0编辑  收藏  举报