网络编程(UDP)

一.UDP服务器

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define  PORT   9998

void handl_client(char *buf)
{
	int i;
	for (i = 0; i < strlen(buf)-1; i++)
	{
		buf[i] += 'A' - 'a';
	}
}

// 监听套接字
int init()
{
	int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == conm_socket)
	{
		perror("创建套接字失败");
		return -1;
	}
	
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(struct sockaddr_in));
	addr.sin_family      = AF_INET;             /* Internet地址族 */
	addr.sin_port        = htons(PORT);         /* 端口号 */
	addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* IP地址, 绑定本地的所有ip地址*/

	int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
	if(-1 == ret)
	{
		perror("绑定失败");
		return -1;
	}
	
	return conm_socket;
}

int main(int argc, char **argv)
{
	int conm_socket = init();
	if (-1 == conm_socket)
		return;
	
	char buf[1024];
	while (1)
	{
		struct sockaddr_in client_addr;
		socklen_t  len = sizeof(client_addr);
		ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
		buf[ret] = '\0';
		handl_client(buf);     // 处理
		
		sendto(conm_socket, buf, ret, 0, (struct sockaddr *)&client_addr,  len);// 回
	}
	return 0;
}

二.UDP客户端

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
 #include <netinet/in.h>

// 子线程负责写数据
void *write_data(void *v)
{
	int conm_socket = *(int*)v;
	
	char buf[1024];
	while (1)
	{
		struct sockaddr_in client_addr;
		socklen_t  len = sizeof(client_addr);
		ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
		if (-1 == ret)
		{
			perror ("read error");
		}
		if (0 == ret)
		{
			printf ("客户端退出\n");
			break;
		}
		buf[ret] = '\0';
		printf ("%s\n", buf);
	}	
}

// 主线程负责读数据
void read_data(int conm_socket, char **argv)
{
	// 接收方的地址
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(struct sockaddr_in));
	addr.sin_family      = AF_INET;                      /* Internet地址族 */
	addr.sin_port        = htons(atoi(argv[2]));         /*服务器用的端口号 */
	addr.sin_addr.s_addr = inet_addr(argv[1]);           /*服务器用的IP地址 */
	char buf[1024];
	while (1)
	{
		fgets(buf, 1024, stdin);
		sendto(conm_socket, buf, strlen(buf), 0, (struct sockaddr *)&addr,  sizeof(addr));
	}		
}

int main(int argc, char **argv)
{
	// 1、创建套接字
	int conn_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == conn_socket)
	{
		perror("创建套接字失败");
		return -1;
	}
	
	pthread_t thread;
	pthread_create(&thread, NULL, write_data, (void *)&conn_socket);
	read_data(conn_socket, argv);
	close(conn_socket);
	
	return 0;
}

三.并发

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define  PORT   9998

int conm_socket; 

typedef struct _msg
{
	char buf[256];
	struct sockaddr_in client_addr;
}Msg;

Msg task[100];   // 任务
int front = 0;
int rear  = 0;


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  // 互斥锁
pthread_cond_t cond1;    // 条件变量


void handl_client(Msg *data)
{
	int i;
	for (i = 0; i < strlen(data->buf)-1; i++)
	{
		data->buf[i] += 'A' - 'a';
	}
	
	sendto(conm_socket, data->buf, strlen(data->buf), 0, (struct sockaddr *)&(data->client_addr),  sizeof(data->client_addr));// 回
}


// 监听套接字
int init()
{
	int conm_socket = socket(AF_INET, SOCK_DGRAM, 0);
	if(-1 == conm_socket)
	{
		perror("创建套接字失败");
		return -1;
	}
	
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(struct sockaddr_in));
	addr.sin_family      = AF_INET;             /* Internet地址族 */
	addr.sin_port        = htons(PORT);         /* 端口号 */
	addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* IP地址, 绑定本地的所有ip地址*/

	int ret = bind(conm_socket, (const struct sockaddr *)&addr, sizeof(addr));
	if(-1 == ret)
	{
		perror("绑定失败");
		return -1;
	}
	
	return conm_socket;
}

void *consume(void *v)
{
	while (1)
	{
		Msg data;
		pthread_mutex_lock(&mutex);
		while (rear == front)//空队列
		{
			pthread_cond_wait(&cond1, &mutex);
		}
		
		front = (front+1)%20;
		data = task[front];
		
		pthread_mutex_unlock(&mutex);
		
		handl_client(&data);
	}
}

void  produce()
{
	char buf[1024];
	while (1)
	{
		struct sockaddr_in client_addr;
		socklen_t  len = sizeof(client_addr);
		ssize_t ret = recvfrom(conm_socket, buf, 1023, 0, (struct sockaddr *)&client_addr, &len);// 收
		buf[ret] = '\0';
		
		printf ("[%s:%d] %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
		
		pthread_mutex_lock(&mutex);
		
		if ((rear+1)%20 == front)
			continue;
		
		rear = (rear+1)%20;
		strcpy(task[rear].buf, buf);
		task[rear].client_addr = client_addr;
		
		pthread_cond_broadcast(&cond1);     // 广播
		pthread_mutex_unlock(&mutex);	
	}
}

int main(int argc, char **argv)
{
	long i;
	pthread_t thread;
	for (i = 0; i < 8; i++)
	{
		pthread_create(&thread, NULL, consume, (void*)(i+1));
		pthread_detach(thread);
	}
	
	pthread_cond_init(&cond1, NULL);
	
	conm_socket = init();
	if (-1 == conm_socket)
		return;
	
	produce();
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值