⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 srv.c

📁 在Linux环境下运行
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>

#define SERVPORT 3000
#define BUFFER_SIZE 128#define BACKLOG 10struct MSG{uint32_t dest_addr;uint32_t head_addr;char buf[BUFFER_SIZE];int imsglen;};int cln_num=0,cln_sck_fd[BACKLOG];struct sockaddr_in cln_addr[BACKLOG];struct sockaddr_in srv_addr;
/*接收*/
void * msg_rcv(void * Parm)
{
         int i,rcvBytes;         struct MSG msg_buf;
         int sck_fd = *((int*)Parm);
         while(1)
	    {
	     if ((rcvBytes = recv(sck_fd,(char *)&msg_buf,sizeof(struct MSG),0)) ==-1) 
	        {
			perror("recv error!");
			return NULL;
		}else if(rcvBytes==0)		{			printf("recv error!\n");
			return NULL;		}		msg_buf.buf[msg_buf.imsglen]=0;		if(msg_buf.dest_addr==srv_addr.sin_addr.s_addr)//在键盘输入的IP与检测连接的IP一致时则显示接收消息		{
			printf("Received Msg from %s: %s\n",inet_ntoa(msg_buf.head_addr),msg_buf.buf);			continue;		}                /*将消息通过服务端转发给另外一个客户端*/		for(i=0;i<cln_num;i++)//当客户端接收到消息就退出		{		    if(cln_addr[i].sin_addr.s_addr==msg_buf.dest_addr)		     {			if(send(cln_sck_fd[i],(char *)&msg_buf,sizeof(struct MSG),0)==-1)			{				perror("sand");              		exit(1);			}			break;		     }		}		if(i==cln_num)//提示消息不能到达		{			strcpy(msg_buf.buf,"Aim host unreachable!");			msg_buf.dest_addr=msg_buf.head_addr;			msg_buf.head_addr=srv_addr.sin_addr.s_addr;			msg_buf.imsglen = strlen(msg_buf.buf);			if(send(sck_fd,(char *)&msg_buf,sizeof(struct MSG),0)==-1)			{				perror("send");              		exit(1);			}		}
	}
	return NULL;
}
     /*发送*/
void * msg_send(void)
{		struct MSG msg_buf;
	int i,len=0;

	while(1)
	{
		memset(&msg_buf,0,sizeof(struct MSG));				scanf("%s",msg_buf.buf);		msg_buf.head_addr=srv_addr.sin_addr.s_addr;		msg_buf.imsglen = strlen(msg_buf.buf);
				if(msg_buf.imsglen<=0)		{			printf("[host addr] [msg]\n");			continue;		}		for(i=0;i<cln_num;i++)		{			msg_buf.dest_addr=cln_addr[cln_num].sin_addr.s_addr;			if(send(cln_sck_fd[i],(char *)&msg_buf,sizeof(struct MSG),0)==-1)			{				printf("Send error!\n");				exit(1);			}		}		   
	}
}

int main()
{
	int srv_sck_fd,iSize;
	pthread_t pThreadID;
	int iRet;

	if((srv_sck_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
	{
		perror("socket error!");
		exit(1);
	}
	srv_addr.sin_family=AF_INET;//使用Internet协议族
	srv_addr.sin_port=htons(SERVPORT);//指定端口号
	srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//使用所有的网络接口	//srv_addr.sin_addr.s_addr = inet_addr("192.168.1.5");
	bzero(&(srv_addr.sin_zero),8);
              /*绑定*/
	if (bind(srv_sck_fd, (struct sockaddr *)&srv_addr, sizeof(struct sockaddr)) == -1) 
	{
		perror("bind error!");
		exit(1);
	}             /*监听*/
	if (listen(srv_sck_fd,10) == -1) 
	{
		perror("listen error!");
		exit(1);
	}

	iSize = sizeof(struct sockaddr_in);
	iRet = pthread_create(&pThreadID,NULL,msg_send,NULL);//创建发送线程         /*接收*/
	while(1)
	{
		if ((cln_sck_fd[cln_num] = accept(srv_sck_fd, (struct sockaddr *)&cln_addr[cln_num],&iSize)) == -1)
		{
			perror("accept error");
			continue;
		}		
		iRet = pthread_create(&pThreadID,NULL,msg_rcv,(void *)&cln_sck_fd[cln_num]);
		
		printf("received a connection from %s\n", inet_ntoa(cln_addr[cln_num].sin_addr));		cln_num++;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -