当前位置:网站首页>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;
}边栏推荐
- C language - linear sequence table
- AE 3D粒子系统插件:Trapcode Particular
- Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
- 常见的SQL优化方法
- 编程学习记录——第7课【函数】
- [song] rebirth of me in py introductory training (10): numpy
- [song] rebirth of me in py introductory training (7): function call
- 回调使用lambda
- [first song] rebirth of me in py introductory training (3): if conditional sentence
- 1半自动爬虫
猜你喜欢

浅记一下十大排序

arcgis for js api(2) 获取要素服务的id集合
![[concurrent programming series 9] priorityblockingqueue, delayqueue principle analysis of blocking queue](/img/2b/1bfadbffad33f8560357fab74e8308.png)
[concurrent programming series 9] priorityblockingqueue, delayqueue principle analysis of blocking queue

What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?

When multiple formulas in latex share a sequence number

Greedy high performance neural network and AI chip application research and training

发布 分辨率0.22m的建筑物分割数据库

2022.6.10 STM32MP157串口时钟的学习

Can it replace PS's drawing software?

C语言扫雷最新 递归展开 超详解(附源码)
随机推荐
编程学习记录——第8课【数组与设计五子棋,扫雷游戏】
浅记一下十大排序
LaTeX中多个公式公用一个序号时
System Design的相关准备材料
制作视频后期特效需要什么工具?
【头歌】重生之我在py入门实训中(10): Numpy
c语言-线性顺序表
服务器相关的指标解释
[first song] rebirth of me in py introductory training (2): formula programming
Unity Shader 概述
Weidongshan digital photo frame project learning (I) display ASCII characters on LCD
【头歌】重生之深度学习篇-Keras(初级)
Leetcode每日一题30. 串联所有单词的子串
Gbase 8C - SQL reference 6 SQL syntax (12)
韦东山 数码相框 项目学习(二)在LCD上显示中文字符
Dpdk network protocol stack VPP OVS DDoS Sdn nfv virtualization high performance expert Road
Kaggle调用自定义模块方法
What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?
编程学习记录--第2课【初识C语言】
[first song] Introduction to data science of rebirth -- return to advanced level