1.多进程
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line:%d\n", __LINE__); \
perror(msg); \
} while (0);
#define IP "192.168.0.112"
int Rdwr_fork(int newfd, struct sockaddr_in bin)
{
char buf[1024] = { 0 };
size_t ret = 0;
while (1) {
bzero(buf, sizeof(buf));
ret = recv(newfd, buf, sizeof(buf), 0);
if (ret < 0) {
ERR_MSG("recv");
return -1;
} else if (ret == 0) {
printf("客户端已下线\n");
break;
}
printf("[%s %d] %d\n", inet_ntoa(bin.sin_addr), ntohs(bin.sin_port), newfd);
strcat(buf, "*_*");
if (send(newfd, buf, sizeof(buf), 0) < 0) {
ERR_MSG("newfd");
return -1;
}
printf("%s\n", buf);
}
}
void hanler(int sig)
{
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
int main(int argc, char const* argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0) {
ERR_MSG("socket");
return -1;
}
signal(SIGCHLD, hanler); // 回调函数
// 填充服务器地址信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(5555);
cin.sin_addr.s_addr = inet_addr(IP);
int reuse = 1;
if (setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse, sizeof(reuse)) < 0) {
ERR_MSG("setsockopt");
return -1;
}
// 绑定
if (bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) < 0) {
ERR_MSG("bind");
return -1;
}
if (listen(cfd, 128) < 0) {
ERR_MSG("listen");
return -1;
}
struct sockaddr_in bin;
socklen_t addrlen = sizeof(bin); // 存放客户端地址信息
while (1) {
int newfd = accept(cfd, (struct sockaddr*)&bin, &addrlen);
if (newfd < 0) {
ERR_MSG("newfd");
return -1;
}
printf("连接成功\n");
pid_t tid = fork();
if (tid > 0) {
close(newfd); // 关闭newfd
} else if (tid == 0) {
close(cfd);
Rdwr_fork(newfd, bin);
close(newfd);
exit(0);
} else {
ERR_MSG("fork");
return -1;
}
}
close(cfd);
return 0;
}
2.多线程
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line:%d\n", __LINE__); \
perror(msg); \
} while (0);
#define IP "192.168.0.112"
struct cli_msg {
int newfd;
struct sockaddr_in cin;
};
void* pthread_wire(void* arg)
{
int newfd = (*(struct cli_msg*)arg).newfd;
struct sockaddr_in cin = (*(struct cli_msg*)arg).cin;
char buf[1024] = { 0 };
size_t ret = 0;
while (1) {
bzero(buf, sizeof(buf));
// 接收
ret = recv(newfd, buf, sizeof(buf), 0);
if (ret < 0) {
ERR_MSG("recv");
break;
} else if (ret == 0) {
printf("客户端已下线\n");
break;
}
printf("[%s %d] %d %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);
// 发送
strcat(buf, "*_*");
if (send(newfd, buf, sizeof(buf), 0) < 0) {
ERR_MSG("send");
break;
;
}
printf("发送成功\n");
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, char const* argv[])
{
pthread_t tid;
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(5555);
sin.sin_addr.s_addr = inet_addr(IP);
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
ERR_MSG("setsockept");
return -1;
}
if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
ERR_MSG("bind");
return -1;
}
if (listen(sfd, 128) < 0) {
ERR_MSG("listen");
return -1;
}
struct sockaddr_in ain;
socklen_t addrlen = sizeof(ain);
struct cli_msg info;
while (1) {
int newfd = accept(sfd, (struct sockaddr*)&ain, &addrlen);
if (newfd < 0) {
ERR_MSG("newfd");
return -1;
}
printf("[%s : %d] newfd = %d 连接成功__%d__\n", inet_ntoa(ain.sin_addr), ntohs(ain.sin_port), newfd, __LINE__);
info.newfd = newfd;
info.cin = ain;
if (pthread_create(&tid, NULL, pthread_wire, (void*)&info) != 0) {
fprintf(stderr, "line:%dpthread_create failed\n", __LINE__);
return -1;
}
pthread_detach(tid); // 分离线程 目的是不用回收子线程资源
}
close(sfd);
return 0;
}