client.c

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

int sockfd;


int file_sign(int signum)
{
	close(sockfd);
}

void* read_cli(void* arg)
{
	int sockfd = *(int*) arg;
	char buf[1024] = {};
	for(;;)
	{
		printf("read:");
		read(sockfd,buf,sizeof(buf));
		printf("%s\n",buf);
	}
}
void* write_cli(void* arg)
{
	int sockfd = *(int*) arg;
	for(;;)
	{
		char buf[1024] = {};
		gets(buf);
		write(sockfd,buf,strlen(buf)+1);
		if(0 == strcmp("quit",buf))
		{
			printf("通信结束!\n");
			break;
		}
	}
}
int main()
{
	printf("服务器创建socket...\n");
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(7777);
	addr.sin_addr.s_addr = inet_addr("192.168.43.183");
	socklen_t len = sizeof(addr);

	printf("绑定连接服务器...\n");
	if(connect(sockfd,(struct sockaddr*)&addr,len))
	{
		perror("bind");
		return -1;
	}

	pthread_t gid1,gid2;
	void *str;
	pthread_create(&gid1,NULL,read_cli,&sockfd);
	pthread_join(gid2,&str);
			pthread_cancel(gid1);
			close(sockfd);
			return 0;
}

server.c

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


int fdt[20]={0};
char buf[1024] = {};

void sendtoclinet(char* buf)
{
	for(int i=0;i<20;i++)
	{
		if (0 != fdt[i])
		{
			write(fdt[i],buf,strlen(buf)+1);
			printf("发送消息至%d\n",i);
		}
	}
	return ;
}

void* startup(void* arg)
{
	int clifd = *(int*)arg;
	for(;;)
	{
		printf("read:");
		read(clifd,buf,sizeof(buf));
		printf("%s\n",buf);
		if(0 == strcmp("quit",buf))
		{
			for(int i=0;i<20;i++)
			{
				if (clifd == fdt[i])
				{
					fdt[i] = 0;
					close(clifd);
				}
			}
			return 0;
		}
		sendtoclinet(buf);
	}
}



int main()
{
	printf("服务器创建socket...\n");
	int sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	printf("准备地址...\n");
	struct sockaddr_in addr = {};
	addr.sin_family = AF_INET;
	addr.sin_port = htons(7777);
	addr.sin_addr.s_addr = inet_addr("192.168.43.183");
	socklen_t len = sizeof(addr);

	printf("绑定socket与地址...\n");
	if(bind(sockfd,(struct sockaddr*)&addr,len))
	{
		perror("bind");
		return -1;
	}

	printf("设置监听...\n");
	if(listen(sockfd,5))
	{
		perror("listen");
		return -1;
	}

	printf("等待客户端连接...\n");
	int num=0;
	for(;;)
	{
		struct sockaddr_in addrcli = {};

		int fd = accept(sockfd,(struct sockaddr*)&addrcli,&len);
		if(0 > fd)
		{
			perror("accept");
			continue;
		}

		for(int i=0;i<20;i++)
		{
			if (0 == fdt[i])
			{
				fdt[i] = fd;
				break;
			}
			if (19 ==i)
			{
				close(fd);
			}
		}

		pthread_t pid;
		pthread_create(&pid,NULL,startup,&fd);


/*		if(0 == fork())
		{
			char buf[1024] = {};
			for(;;)
			{
				printf("read:");
				read(clifd[num-1],buf,sizeof(buf));
				printf("%s\n",buf);
				if(0 == strcmp("quit",buf))
				{
					close(clifd[num-1]);
					clifd[num-1]=-1;
					return 0;
				}
				for(int i=0;i<20;i++)
				{
					if (-1 == clifd[i]) continue;
					write(clifd[i],buf,strlen(buf)+1);
					printf("%s %d\n",buf,i);
				}
			}
		}
		*/
	}
}
08-12 00:34