基于c++实现的UDP client 源码
使用cmake 编译
其中接收和发送使用两个线程。在Ubuntu中已经验证可行
udpsession.cpp如下
#include"udpsession.h"
int skd=0;
udpsession::udpsession()
{
this->CreateSocket();
this->BingSocket();
}
udpsession::~udpsession()
{
}
bool udpsession::CreateSocket()
{
this->socketFd= socket(AF_INET,SOCK_DGRAM,0);//UDP方式
skd = this->socketFd;
printf("* this->socketFd :%d *\n",this->socketFd);
return this->socketFd;
}
bool udpsession::BingSocket()
{
/* bzero(&this->localAddr, sizeof(this->localAddr));
this->localAddr.sin_family = AF_INET;
this->localAddr.sin_addr.s_addr = inet_addr("10.100.86.26");
this->localAddr.sin_port = htons (50000);
printf("* this->socketFd :%d *\n",this->socketFd);
int bindResult = bind( this->socketFd, (struct sockaddr *) & this->localAddr, sizeof( this->localAddr));
if ( -1 == bindResult )
{
perror( "bind:" );
sleep(10);
close(this->socketFd);
exit(-1);
}
*/
// 服务器IP
bzero(& this->svrAddr, sizeof( this->svrAddr));
this->svrAddr.sin_family = AF_INET;
this->svrAddr.sin_addr.s_addr = inet_addr("10.100.86.26");
this->svrAddr.sin_port = htons (50000);
// char tempBuff[2048] = {0};
}
bool udpsession::Init()
{
/*
pthread_attr_t attr;
pthread_attr_t attr_freetp;
struct sched_param param;
struct sched_param param_ft;
pthread_t pthread_id = 0 ;
pthread_t pthread_ft_id = 0 ;
// print_software_info();
printf("* udpsession::Init() *\n");
pthread_attr_init (&attr);
pthread_attr_setschedpolicy (&attr, SCHED_RR);
param.sched_priority = APP_TSK_DEFAULT_PRIO;
pthread_attr_setschedparam (&attr, ¶m);
pthread_create (&pthread_id, &attr,&udpcli_UdpRecvthread, NULL);
pthread_attr_destroy (&attr);
pthread_attr_init (&attr_freetp);
pthread_attr_setschedpolicy (&attr_freetp, SCHED_RR);
param.sched_priority = 3;
pthread_attr_setschedparam (&attr_freetp, ¶m_ft);
pthread_create (&pthread_ft_id, &attr,&udpcli_UdpSendthread, NULL);
pthread_attr_destroy (&attr_freetp);
*/
}
void udpsession::UdpSendthread()
{
unsigned char tempBuff[124];
//udpsession & a = (udpsession*)arg;
printf("* UdpSendthread *\n");
printf("* skd:%d *\n",skd);
tempBuff[0]='a';
while(1)
{
// 发送数据
//
ssize_t result = sendto( this->socketFd, tempBuff, 1, 0, (struct sockaddr*)&this->svrAddr, sizeof(this->svrAddr));
if ( -1 == result )
{
perror("sendto:");
}
else
{
printf("send data success. data len:%d \n", result );
}
sleep(5);
}
}
void udpsession::UdpRecvthread()
{
struct sockaddr_in fromAddr;
unsigned int i;
unsigned char tempBuff[124];
printf("* UdpRecvthread *\n");
while(1)
{
printf("* UdpRecvthread *\n");
socklen_t fromLen = sizeof(this->svrAddr);
ssize_t result = recvfrom(this->socketFd, tempBuff, sizeof(tempBuff), 0, (struct sockaddr *)&this->svrAddr, &fromLen);
if ( -1 == result )
{
perror("recvfrom:");
}
else
{
printf( "recv data %s successed. data len: %d\n", inet_ntoa(fromAddr.sin_addr), result );
printf( "data:\n");
for ( int i = 0; i < result; i++ )
{
printf( "%02x ", tempBuff[i] );
if ( (i+1)%16 == 0 )
{
printf( "\n" );
}
}
printf("\n");
}
sleep(2);
}
close(this->socketFd);
}
udpsession.h如下
#pragma once
#include "sys/ipc.h"
#include "sys/msg.h"
#include <sys/types.h>
#include "pthread.h"
/*Socket*/
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/ip.h>
#include <sys/un.h>
#include <fcntl.h>
//#include "boost/asio.hpp"
//#include "boost/shared_ptr.hpp"
//#include "boost/weak_ptr.hpp"
//#include "boost/thread.hpp"
#include "map"
#include "boost/function.hpp"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define WM_RECVDATA WM_USER+500 //定义接收到数据发出的消息号
#define MAX_MSG_LEN 1500 //最大的消息长度(MTU)
#define DEFAULTPORT 1991 //定义默认服务端口
class udpsession;
struct RECVPARAM
{
udpsession* psocket;//
//HWND hwnd;//
};
class udpsession
{
public:
udpsession();
virtual ~udpsession();
public:
bool Init();
bool CreateSocket();//
bool BingSocket();//
bool Start(RECVPARAM* recvPar);
bool Stop();
bool GetSocket();//得到当前socket
std::string m_strData;//发出去的数据
bool m_bIsRun; //是否继续运行
u_long m_ulLocalIP;//本地IP
u_short m_usLocalPort;//本地端口
u_long m_ulRemoteIP;//远程IP
u_short m_usRemotePort;//远程端口
//发送数据
bool SendData(std::string strSend);
bool UdpSend();
private:
int socketFd;
struct sockaddr_in svrAddr;
struct sockaddr_in localAddr;
public:
//UDP接收线程
void UdpRecvthread();
void UdpSendthread();
};
main.cpp如下
#include"udpsession.h"
#include <sys/stat.h>
#include <signal.h>
#define APP_NAME "udptest"
#define APP_TSK_DEFAULT_PRIO 5
#define APP_NAME_VERSION 100
#define APP_PATH "/usr/bin/daemonconf"
void print_software_info(void)
{
char *modified_date = __DATE__;
struct stat info;
if (-1 == stat(APP_PATH, &info))
{
printf("unable to get %s file information! \n", APP_NAME);
}
printf("************ Software Information ************\n");
printf("* *\n");
printf("* App Name : %s *\n", APP_NAME);
printf("* Author : amplesky *\n");
printf("* Version : V%d.%d *\n", \
APP_NAME_VERSION / 100, APP_NAME_VERSION % 100);
printf("* File Size : %6ld *\n", info.st_size);
printf("* Modify Time : %s *\n", modified_date);
printf("* *\n");
printf("* Copyright (c) 2007-2016, amplesky Co., Ltd.*\n");
printf("* All rights reserved. *\n");
printf("**********************************************\n");
}
void *udpcli_UdpRecvthread(void *arg)
{
bool ret=true;
udpsession * a = (udpsession*)arg;
printf("udpcli_UdpRecvthread \n");
while(ret==true)
{
a->UdpRecvthread();
}
}
void *udpcli_UdpSendthread(void *arg)
{
bool ret=true;
printf("udpcli_UdpSendthread \n");
udpsession * a = (udpsession*)arg;
while(ret==true)
{
a->UdpSendthread();
}
}
bool flag1 = true;
bool flag2 = true;
void fun1(int arg)
{
flag1 = false;
}
void fun2(int arg)
{
flag2 = false;
}
int main(int argc, char* argv[])
{
signal(SIGINT,fun1);
signal(SIGTERM,fun2);
pthread_attr_t attr;
pthread_attr_t attr_freetp;
struct sched_param param;
struct sched_param param_ft;
pthread_t pthread_id = 0 ;
pthread_t pthread_ft_id = 0 ;
int ret;
void *retstat;
void *retstatu;
print_software_info();
udpsession* cc = new udpsession();
cc->Init();
printf("* udpsession::Init() *\n");
pthread_attr_init (&attr);
pthread_attr_setschedpolicy (&attr, SCHED_RR);
param.sched_priority = APP_TSK_DEFAULT_PRIO;
pthread_attr_setschedparam (&attr, ¶m);
ret=pthread_create (&pthread_id, &attr,&udpcli_UdpRecvthread, cc);
printf("thread creat result :%d ",ret);
pthread_attr_destroy (&attr);
pthread_attr_init (&attr_freetp);
pthread_attr_setschedpolicy (&attr_freetp, SCHED_RR);
param.sched_priority = APP_TSK_DEFAULT_PRIO;
pthread_attr_setschedparam (&attr_freetp, ¶m_ft);
pthread_create (&pthread_ft_id, &attr,&udpcli_UdpSendthread,cc );
pthread_attr_destroy (&attr_freetp);
while(flag1&&flag2)
{
sleep(1000);
}
delete cc;
return 0;
}
源码下载地址