使用线程完成TCP服务器
tcp特点:有链接,效率低,安全(丢包重传) 步骤:
- 创建套接字
- 设置端口复用
- 绑定
- 监听
- accept取出队列中的第一个客服端
- 创建线程,对每一个客服端同时进行收发消息
- 关闭套接字
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct
{
int new_sockfd;
struct sockaddr_in cli_addr;
}CLI_MSG;
void * client_fun(void* arg)
{
CLI_MSG *p=(CLI_MSG *)arg;
char des_ip[16]="";
inet_ntop(AF_INET,(void *)&p->cli_addr.sin_addr.s_addr,des_ip,16);
printf("%s连接上了\n",des_ip);
while(1)
{
char buf[512]="";
ssize_t j=recv(p->new_sockfd, buf, sizeof(buf), 0);
printf("len=%d :text=%s\n",j,buf);
if(j==0){
break;
}
send(p->new_sockfd,buf,j,0);
}
close(p->new_sockfd);
if(p!=NULL)
{
free(p);
p=NULL;
}
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd>0)
printf("sockfd=%d\n",sockfd);
else
printf("创建失败\n");
int yes=1;
setsockopt(sockfd, SOL_SOCKET,SO_REUSEADDR, (void *)&yes, sizeof(yes));
struct sockaddr_in my_addr;
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(9000);
my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bind(sockfd,(struct sockaddr*)&my_addr,sizeof(my_addr));
listen(sockfd,20);
while(1)
{
struct sockaddr_in cli_addr;
socklen_t cli_len=sizeof(cli_addr);
int new_sockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&cli_len);
CLI_MSG *p=(CLI_MSG *)calloc(1,sizeof(CLI_MSG));
p->new_sockfd=new_sockfd;
p->cli_addr=cli_addr;
pthread_t pth;
pthread_create(&pth,NULL,(void *)client_fun,(void *)p);
pthread_detach(pth);
}
close(sockfd);
return 0;
}
|