多线程的TCP并发服务器的实现步骤:
1,创建一个socket套接字
2,绑定IP地址和端口信息到socket,使用函数bind()
1)IP地址
2)bind
3,设置允许的最大连接数,使用函数listen()
4,等待来自客户端的连接请求,使用函数accept()
5,收发数据,用函数recv(),send()/sendto()或者read(),write()
6,关闭网络连接,close
开始:建立文件
/*DATA: 2018-1-17
*AUTHOR: wg
*DESCRIPTION: 多线程TCP服务器
* 1,int socket(int domain,int type,int protocol);
* 2, int bind(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
* 3, int listen(int sockfd,int backlog);
* 4, accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
* 5, read(),write()
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_LISTEN 100
int ad[10];
char buf[128]={0};
struct sockaddr_in server_ip, remote_ip;
void *thread_fun(void *arg)
{
while(1)
{
printf("read data from client : %s\n",inet_ntoa(remote_ip.sin_addr));
memset(buf,0,128);
read(ad[(int)arg],buf,128);
printf("buf =%s\n",buf);
}
return NULL;
}
int main()
{
int server_len,remote_len;
int err,sd;
pthread_t tid[10];
int i=0;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd == -1)
{
printf("create socket failure, errno =%d\n",sd);
close(sd);
return -1;
}
server_ip.sin_family =AF_INET;
server_ip.sin_port = htons(10086);
server_ip.sin_addr.s_addr =htonl(INADDR_ANY);
memset(server_ip.sin_zero,0,8);
err=bind(sd,(struct sockaddr *)(&server_ip),sizeof(struct sockaddr));
if(err==-1)
{
printf("bind error,errno=%d\n",err);
close(sd);
return err;
}
err=listen(sd,MAX_LISTEN);
if(err == -1)
{
printf("listen error ,errno =%d\n",err);
close(sd);
return err;
}
remote_len=sizeof(struct sockaddr);
while(1)//listen n client quest
{
ad[i]=accept(sd,(struct sockaddr *)(&remote_ip),&remote_len);
if(ad[i] !=0 )
{
printf("accept error,errno =%d\n",ad[i]);
close(sd);
return -2;
}
err=pthread_create(&tid[i],NULL,thread_fun,(void*)i);
if(err !=0)
{
printf("create new thread failure\n");
close(ad[i]);
}
++i;
}
close(sd);
return 0;
}
可循环监听和接收多个客服端的请求。
Tcp客户端创建步骤:
1,创建一个socket,使用函数socket()
2, 设置要连接的服务器地址和端口
3,连接服务器,使用函数connect()
4,收发数据,使用函数recv(),sendto(),或write()和read()
5, 关闭网络连接,close()
创建client.c文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
char buf[100];
int sd;
struct sockaddr_in server_ip;
void *thread_write(void *arg)
{
while(1)
{
write(sd,"hello",6);
sleep(1);
}
}
void *thread_read(void *arg)
{
while(1)
{
sleep(1);
memset(buf,0,100);
read(sd,buf,100);
printf("client 2 say: %s\n",buf);
}
}
int main()
{
int server_len, remote_len;
pthread_t tid_read,tid_write;
int err;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd == -1)
{
printf("create socket failure");
return sd;
}
server_ip.sin_family =AF_INET;
server_ip.sin_port = htons(10086);
server_ip.sin_addr.s_addr = htonl(INADDR_ANY);
memcpy(server_ip.sin_zero,0,8);
err = connect(sd,(struct sockaddr *)(&server_ip),sizeof(struct sockaddr));
if(err == -1)
{
printf("connect failure\n");
close(sd);
return err;
}
err = pthread_create(&tid_read,NULL,thread_read,NULL);
if(err !=0)
{
printf("create read thread failure\n");
close(sd);
return err;
}
err = pthread_create(&tid_write,NULL,thread_write,NULL);
if(err !=0)
{
printf("create write thread failure\n");
close(sd);
return err;
}
pthread_join(&tid_read,NULL);
pthread_join(&tid_write,NULL);
close(sd);
return 0;
}