【linux网络编程学习笔记】第四节:创建UDP通信(双向)(sendto、recvfrom、inet_aton、inet_ntoa、inet_addr、inet_pton、inet_ntop...)

本文介绍了在Linux环境中,如何使用C/C++实现UDP的双向通信。内容涵盖关键函数如sendto、recvfrom、inet_aton、inet_ntoa、inet_addr、inet_pton和inet_ntop的使用,强调在相同主机上运行时需要注意端口号的区分。

Nurture passes nature.

  教养胜过天性

      udp的双向通信和TCP的大同小异,都是通过多线程进行收发,不同的就是本次例程只写了一份代码,如果在同一台虚拟机上跑一定要区分端口号。

直接上例程

udp_rdwr.c

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <strings.h>
#include <unistd.h>
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

struct msg_info{
	char name[256];
	char msg[1024];
};

void *recv_thread(void *arg)
{
	int udp_fd = *(int *)arg;
	ssize_t recv_size;
	struct msg_info msg;
	struct sockaddr_in recv_addr;
	socklen_t skt_len = sizeof(struct sockaddr_in);

	while(1)
	{
		bzero(&msg, sizeof(msg));

		recv_size = recvfrom
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值