Linux-C 聊天程序学习(socket+pthread)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33850438/article/details/52058368

主要学习socket编程。

实现client跟server随意聊天(TCP协议)。只允许一个server跟一个client 聊天,不能群聊

利用多线程实现,聊天双方各创建两个线程:实现发送和接受消息。

(代码都有详细注释)。


client(sock.c):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

void *recvsocket(void *arg)//接收server端socket数据的线程
{
	int st = *(int *)arg;
	char s[1024];

	while(1)
	{
		memset(s, 0, sizeof(s));
		int rc = recv(st, s, sizeof(s), 0);
		if (rc <= 0)//如果recv返回小于等于0,代表socket已经关闭或者出错了
			break;
		printf("server:%s", s);

	}
	return NULL;
}

void *sendsocket(void *arg)//向server端socket发送数据的线程
{
	int st = *(int *)arg;
	char s[1024];
	while(1)
	{
		memset(s, 0, sizeof(s));
		read(STDIN_FILENO, s, sizeof(s));//从键盘读取用户输入信息
		send(st, s, strlen(s), 0);
	}
	return NULL;
}

int main(int arg, char *args[])
{
	if (arg < 3)
		return -1;

	int port = atoi(args[2]);
	int st = socket(AF_INET, SOCK_STREAM, 0); //初始化socket,

	struct sockaddr_in addr; //定义一个IP地址的结构
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET; //设置结构地址类型为TCP/IP地址
	addr.sin_port = htons(port); //指定一个端口号:8080,htons:将short类型从host字节类型到net字节类型转化
	addr.sin_addr.s_addr = inet_addr(args[1]); //将字符串类型的IP地址转化为int,赋给addr结构成员.

	//调用connect连接到结构addr指定的IP地址和端口号
	if (connect(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
	{
		printf("connect failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	pthread_t thrd1, thrd2;
	pthread_create(&thrd1, NULL, recvsocket, &st);
	pthread_create(&thrd2, NULL, sendsocket, &st);
	pthread_join(thrd1, NULL);
	//pthread_join(thrd2, NULL);
	close(st); //关闭socket
	return EXIT_SUCCESS;

}


server:(server.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>


struct ps
{
	int st;
	pthread_t *thr;
};
//静态多线程初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//只能开启一个client
int status = 0;

void *recvsocket(void *arg)//接收client端socket数据的线程
{
	struct ps *p = (struct ps *)arg;
	int st = p->st;
	char s[1024];

	while(1)
	{
		memset(s, 0, sizeof(s));
		int rc = recv(st, s, sizeof(s), 0);
		if (rc <= 0)//如果recv返回小于等于0,代表socket已经关闭或者出错了
			break;
		printf("client:%s", s);

	}
	pthread_mutex_lock(&mutex);
	status = 0;
	pthread_mutex_unlock(&mutex);
	pthread_cancel(*(p->thr));//被cancel掉的线程内部没有使用锁。
	return NULL;
}

void *sendsocket(void *arg)//向client端socket发送数据的线程
{
	int st = *(int *)arg;
	char s[1024];
	while(1)
	{
		memset(s, 0, sizeof(s));
		read(STDIN_FILENO, s, sizeof(s));//从键盘读取用户输入信息
		send(st, s, strlen(s), 0);
	}
	return NULL;
}

int main(int arg, char *args[])
{
	if (arg < 2)
	{
		return -1;
	}

	int port = atoi(args[1]);
	int st = socket(AF_INET, SOCK_STREAM, 0);//初始化socket
	int on = 1;
         //IP可重用,关掉程序还能启动同个IP聊天
	if (setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
	{
		printf("setsockopt failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}


	struct sockaddr_in addr;     //定义一个IP地址结构
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;//将addr结构的属性定位为TCP/IP地址
	addr.sin_port = htons(port);//将本地字节顺序转化为网络字节顺序。
	addr.sin_addr.s_addr = htonl(INADDR_ANY);//INADDR_ANY代表这个server上所有的地址

	//将IP与server程序绑定
	if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
	{
		printf("bind failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	//server端开始listen,
	if (listen(st, 20) == -1)
	{
		printf("listen failed %s\n", strerror(errno));
		return EXIT_FAILURE;
	}
	int client_st = 0;      //client端socket
	struct sockaddr_in client_addr;//表示client端的IP地址

	pthread_t thrd1, thrd2;

	while (1)
	{
		memset(&client_addr, 0, sizeof(client_addr));
		socklen_t len = sizeof(client_addr);
		//accept会阻塞,直到有客户端连接过来,accept返回client的socket描述符
		client_st = accept(st, (struct sockaddr *)&client_addr , &len);
		pthread_mutex_lock(&mutex);//为全局变量加一个互斥锁,防止与线程函数同时读写变量的冲突
		status++;
		pthread_mutex_unlock(&mutex);//解锁
		if (status > 1)//代表这是第二个socket连接
		{
			close(client_st);
			continue;
		}
		if (client_st == -1)
		{
			printf("accept failed %s\n", strerror(errno));
			return EXIT_FAILURE;
		}

		printf("accept by %s\n", inet_ntoa(client_addr.sin_addr));
		struct ps ps1;
		ps1.st = client_st;
		ps1.thr = &thrd2;
		pthread_create(&thrd1, NULL, recvsocket, &ps1);
		pthread_detach(thrd1);//设置线程为可分离
		pthread_create(&thrd2, NULL, sendsocket, &client_st);
		pthread_detach(thrd2);//设置线程为可分离
	}
	close(st);//关闭server端listen的socket
	return EXIT_SUCCESS;
}

运行结果:



注意点:(不小心server用了自己socket出来的描述符跟client收发消息,结果调试了好一会)

/*
 * 
 * server accept 返回client descriptor(client 描述符)
 * server recv和send都是通过这个描述符
 *
 * client socket得到的描述符号用于recv和send
 *
 * */

多线程另外一个综合例子学习:http://blog.csdn.net/qq_33850438/article/details/52038708
socket另外一个综合例子学习:http://blog.csdn.net/qq_33850438/article/details/52055390



展开阅读全文

没有更多推荐了,返回首页