思路
首先要考虑到服务器的流程,TCP服务器端程序流程:
- socket
- bind绑定
- listen监听
- accept等待连接
多线程并发服务器需要通过多个线程实现与多个客户端的连接,当每次有一个客户端连接来时,创建一个线程,用于与客户端的通信。
创建线程后,将所用参数传递给线程,tid,cfd,clientaddr;
最后将线程分离,方便线程结束回收子线程资源。
服务器代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
struct sockInfo
{
int fd;
pthread_t pthd;
struct sockaddr_in clientaddr;
};
void * pthreadCallback(void * arg)
{
char rbuf[1024];
struct sockInfo sockinfo = *(struct sockInfo *)arg;
int cfd = sockinfo.fd;
printf("the port is : %d\n", ntohs(sockinfo.clientaddr.sin_port));
while(1)
{
int len = read(cfd, rbuf, 1024);
rbuf[len] = 0;
if(len > 0)
{
write(cfd, rbuf, len);
printf("%d send:%s", cfd, rbuf);
}
else if(len == -1)
{
perror("read");
close(cfd);
pthread_exit(NULL);
}
else
{
printf("%d client is closed...\n", cfd);
close(cfd);
pthread_exit(NULL);
}
}
}
int main()
{
int lfd = socket(PF_INET, SOCK_STREAM, 0);
if(lfd == -1)
{
perror("socket");
exit(-1);
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
inet_ntop(lfd, &saddr.sin_addr.s_addr, "192.168.1.108", sizeof(saddr));
saddr.sin_port = htons(9999);
int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr));
if(ret)
{
perror("bind");
exit(-1);
}
ret = listen(lfd, 128);
if(ret)
{
perror("listen");
exit(-1);
}
int cfd;
pthread_t pthd;
while(1)
{
struct sockaddr_in caddr;
int clen = sizeof(caddr);
cfd = accept(lfd, (struct sockaddr *)&caddr, &clen);
struct sockInfo cinfo;
cinfo.fd = cfd;
//cinfo.clientaddr = caddr;
memcpy(&cinfo.clientaddr, &caddr, sizeof(caddr));
ret = pthread_create(&cinfo.pthd, NULL, pthreadCallback, (void *)&cinfo);
if(ret == -1)
{
perror("pthread_creat");
printf("err is: %s\n", strerror(ret));
}
ret = pthread_detach(cinfo.pthd);
if(ret == -1)
{
printf("pthread error : %s\n", strerror(ret));
}
printf("the %d client link success!, pthd:%ld\n", cfd, cinfo.pthd);
}
return 0;
}
客户端代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
int main()
{
int lfd = socket(PF_INET, SOCK_STREAM, 0);
if(lfd == -1)
{
perror("socket");
exit(-1);
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
int len = sizeof(saddr);
inet_pton(AF_INET, "192.168.1.108", &saddr.sin_addr.s_addr);
saddr.sin_port = htons(9999);
int ret = connect(lfd, (struct sockaddr *)&saddr, sizeof(saddr));
if(ret == -1)
{
perror("connect");
exit(-1);
}
printf("client link success!\n");
//由于读操作会阻塞,客户端需要先发送数据,若先读取数据,一个进程就会阻塞住,一个进程就要先发数据,如果一次数据没有接收到,则会阻塞住。
//可以采用两个进程,发、收互不影响
pid_t pid = fork();
if(pid==0)
{
char rbuf[1024];
while(1)
{
//memset(rbuf, 0, 1024);
int lent = read(lfd, rbuf, 1024);
if(lent > 0)
{
printf("send: %s", rbuf);
}
else if(lent == -1) perror("read");
}
}
else if(pid > 0)
{
int i = 0;
char sbuf[1024];
while(1)
{
i++;
if(i > 255) i = 0;
sprintf(sbuf, "the num is %d\n", i);
write(lfd, sbuf,strlen(sbuf));
sleep(1);
}
}
close(lfd);
return 0;
}
问题
问题1:之前说C语言种结构体无法直接赋值,只能单个成员直接赋值,但经过C语言测试,可以直接赋值。
问题2:可以预设客户端信息数组,在主线程和子线程中使用指针变量控制客户端连接信息,通过指针控制方便,但客户端信息数组空间需要提前申请好,放在共享空间堆中。