封包发送数据包

服务器:

#include"common.h"

struct cli_t{
 	int num;
	int cfd;
	struct sockaddr_in caddr;
	struct cli_t *before;
	struct cli_t *next;
}cli_t;
struct cli_t *head = NULL;
fd_set set;
void *hander()
{
	struct cli_t *hcli=NULL;

	while(1)
	{
		for(hcli = head;hcli!=NULL;hcli=hcli->next)
		{
			(hcli->num)++;
			int hfd=hcli->cfd;
			printf("[%d]->num==%d\n",hcli->cfd,hcli->num);

			if(hcli->num==20||hcli->num>20)
			{
				
				if(hcli->before==NULL)
					head=hcli->next;
				
				else 
					hcli->before->next=hcli->next;
				
				printf("已退出\n");
				
				FD_CLR(hfd,&set); 

			}
		}
		sleep(1);
	}
}

int main()
{

	int fd,nfd,maxfd;
	int ret;
	struct sockaddr_in  saddr,caddr;
	int addr_len;
	char buf[1024];
	int *pfd;
	fd_set rset;
	struct cli_t *pcli;


	fd = socket(AF_INET,SOCK_STREAM,0);
	if(fd<0)
	{
		perror("socket");
		return -1;
	}


	saddr.sin_family = AF_INET;
	saddr.sin_port   = htons(9090);
	inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr.s_addr);

	ret = bind(fd,(struct sockaddr*)&saddr,sizeof(saddr));
	if(ret<0)
	{
		perror("bind");
		goto END;
	}

	ret = listen(fd,20);
	if(ret<0)
	{
		perror("listen");
		goto END;
	}

	FD_ZERO(&set);
	FD_SET(fd,&set);
	maxfd = fd;

struct timeval tv;
tv.tv_sec=5;
tv.tv_usec=0;
	pthread_t pthid;
	int ret2;
	ret2=pthread_create(&pthid,NULL,hander,(void*)head);
	if(ret2!=0)
		perror("pthread");
	while(1)
	{
		rset = set;
		printf("select ...\n");
		ret = select(maxfd+1,&rset,NULL,NULL,&tv);
tv.tv_sec=5;	
		printf("select over ret = %d\n",ret);
		if(ret<0)
		{
			perror("select");
			break;
		}
		if(FD_ISSET(fd,&rset))
		{
			//接受链接
			addr_len = sizeof(caddr);
			printf("accept..\n");
			nfd = accept(fd,(struct sockaddr*)&caddr,&addr_len);
			printf("accept over..\n");
			if(nfd<0)
			{
				perror("accept");
			}

			//加入集合
			FD_SET(nfd,&set);
			if(nfd>maxfd)
				maxfd = nfd;

			//加入链表
			pcli = malloc(sizeof(struct cli_t));
			pcli->next=head;
			pcli->before=NULL;
			pcli->cfd = nfd;
			pcli->caddr = caddr;
			if(head!=NULL)
				head->before=pcli;
			
			head = pcli;
		}

		for(pcli = head;pcli!=NULL;pcli=pcli->next)
		{
			int tfd = pcli->cfd;

			if(!FD_ISSET(tfd,&rset))
				continue;
			char buf[201];
			memset(buf,0,201);
			char *level;
			ret = read(tfd,buf,201);
			if(ret <= 0)   //端口断开,用户下线
			{

				FD_CLR(tfd,&set); //取消检测此客户端
			}
			else if(ret>0)
			{	pcli->num=0;
				level=(char*)buf;
				printf("level==%c\n",*level);

				if(strcmp(buf,"send")==0)
					printf("数据已传输成功\n");
				else if(strcmp(buf,"receive")==0)
					printf("数据接收成功\n");
				else
							printf("buf==%s\n",buf);
			}
		}
	}

END:
	close(fd);
	return 0;
}

客户端:

#include"common.h"
int main()
{
	/*建立连接*/
	int fd;
	int ret;
	struct sockaddr_in  saddr;
	char *buf=(char*)malloc(201);
	char buf2[200];
	char *temp;

	fd = socket(AF_INET,SOCK_STREAM,0);
	if(fd<0)
	{
		perror("socket");
		return -1;
	}


	saddr.sin_family = AF_INET;
	saddr.sin_port   = htons(9090);
	inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr.s_addr);

	ret = connect(fd,(struct sockaddr*)&saddr,sizeof(saddr));
	if(ret<0)
	{
		perror("connect");close(fd);
		return -1;
	}

	while(1)
	{
		char a='8';
		*buf=a;	
		scanf("%s",buf2);getchar();
		temp=buf2;
		memcpy((buf+1),temp,200);
		write(fd,buf,201);
	}

}

 

展开阅读全文

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