当前位置:网站首页>Socket programming 1: using fork() to realize the most basic concurrency mode
Socket programming 1: using fork() to realize the most basic concurrency mode
2022-07-27 06:06: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, Don't use atoi It can lead to 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.
// If not initialized , stay accept China will directly report : 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;
}边栏推荐
- Day 2. Depressive symptoms, post-traumatic stress symptoms and suicide risk among graduate students
- 基于C#的Winform对Access数据库进行操作(mdb结尾)
- Weidongshan digital photo frame project learning (III) transplantation of freetype
- Stm32-fsmc extended memory SRAM
- 力扣题解 动态规划(5)
- 编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
- 解决conda install 安装停止、中断问题
- When multiple formulas in latex share a sequence number
- 使用Markdowm
- Baiwen driving Daquan learning (II) I2C driving
猜你喜欢

【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?

2021-06-26

Multi task foundation of IOT operating system

What tools are needed to make video post effects?

PZK学C语言之初识指针

C# Json编码在TCP通讯中的一些使用总结

李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22

Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上

能替代ps的修图软件?

韦东山 数码相框 项目学习(四)简易的TXT文档显示器(电纸书)
随机推荐
Baiwen driving Daquan learning (II) I2C driving
编程学习记录——第3课【初识C语言】
QGIS系列(1)-QGIS(server-apache) win10安装
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
STM32-FSMC外扩内存SRAM
C语言-自定义结构类型
Xmind 思维导图 2022 v12.0.3中文版更新了哪些内容?
std::bind与std::function的一些应用
谈谈为何需要将类的成员函数声明为private
Unity Shader 概述
Gbase 8C - SQL reference 6 SQL syntax (12)
acwing每日一题 正方形数组的数目
韦东山 数码相框 项目学习(三)freetype的移植
人月神话阅读笔记
【头歌】重生之机器学习-线性回归
使用-Wall清除代码隐患
芯片、存储器及其关键指标简述 一
韦东山 数码相框 项目学习(二)在LCD上显示中文字符
【头歌】重生之我在py入门实训中(2):公式编程
Weidongshan digital photo frame project learning (I) display ASCII characters on LCD