Linux-based operating system,a network chat room design and implementation

//-------------client.c------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h> 
#include <sys/socket.h>
#include <unistd.h>



#define TRUE 1
#define PORT 6555
 
static int sockfd;
void recvfromserver()        //接受服务器消息线程入口函数
{
	char mes[1024];
	int nbytes=0;

	while(1)
	{
		memset(mes,0,sizeof(mes));
		nbytes=read(sockfd,mes,sizeof(mes));
		if(nbytes>0)
		{
			mes[nbytes]='\0';
			printf("%s\n",mes);
		}
	}
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
//	int sockfd;
	char buffer[1024];
	struct sockaddr_in server_addr;
	struct hostent *host;
	int portnumber,nbytes;
	char strhost[16];
	char clientname[20];
	char mes[1024];
	int        thr_id;         /* thread ID for the newly created thread */
	pthread_t  p_thread;       /* thread's structure                     */
	if(argc!=1)
	{
		fprintf(stderr,"Usage:%s \a\n",argv[0]);
		exit(1);
	}
	printf("请输入服务器ip地址\n");
	//scanf("%s",strhost);
	if((host=gethostbyname("127.0.0.1"))==NULL)
	{
		fprintf(stderr,"Gethostname error\n");
		exit(1);
	}
	/* 客户程序开始建立 sockfd 描述符 */
	printf("正在建立套接口...\n");
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
		exit(1);
	}
	/* 客户程序填充服务端的资料 */
	bzero(&server_addr,sizeof(server_addr));
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(PORT);
	server_addr.sin_addr=*((struct in_addr *)host->h_addr);
	printf("套接口创建成功,正在链接服务器...\n");
	/* 客户程序发起连接请求 */

	if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
	{
		fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
		exit(1);
	}
	/* 连接成功了 */
	printf("链接服务器成功\n欢迎来到聊天室\n");
	printf("请输入你的用户昵称\n");
	scanf("%s",clientname);
//	write(sockfd,clientname,sizeof(clientname));

	printf("\n\n开始聊天吧(\"Quit\"断开连接)\n\n");


	thr_id = pthread_create(&p_thread, NULL, recvfromserver, NULL);
	while(1)
	{
		memset(buffer,0,sizeof(buffer));
		memset(mes,0,sizeof(mes));
		gets(buffer);	
		strcat(mes,clientname);
		strcat(mes,":");
		strcat(mes,buffer);
	//	printf("main thread %s\n",mes);
		if((write(sockfd,mes,sizeof(mes)))==-1)
		{
			fprintf(stderr,"Write Error:%s\n",strerror(errno));
			exit(1);
		}
		if(strcmp(buffer,"Quit")==0)
		{
			break;
		}
	}
	/* 结束通讯 */
	close(sockfd);
	exit(0);
}

 

 

//-----------------server.c--------------

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define MAXLINE 1000  //在一条消息中最大的输出字符数
#define LISTENQ 20              //最大监听队列
#define PORT 6555               //监听端口
#define MAXFD 20                //最大的在线用户数量
void *get_client(void *);     
int sockfd,i;
static int maxi=0;//maxi表示当前client数组中最大的用户的i值
static int client[MAXFD];

void recvandsend(void * arg)           //监听转发线程入口函数
{
	printf("hello new thread\n");
	int index=(int)arg;
	int nbytes=0;
	char buffer[1024];
	int len;
	int outindex=0;
	printf("Listen Client[%d]", index);
	while(1)
	{

		memset(buffer,0,sizeof(buffer));
		nbytes=0;		
		//index++;	
		nbytes=read(client[index],buffer,sizeof(buffer));
//			printf("%d,%d\n",index,client[index]);	
		if(nbytes>0)
		{
			buffer[nbytes]='\0';
			printf(" %s\n",buffer);
			outindex=0;
			while(outindex<maxi)
				if(write(client[outindex++],buffer,sizeof(buffer))==-1)
				{
					fprintf(stderr,"Write Error:%s\n",strerror(errno));
					exit(1);
				}
		}

	}
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
//	int client_fd[LISTENQ],clientnum=0;;
        printf("server port has been open\a\n");
	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	int sin_size,portnumber;
	char hello[]="Hello! Are You Fine?\n";
	int thr_id[10];         /* thread ID for the newly created thread */
	pthread_t  p_thread[10];       /* thread's structure                     */
	int new_fd=0;
	memset(client,0,sizeof(client));
	if(argc!=1)
	{
		fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
		exit(1);
	}

	/* 服务器端开始建立 socket 描述符 */
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
		exit(1);
	}
	/* 服务器端填充 sockaddr 结构 */
	bzero(&server_addr,sizeof(struct sockaddr_in));
	server_addr.sin_family=AF_INET;
	server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	server_addr.sin_port=htons(PORT);
	/* 捆绑 sockfd 描述符 */
	if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
	{
		fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
		exit(1);
	}
	printf("The server port has been opened%d...\n",PORT);
	/* 监听 sockfd 描述符 */
	if(listen(sockfd,LISTENQ)==-1)
	{
		fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
		exit(1);
	}
	
	printf("Welcome to this chatroom\n");
	while(1)
	{
		/* 服务器阻塞,直到客户程序建立连接 */
		if(maxi>=20)
		{
		     printf("The number of the people was too much,Please wait while !以达到人数上线\a\n");
			continue;
		}
		sin_size=sizeof(struct sockaddr_in);
		if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
		{
			fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
			exit(1);
		}
		/*fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));*/
		client[maxi]=new_fd;
		thr_id[maxi] = pthread_create(&p_thread[maxi], NULL, recvandsend, maxi);
		maxi++;
		printf("\n新用户进入聊天室%d\n",new_fd);	
	}
	close(sockfd);
	exit(0);
}

    if you want running the coding,please according to the next guide:

   ->gcc -o server ../server.c -lpthread

   ->./server

   ->ggc -o client  ../client.c -lpthread

   ->./client

 

  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值