網(wǎng)站首頁 編程語言 正文
Linux創(chuàng)建TCP的步驟
TCP編程需要客戶端和服務(wù)器兩套編碼,其創(chuàng)建TCP的流程也是不完全一致的
服務(wù)端
使用socket函數(shù)創(chuàng)建一個(gè)套接字
使用setsockopt函數(shù)設(shè)置套接字的屬性
使用bind函數(shù)綁定IP地址、端口信息到套接字上使用listen函數(shù)監(jiān)聽指定端口
使用accept函數(shù)接收客戶端的連接請求
使用send/recv和read/write函數(shù)進(jìn)行數(shù)據(jù)的收發(fā)
使用close函數(shù)關(guān)閉網(wǎng)絡(luò)連接和監(jiān)聽
客戶端
使用socket函數(shù)創(chuàng)建套接字使用setsockopt函數(shù)設(shè)置套接字屬性
使用bind函數(shù)綁定IP地址和端口信息
設(shè)置需要連接的IP地址和端口使用connect函數(shù)請求建立連接
使用send/recv和read/write函數(shù)進(jìn)行數(shù)據(jù)的收發(fā)
使用close函數(shù)關(guān)閉網(wǎng)路連接
TCP建立流程
示例代碼
服務(wù)器
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <pthread.h> #include <time.h> #define MAXSIZE 128 char news[MAXSIZE]; int res; //用以接收函數(shù)返回值 void* pthread_chat(void * arg) //創(chuàng)建線程用以接收數(shù)據(jù) { int confd = *(int *)arg; while(1) { res = recv(confd, news, sizeof(news), 0); if(res <= 0) { perror("recv"); break; } printf("The news is: %s\n",news); memset(news,0,MAXSIZE); send(confd,"OK",2,0); } printf("One client over\n"); close(confd); } char *Time() //獲取當(dāng)前時(shí)間 { time_t timer; struct tm *tblock; timer = time(NULL); tblock = localtime(&timer); return asctime(tblock); } void save(char *s) //儲(chǔ)存日志文件 { int fd; fd = open("journal",O_RDWR|O_APPEND|O_CREAT); if(fd < 0) perror("open"); else { char *buf = Time(); strcat(buf,s); write(fd,buf,MAXSIZE); lseek(fd,0,SEEK_END); if(res < 0) perror("write"); } } int main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in saddr, caddr; saddr.sin_family = AF_INET; saddr.sin_port = htons(6666); saddr.sin_addr.s_addr = inet_addr("127.0.0.1"); res = bind(sockfd,(struct sockaddr*)&saddr, sizeof(saddr)); if(res < 0) perror("bind"); listen(sockfd, 5); //監(jiān)聽端口 while(1) { int len = sizeof(caddr); int confd = accept(sockfd,(struct sockaddr*)&caddr, &len); if(confd < 0) { perror("accept"); continue; }else { save(inet_ntoa(caddr.sin_addr)); } printf("Accept confdis:%d, ip=%s\n",confd,inet_ntoa(caddr.sin_addr)); pthread_t tid; pthread_create(&tid, NULL, pthread_chat, &confd); } }
客戶端
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define MAXSIZE 128 char news[MAXSIZE]; int res; //用來接收函數(shù)返回值 int main() { printf("------Welcome join the chat room-----\n"); printf("If you want to quit,please input --bye--\n"); int sockfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in saddr; saddr.sin_family = AF_INET; saddr.sin_port = htons(6666); saddr.sin_addr.s_addr = inet_addr("127.0.0.1"); int confd = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr)); if(confd < 0) perror("connect"); while(1) { printf("Please input the news\n"); fgets(news,MAXSIZE,stdin); if(strncmp(news,"bye",3) == 0) { break; } send(sockfd, news, strlen(news), 0); memset(news,0,MAXSIZE); recv(sockfd, news, sizeof(news), 0); printf("The serve's news is: %s\n",news); } close(sockfd); exit(0); }
請注意,服務(wù)端由于使用了多線程開發(fā),需要在編譯時(shí)添加-lpthread選項(xiàng)
程序運(yùn)行效果如下:
原文鏈接:https://blog.51cto.com/u_15446743/4722630
相關(guān)推薦
- 2023-01-17 用Python實(shí)現(xiàn)的等差數(shù)列方式_python
- 2022-12-10 C++?Boost?Spirit進(jìn)階教程_C 語言
- 2023-02-15 清理或刪除docker無用鏡像的操作方法_docker
- 2022-09-06 C#面向?qū)ο缶幊讨薪涌诟綦x原則的示例詳解_C#教程
- 2022-12-13 Android?DataBinding單向數(shù)據(jù)綁定深入探究_Android
- 2022-05-13 c++ remove_reference
- 2022-09-15 關(guān)于c++11與c風(fēng)格路徑拼接的速度對比_C 語言
- 2022-06-16 c語言單詞搜索的實(shí)現(xiàn)_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支