客户端
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main() {
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
inet_pton(AF_INET, "10.37.62.58", &serveraddr.sin_addr.s_addr);
serveraddr.sin_port = htons(9999);
int ret = connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
char recvBuf[1024] = {0};
int i = 0;
while (1) {
sprintf(recvBuf, "data: %d\n", i++);
write(fd, recvBuf, strlen(recvBuf) + 1);
int len = read(fd, recvBuf, sizeof(recvBuf));
if (len == -1) {
perror("read");
exit(-1);
}
else if (len > 0) {
printf("receive %s\n", recvBuf);
}
else if (len == 0) {
printf("server close\n");
break;
}
sleep(1);
}
close(fd);
return 0;
}
多进程实现并发服务器
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
void recycleChild(int arg) {
while (1) {
int ret = waitpid(-1, NULL, WNOHANG);
if (ret == -1) {
break;
}
else if (ret == 0) {
break;
}
else if (ret > 0) {
printf("recycle child process: %d\n", ret);
}
}
}
int main() {
struct sigaction act;
act.sa_flags = 0;
act.sa_handler = recycleChild;
sigemptyset(&act.sa_mask);
sigaction(SIGCHLD, &act, NULL);
int lfd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
saddr.sin_addr.s_addr = INADDR_ANY;
int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr));
ret = listen(lfd, 128);
while (1) {
struct sockaddr_in cliaddr;
int len = sizeof(cliaddr);
int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);
if (cfd == -1) {
if (errno == EINTR) {
continue;
}
perror("accept");
exit(-1);
}
pid_t pid = fork();
if (pid == 0) {
char cliIP[16];
inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, cliIP, sizeof(cliIP));
unsigned short cliPort = ntohs(cliaddr.sin_port);
printf("client IP: %s, Port: %d\n", cliIP, cliPort);
char recvBuf[1024];
while (1) {
int len = read(cfd, recvBuf, sizeof(recvBuf));
if (len == -1) {
perror("read");
exit(-1);
}
else if (len > 0) {
printf("receive %s\n", recvBuf);
}
else if (len == 0) {
printf("client close\n");
break;
}
write(cfd, recvBuf, strlen(recvBuf) + 1);
}
close(cfd);
exit(0);
}
}
close(lfd);
return 0;
}
多线程实现并发服务器
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
struct sockInfo {
int fd;
struct sockaddr_in addr;
pthread_t tid;
};
struct sockInfo sockinfos[128];
void* working(void *arg) {
struct sockInfo *pinfo = (struct sockInfo *)arg;
char cliIP[16];
inet_ntop(AF_INET, &pinfo->addr.sin_addr.s_addr, cliIP, sizeof(cliIP));
unsigned short cliPort = ntohs(pinfo->addr.sin_port);
printf("client IP: %s, Port: %d\n", cliIP, cliPort);
char recvBuf[1024];
while (1) {
int len = read(pinfo->fd, recvBuf, sizeof(recvBuf));
if (len == -1) {
perror("read");
exit(-1);
}
else if (len > 0) {
printf("receive %s\n", recvBuf);
}
else if (len == 0) {
printf("client close\n");
break;
}
write(pinfo->fd, recvBuf, strlen(recvBuf) + 1);
}
close(pinfo->fd);
return NULL;
}
int main() {
int lfd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(9999);
saddr.sin_addr.s_addr = INADDR_ANY;
int ret = bind(lfd, (struct sockaddr *)&saddr, sizeof(saddr));
ret = listen(lfd, 128);
int max = sizeof(sockinfos) / sizeof(sockinfos[0]);
for (int i = 0; i < max; i++) {
bzero(&sockinfos[i], sizeof(sockinfos[i]));
sockinfos[i].fd = -1;
sockinfos[i].tid = -1;
}
while (1) {
struct sockaddr_in cliaddr;
int len = sizeof(cliaddr);
int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);
struct sockInfo *pInfo;
for (int i = 0; i < max; i++) {
if (sockinfos[i].fd == -1) {
pInfo = &sockinfos[i];
break;
}
if (i == max - 1) {
sleep(1);
i = -1;
}
}
pInfo->fd = cfd;
memcpy(&pInfo->addr, &cliaddr, len);
pthread_create(&pInfo->tid, NULL, working, pInfo);
pthread_detach(pInfo->tid);
}
close(lfd);
return 0;
}