当前位置:网站首页>socket编程一:使用fork()实现最基础的并发模式
socket编程一:使用fork()实现最基础的并发模式
2022-07-27 05:19:00 【Mr FF】
tcp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
// create socket
int socketServerListen = socket(AF_INET, SOCK_STREAM, 0);
if (socketServerListen < 0) {
perror("socket");
exit(1);
}
struct sockaddr_in serverAddr;
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
// atoi, 不使用atoi会导致connection refused.
serverAddr.sin_port = htons(atoi(argv[2]));
serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
// bind, notice: it use the standard struct: sockaddr, not sockaddr_in
if (bind(socketServerListen, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) < 0) {
perror("bind");
exit(2);
}
// listen
if (listen(socketServerListen, 30) < 0) {
perror("listen");
exit(3);
}
struct sockaddr_in clientAddr;
bzero(&clientAddr, sizeof(clientAddr));
int connFd;
// Invalid argument, if not initialized.
// 如果不初始化,在accept中会直接报: Invalid argument.
socklen_t clientAddrLen = 0;
while (1) {
// accpet was different with bind, the parameters 3 was socklen_t*, but not socklen_t.
if (connFd = accept(socketServerListen, (struct sockaddr*)&clientAddr, &clientAddrLen) < 0) {
perror("accept");
continue;
}
printf("tcp_server: client addr is:%s, client port is:%d\n",
inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
pid_t pid = fork();
if (pid < 0) { // error
perror("fork");
} else if (pid == 0) { // child
// in child, it should close the socketServerListen, keep the connFd.
close(socketServerListen);
char buf[1024] = {0};
ssize_t len = 0;
// read the data from the connFd
while (len = read(connFd, buf, 1024) > 0) {
printf("read the data:%s\n", buf);
//write(connFd, buf, len);
}
} else {
// in father, it should close the connFd, just keep the socketServerListen
close(connFd);
}
}
return 0;
}tcp_client.c
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
// socket
int clientSockFd = socket(AF_INET, SOCK_STREAM, 0);
if (clientSockFd < 0) {
perror("clientSockFd");
exit(1);
}
struct sockaddr_in serverAddr;
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
// atoi, if not use this ,connect will fail.
serverAddr.sin_port = htons(atoi(argv[2]));
// use inet_addr to transfer the address
serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
//serverAddr.sin_addr.s_addr = htons(INADDR_ANY);
// connect
if (connect(clientSockFd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) < 0) {
perror("connect");
exit(2);
}
// get the data from standard input. (keyboard)
char buf[1024] = {0};
while (1) {
read(0, buf, 1024);
write(clientSockFd, buf, 1024);
fflush(0);
}
return 0;
}边栏推荐
- GBASE 8C——SQL参考6 sql语法(14)
- Day 9. Graduate survey: A love–hurt relationship
- Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
- 2.简单回归问题
- Gbase 8C - SQL reference 6 SQL syntax (4)
- 【mysql学习】8
- 模型的推理速度
- 西瓜书学习笔记---第四章 决策树
- Digital image processing Chapter 5 - image restoration and reconstruction
- 6. Dimension transformation and broadcasting
猜你喜欢

cycleGAN解析

数字图像处理第四章——频率域滤波

14. Example - Multi classification problem

视觉横向课题bug1:FileNotFoundError: Could not find module ‘MvCameraControl.dll‘ (or one of it

17. Attenuation of momentum and learning rate

数字图像处理 第一章 绪论

Digital image processing Chapter 4 - frequency domain filtering

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

Numpy basic learning

19.上下采样与BatchNorm
随机推荐
Gbase 8C - SQL reference 6 SQL syntax (1)
[MVC Architecture] MVC model
模型的推理速度
Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
面试常问Future、FutureTask和CompletableFuture
舆情&传染病时空分析文献阅读笔记
Public opinion & spatio-temporal analysis of infectious diseases literature reading notes
数字图像处理 第二章 数字图像基础
维度问题以及等高线
3. Classification problems - initial experience of handwritten digit recognition
Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
西瓜书学习笔记---第四章 决策树
13. Logistic regression
19.上下采样与BatchNorm
Day10. Work organization and mental health problems in PhD students
pytorch转onnx相关问题
Gbase 8C - SQL reference 6 SQL syntax (4)
2021中大厂php+go面试题(2)
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis