• 大小: 414KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: 其他
  • 标签: 多线程  +udp  

资源简介

linux系统下建立多线程程序设计,完成UDP网络通信的发送与接收,包括总结与源代码,实测效果可见链接https://blog.csdn.net/zxp121127/article/details/78506081

资源截图

代码片段和文件信息

/*------------------------------------------------------------------------------------
* @filename: udpclient.c
* @author  : Zhou Xiangping
* @date    : 2-11-2017
* @versions: V0
* @brief   : linux系统下多线程网络(udp)编程,实现udp发送与接收可以同时进行
-------------------------------------------------------------------------------------*/
#include
#include
#include//多线程
#include
#include
#include   
#include   
#include  //以太网 
#include  //以太网
#include   //以太网
#include   
#include    
#include 

typedef void* (*fun)(void*);

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* udprecv(void*); //udp接收线程
pthread_t udpc_id;   //接收线程id
void* udpsend(void*); //udp发送线程
pthread_t udps_id;    //发送线程id

#define PORT 8081     //设置端口号
#define MAXDATASIZE 1024   //最大接受/发送数据容量
char ipaddr[16] = “192.168.211.4“;//回环调试模式
  //char ipaddr[16] = “192.168.211.85“;//回环调试模式

int sockfd = 0;
int sendCounter = 0;
int recvCounter = 0;
int sendflag = 0;
/*------------------------------------------------------------------------------------
* @filename: Can1_Receive_Msg(unsigned char *buf)
* @author  : Zhou Xiangping
* @input   : nothing
* @return  : nothing
* @date    : 2-11-2017
* @brief   : 在main函数中创建两个线程:udpsend and udprecv,并确保udpsend比udprecv先运行(先初始化套接字!)
* -------------------------------------------------------------------------------------*/
int main(int argc char *argv[])
{
printf(“the program is run!\n“);

if (argc == 2)//在终端输入服务器ip地址
{
while (1)
{
if (strlen(argv[1]) <= 15)
{
//ipaddr = argv[1];
strcpy(ipaddr argv[1]);
break;
}
else {
printf(“the ipaddr is nonconformity!please input correct ipaddr...\n“);
scanf(“%s“ argv[1]);
}
}
}
else {
printf(“Use the default ipaddr!\n“);
}
printf(“server ipaddr is:%s\n“ ipaddr);

int rc1 = 0 rc2 = 0;
rc1 = pthread_create(&udps_id NULL udpsend NULL);//创建udp发送线程
if (rc1 != 0)
{
printf(“pthread rc1 create failure!\n“);
exit(EXIT_FAILURE);
}
sleep(1);
rc2 = pthread_create(&udpc_id NULL udprecv NULL);//创建udp接收线程
if (rc2 != 0)
{
printf(“pthread rc2 create failure!\n“);
exit(EXIT_FAILURE);
}
while (1);
return 0;
}
/*------------------------------------------------------------------------------------
* @filename: void* udpsend(void* arg)
* @author  : Zhou Xiangping
* @input   : nothing
* @return  : nothing
* @date    : 2-11-2017
* @brief   : 完成UDP网络数据的发送(测试程序中需要手动输入)
* -------------------------------------------------------------------------------------*/
void* udpsend(void* arg)
{
char sendbuf[MAXDATASIZE];
struct hostent *target; //目标设备ip地址
struct sockaddr_in server;//创建ipv4协议的网络

target = gethostbyname(ipaddr);
if ((sockfd = socket(AF_INET SOCK_DGRAM 0)) == -1)// 创建套接字描述符
{
printf(“socket() error\n“);
exit(1);
}
//初始化套接字
bzero(&server si

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-11 13:07  linux多线程+UDP网络通信\
     文件      441578  2017-11-11 13:07  linux多线程+UDP网络通信\linux多线程+UDP网络通信总结.pdf
     文件       14936  2017-11-09 08:19  linux多线程+UDP网络通信\udpc
     文件        4766  2017-11-11 12:57  linux多线程+UDP网络通信\udpclient.c
     文件       14688  2017-11-09 07:33  linux多线程+UDP网络通信\udps
     文件        4947  2017-11-11 12:57  linux多线程+UDP网络通信\udpserver.c

评论

共有 条评论