当前位置:网站首页>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;
}边栏推荐
猜你喜欢

制作视频特效必备工具:NUKE 13

IOT operating system

WebODM win10安装教程(亲测)

【头歌】重生之机器学习-线性回归

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

PZK学C语言之数据类型,进制转换,输入输出,运算符,分支语句ifelse

物联网操作系统多任务基础

Kaggle调用自定义模块方法

Live Home 3D Pro室内家居设计工具

Weidongshan digital photo frame project learning (IV) simple TXT document display (e-paper book)
随机推荐
力扣题解 动态规划(6)
百问网驱动大全学习(一)LCD驱动
C语言-动态内存管理
Cmder的基础文件操作
关于druid连接不上数据库的问题
Li Kou daily question sword finger offer II 091. paint the house
Linked list palindrome judgment
[song] rebirth of me in py introductory training (8): module
Unity Shader 概述
Live Home 3D Pro interior home design tool
百问网驱动大全学习(二)I2C驱动
Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?
韦东山 数码相框 项目学习(四)简易的TXT文档显示器(电纸书)
LaTeX中多个公式公用一个序号时
arcgis for js api-入门系列
子类调用父类构造函数的时机
编程学习记录——第9课【操作符】
【5·20特辑】MatLAb之我在和你表白
C# Json编码在TCP通讯中的一些使用总结