1.TCP并发进程服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#define IP "192.168.8.101" //服务器 IP
#define PORT 8888 //端口号
#define MAX 128
typedef void (*sighandler_t)(int);
#define ERROR(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
void handler(int sig)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, char *argv[])
{
int sfd, cfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_size;
if(SIG_ERR == signal(SIGCHLD,handler))
{
ERROR("signal");
return -1;
}
//1.创建流式套接字
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERROR("socket");
return -1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
memset(&client_addr, 0, sizeof(struct sockaddr_in));
//填充地址信息结构体
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); //端口的网络字节序
server_addr.sin_addr.s_addr = inet_addr(IP); //IP的网络字节序
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERROR("setsockopt");
return -1;
}
//2.将地址信息绑定到套接字上
if (bind(sfd, (struct sockaddr *) &server_addr,
sizeof(struct sockaddr_in)) < 0)
{
ERROR("bind");
return -1;
}
//3.将sfd设置为被动监听状态
if (listen(sfd, 128) < 0)
{
ERROR("listen");
return -1;
}
printf("服务器正在监听....\n");
client_addr_size = sizeof(struct sockaddr_in);
char buf[MAX] = "";
ssize_t res = 0;
while(1)
{
//4.从队列头获取一个客户的信息
cfd = accept(sfd, (struct sockaddr *) &client_addr,
&client_addr_size);
if (cfd < 0)
{
ERROR("accept");
return -1;
}
printf("[%s:%d] cfd = %d 连接成功\n", \
inet_ntoa(client_addr.sin_addr), \
ntohs(client_addr.sin_port), cfd);
pid_t pid = fork();
if(pid > 0)
{
close(cfd);
}
else if(0 == pid)
{
close(sfd);
while(1)
{
bzero(buf, sizeof(buf));
//5.接收来自客户端信息
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERROR("recv");
return -1;
}
if(0 == res)
{
printf("客户端已关闭\n");
break;
}
printf("[%s:%d] recmsg:%s\n", \
inet_ntoa(client_addr.sin_addr), \
ntohs(client_addr.sin_port), buf);
//6.发送来自客户端信息
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERROR("send");
return -1;
}
}
exit(0);
}
else
{
ERROR("fork");
return -1;
}
}
return 0;
}
2.TCP并发线程服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>
#define IP "192.168.8.101" //服务器 IP
#define PORT 8888 //端口号
#define MAX 128
#define ERROR(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
struct msg
{
int cfd;
struct sockaddr_in client_addr;
};
void* callback(void* meg)
{
pthread_detach(pthread_self());
int cfd = ((struct msg*)meg)->cfd;
struct sockaddr_in client_addr = ((struct msg*)meg)->client_addr;
char buf[MAX] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//5.接收来自客户端信息
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERROR("recv");
break;
}
if(0 == res)
{
printf("客户端已关闭\n");
break;
}
printf("[%s:%d] recmsg:%s\n", \
inet_ntoa(client_addr.sin_addr), \
ntohs(client_addr.sin_port), buf);
//6.发送来自客户端信息
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERROR("send");
break;
}
}
close(cfd);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int sfd, cfd;
struct sockaddr_in server_addr,client_addr;
socklen_t client_addr_size;
struct msg info;
//1.创建流式套接字
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERROR("socket");
return -1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
//填充地址信息结构体
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); //端口的网络字节序
server_addr.sin_addr.s_addr = inet_addr(IP); //IP的网络字节序
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERROR("setsockopt");
return -1;
}
//2.将地址信息绑定到套接字上
if (bind(sfd, (struct sockaddr *) &server_addr,
sizeof(struct sockaddr_in)) < 0)
{
ERROR("bind");
return -1;
}
//3.将sfd设置为被动监听状态
if (listen(sfd, 128) < 0)
{
ERROR("listen");
return -1;
}
printf("服务器正在监听....\n");
client_addr_size = sizeof(struct sockaddr_in);
pthread_t tid;
//创建线程用收发信息
while(1)
{
//4.从队列头获取一个客户的信息
cfd = accept(sfd, (struct sockaddr *) &client_addr,
&client_addr_size);
if (cfd < 0)
{
ERROR("accept");
return -1;
}
printf("[%s:%d] cfd = %d 连接成功\n", \
inet_ntoa(client_addr.sin_addr), \
ntohs(client_addr.sin_port), cfd);
//创建线程用于接待客户端
info.cfd = cfd;
info.client_addr = client_addr;
if(pthread_create(&tid, NULL, callback,(void*)&info) < 0)
{
ERROR("pthread_create");
return -1;
}
}
return 0;
}