#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
struct SockInfo
{
int fd;
pthread_t tid;
struct sockaddr_in addr;
};
struct SockInfo infos[128];
void* working(void* arg)
{
while(1)
{
struct SockInfo* info = (struct SockInfo*)arg;
// 接收数据
char buf[1024];
int ret = read(info->fd, buf, sizeof(buf));
if(ret == 0)
{
printf("客户端已经关闭连接...\n");
info->fd = -1;
break;
}
else if(ret == -1)
{
printf("接收数据失败...\n");
info->fd = -1;
break;
}
else
{
write(info->fd, "data received:\n", strlen(buf)+1);
write(info->fd, buf, strlen(buf)+1);
}
}
return NULL;
}
int main()
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd == -1)
{
perror("socket");
exit(0);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8989);
addr.sin_addr.s_addr = INADDR_ANY;
int ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
if(ret == -1)
{
perror("bind");
exit(0);
}
ret = listen(fd, 100);
if(ret == -1)
{
perror("listen");
exit(0);
}
int len = sizeof(struct sockaddr);
int max = sizeof(infos) / sizeof(infos[0]);
for(int i=0; i<max; ++i)
{
bzero(&infos[i], sizeof(infos[i]));
infos[i].fd = -1;
infos[i].tid = -1;
}
while(1)
{
struct SockInfo* pinfo;
for(int i=0; i<max; ++i)
{
if(infos[i].fd == -1)
{
pinfo = &infos[i];
break;
}
if(i == max-1)
{
sleep(1);
i--;
}
}
int connfd = accept(fd, (struct sockaddr*)&pinfo->addr, &len);
printf("parent thread, connfd: %d\n", connfd);
if(connfd == -1)
{
perror("accept");
exit(0);
}
pinfo->fd = connfd;
pthread_create(&pinfo->tid, NULL, working, pinfo);
pthread_detach(pinfo->tid);
}
close(fd); // 监听
return 0;
}
这是一个C语言实现的TCP/IP服务器端程序,它使用多线程来处理客户端的连接请求。服务器监听8989端口,接收到连接请求后,为每个客户端创建一个新的线程进行数据的接收和发送。当客户端关闭连接或数据接收出错时,服务器会关闭相应的文件描述符。
830

被折叠的 条评论
为什么被折叠?



