并发网络编程-进程和线程

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

#define  PORT   9998

void handl_client(int client_socket)
{
	char buf[1024] = {"asdsadsadsadsa"};
	
	while (1)
	{
		// 读-1 sizeof() 打印
		int ret = read(client_socket, buf, sizeof(buf)-1);
		
		if (-1 == ret)
		{
			perror ("read error");
		}
		
		if (0 == ret)
		{
			printf ("客户端退出\n");
			
			break;
		}
		
		buf[ret] = '\0';
		printf ("收到客户端数据:%s\n", buf);
		int i;
		
		for (i = 0; i < ret-1; i++)
		{
			buf[i] += 'A' - 'a';
		}
		
		// 将数据(小写转大写)回写到客户端,相当于回复
		write(client_socket, buf, ret);
	}	
}

// 监听套接字
int init()
{
	// 创建 绑定 连接
	int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
	
	if(-1 == listen_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地址*/

	// 2、绑定本地的 ip 地址和端口
	// 第一个参数:需要绑定的套接字
	// 第二个参数:是要绑定的ip和端口
	// 第三个参数:第二个参数所占的字节数
	int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
	
	if(-1 == ret)
	{
		perror("绑定失败");
		
		return -1;
	}
	
	// 3、监听套接字
	ret = listen(listen_socket, 5);
	
	if(-1 == ret)
	{
		perror("监听失败");
		return -1;
	}
	
	return listen_socket;
	
}

// 通信套接字
int myAccept(int listen_socket)
{
	struct sockaddr_in client_addr;
	socklen_t  len = sizeof(client_addr);
	
	// 阻塞型函数
	int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
	
	if (-1 == client_socket)
	{
		perror("accept  失败");
		return -1;
	}
	
	printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
	
	return client_socket;
}

// 主函数
// 用多进程实现可多客户端访问的服务器
int main(int argc, char **argv)
{
	// 创建套接字、绑定本地、监听/处理连接请求/建立连接
	int listen_socket = init();
	
	if(-1 == listen_socket)
	{
		perror("创建套接字失败");
		
		return -1;
	}
	
	while(1)
	{
		// 进行通信,函数调用里的accept为阻塞型函数,无客户端连接时一直等待
		int client_socket = myAccept(listen_socket);
		
		// 创建子进程为客户端服务
		pid_t pid = fork();
		
		switch(pid)
		{
			case -1:
				perror("fork失败");
				break;
			case 0:
				close(listen_socket);
				
				// 把通信套接字传过去
				handl_client(client_socket);
				close(client_socket);
				break;
			default:
				close(client_socket);
				break;
		}
	}
	
	close(listen_socket);
	
	return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define PORT 9997

// 收到数据,然后显示
// 将收到的数据发回
void *handl_client(void *v)
{
	int client_socket = (long)v;
	
	char buf[1024] = {"asdsadsadsadsa"};
	
	while (1)
	{
		// read的第一个参数是int型
		int ret = read(client_socket, buf, sizeof(buf)-1);
		
		if (-1 == ret)
		{
			perror ("read error");
		}
		
		if (0 == ret)
		{
			printf ("客户端退出\n");
			
			break;
		}
		
		buf[ret] = '\0';
		printf ("收到客户端的数据:%s\n", buf);
		
		int i;
		
		for (i = 0; i < ret - 1; i++)
		{
			buf[i] += 'A' - 'a';
		}
		
		write(client_socket, buf, ret);
	}	
}

// 监听套接字
int init()
{
	int listen_socket = socket(AF_INET, SOCK_STREAM, 0);
	
	if(-1 == listen_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地址*/

	// 2、绑定本地的 ip 地址和端口
	// 第一个参数:需要绑定的套接字
	// 第二个参数:是要绑定的ip和端口
	// 第三个参数:第二个参数所占的字节数
	int ret = bind(listen_socket, (const struct sockaddr *)&addr, sizeof(addr));
	
	if(-1 == ret)
	{
		perror("绑定失败");
		
		return -1;
	}
	
	// 3、监听套接字
	ret = listen(listen_socket, 5);
	
	if(-1 == ret)
	{
		perror("监听失败");
		return -1;
	}
	
	return listen_socket;
	
}

// 通信套接字
int myAccept(int listen_socket)
{
	struct sockaddr_in client_addr;
	socklen_t  len = sizeof(client_addr);
	int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);
	
	if (-1 == client_socket)
	{
		perror("accept  失败");
		return -1;
	}
	
	printf ("客户端的 ip = %s, 端口 = %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
	
	return client_socket;
}

// 主函数
// 用线程创建可多客户端访问的服务器
int main(int argc, char **argv)
{
	// 创建 绑定 连接
	int listen_socket = init();
	
	if(-1 == listen_socket)
	{
		perror("创建套接字失败");
		
		return -1;
	}
	
	while(1)
	{
		// 因为在pthread_socket中的第四个参数是个地址,故需要将client_socket定位8字节
		long client_socket = myAccept(listen_socket);
		
		if (-1 == client_socket)
		{
			continue;
		}
		
		// 开辟线程为客户端服务
		pthread_t thread;
		
		// int ret = pthread_create(&thread, NULL, worker, (void *)120);
		pthread_create(&thread, NULL, handl_client, (long *)client_socket);
		
		// 线程分离后,该主线程还在while(1)等待监听套接字建立新的连接 
		pthread_detach(thread);
	}
	
	close(listen_socket);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值