實現Linux select IO復用C/S服務器代碼

服務器端#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#include <sys/select.h>#define MAXBUF 256
#define MAXLISTEN 8
#define PORT 8888struct msgtemp 
{
int num;
char *s;
};int main()
{
int clen,dirnum,opt=1,i,nbyte;
int listenfd,clientfd,maxfd;
int client[FD_SETSIZE];struct sockaddr_in client_addr,server_addr;
char readbuf[MAXBUF],writebuf[MAXBUF];
struct msgtemp msg[FD_SETSIZE];
fd_set rset, allset;if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket error:");
exit(1);
}
clen = sizeof(client_addr);
bzero(&server_addr,0);
server_addr.sin_family =AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT);setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(char*)&opt,sizeof(opt)); 
if(bind(listenfd,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){
perror("bind error");
exit(1);
}
if(listen(listenfd,MAXLISTEN)<0){
perror("listen error:");
exit(1);
}maxfd = listenfd;
for (i = 0; i < FD_SETSIZE; i++) 
client[i] = -1;FD_ZERO(&allset);
FD_SET(listenfd, &allset);while(1){
rset = allset;
if(select(maxfd+1, &rset, NULL, NULL, NULL)<0)
{
perror("select");
return -1;
}
if (FD_ISSET(listenfd, &rset)) { /* 是否有新的連接進來*/
clientfd=accept(listenfd,(struct sockaddr *)&client_addr,&clen);
if(clientfd < 0)
{
perror("accept");
}
printf("new connection fd = %d\n",clientfd);
FD_SET(clientfd, &allset);
maxfd = clientfd > maxfd ? clientfd : maxfd;
for (i = 0; i < FD_SETSIZE; i++)
if (client[i] < 0) {
client[i] = clientfd; 
break;
}
}
for (i = 0; i < FD_SETSIZE; i++)
{
if (FD_ISSET(client[i], &rset)) {
if ( (nbyte = read(client[i], readbuf, MAXBUF)) < 0) {
perror("read");
continue;} 
else if (nbyte ==0)	
{
close(client[i]);
FD_CLR(client[i], &allset); 
printf("connection fd = %d closed\n",client[i]);
client[i] = -1;
msg[i].num =0;
msg[i].s = NULL;}
else{
printf("recv msg from fd = %d : %s\n",client[i],readbuf);msg[i].s = readbuf;
sprintf(writebuf,"%03d : %s",msg[i].num,msg[i].s);
write(client[i], writebuf, strlen(writebuf)+1);
msg[i].num ++;
}}
}}
return 0;
}客戶端#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#include<fcntl.h>#define MAXBUF 256
#define PORT 8888int main()
{
int ssock;
int clen,readbytes,fd2,i;
struct sockaddr_in server_addr;
char writebuf[MAXBUF],readbuf[MAXBUF],file_path[MAXBUF];if((ssock=socket(AF_INET,SOCK_STREAM,0))<0){
perror("socket error:");
exit(1);
}
clen = sizeof(server_addr);
bzero(&server_addr,0);
server_addr.sin_family =AF_INET;
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
server_addr.sin_port =htons(PORT);
if(connect(ssock,(struct sockaddr *)&server_addr,clen)<0){
perror("connect error:");
exit(1);
}while(1) 
{
printf("input msg:");
fgets(writebuf,MAXBUF,stdin);
if(write(ssock,writebuf,MAXBUF)<0)
{
perror("write error:");
}readbytes=read(ssock,readbuf,MAXBUF);
if( readbytes <0 )
{perror("read error:");
exit(1);
}
if( readbytes ==0 )
{
printf("connection closed \n");
break;
}printf("%s\n",readbuf);
}close(ssock);return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383630.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383630.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383630.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Bellman-Ford算法和SPFA算法

Belloman-Ford算法 算法介紹 Dijkstra可以解決單源無負邊最短路徑問題。但是當遇到含有負邊的單源最短路徑問題就需要使用Bellman-Ford算法來解決。Bellman-Ford算法還可以檢測出負環。 算法步驟 源點s,數組d[u]d[u]d[u]表示s到u的最短距離初始化&#xff1a;d[s]0d[s]0d[s…

C語言實現單鏈表操作

SLIST_H #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #include<assert.h> typedef int ElemType; typedef struct Node { //定義單鏈表中的結點信息 ElemType data; //結點的數據域 struct Node *next; //結點的指針…

計算機網絡【4】傳輸層

概述 傳輸層是只有主機才有的層次 傳輸層的功能&#xff1a; 傳輸層提供進程和進程之間的邏輯通信&#xff08;網絡層提供主機與主機之間的邏輯通信&#xff09;復用和分用傳輸層對收到的報文進行差錯檢測 傳輸層有兩個協議&#xff1a; 面向連接的傳輸層控制協議TCP&…

Plotly繪圖

在做Python數據分析實驗的時候發現使用Plotly庫繪圖比較漂亮&#xff0c;在網上找到了一個比較好的教程&#xff0c;這里記錄一下&#xff0c;方便以后查找。 傳送門

計算機網絡【0】概述

計算機網絡概念和功能 概念 是一個將分散的、具有獨立功能的計算機系統&#xff0c;通過通信設備與線路連接起來&#xff0c;由功能完善的軟件實現資源共享和信息傳遞的系統。 計算機網絡是互連的、自治&#xff08;無主從關系&#xff09;的計算機集合。 功能 數據通信&am…

計算機網絡【1】物理層

物理層解決如何在連接各種計算機的傳輸媒體上傳輸數據比特流&#xff0c;而不是指具體的傳輸媒體。 確定與傳輸媒體接口有關的特性 機械特性&#xff1a;定義物理連接的特性&#xff0c;如規格、接口形狀、引線數目、引腳數目、排列電氣特性&#xff1a;規定傳輸二進制位時的電…

計算機網路【2】數據鏈路層

結點&#xff1a;主機、路由器 鏈路&#xff1a;兩個節點的物理通道 數據鏈路&#xff1a;邏輯通道&#xff0c;把實現 控制數據傳輸協議的硬件和軟件加到鏈路上就構成數據鏈路 幀&#xff1a;鏈路層的協議數據單元&#xff0c;封裝網絡層數據報 數據鏈路層在物理層提供服務的…

計算機網絡【5】應用層

應用層對應用程序的通信提供服務 應用層協議定義&#xff1a; 應用層的功能&#xff1a; 文件傳輸、訪問和管理電子郵件虛擬終端查詢服務和遠程作業登錄 重要協議&#xff1a;FTP、SMTP、POP3、HTTP、DNS 網絡應用模型 客戶/服務器模型&#xff08;Client/Server&#x…

操作系統【八】文件管理

文件&#xff1a;一組有意義的信息/數據集合 文件的屬性&#xff1a; 文件名&#xff1a;由創建文件的用戶決定文件名&#xff0c;主要是為了方便用戶找到文件。同一個目錄下不允許有重名文件標識符&#xff1a;一個系統內的個文件標識符唯一&#xff0c;對用戶來說毫無可讀性…

數據庫原理及應用【六】數據庫設計

數據依賴 函數依賴FD&#xff1a;一個屬性或者一組屬性的值可以決定另一個屬性的值 多值依賴MVD&#xff1a;一個屬性或者一組屬性的值可以決定另一個屬性的值的集合。FD是MVD的特例 符號表示&#xff1a;Name->->Course&#xff0c;課程多值依賴于姓名 連接依賴&#x…

數據可視化【一】JavaScript學習

本博客是我學習Curran Kelleher老師數據可視化課程的筆記&#xff0c;感興趣的小伙伴可以點擊這里學習。 three cores of data visualization: analysisdesignconstruction 推薦書籍《visualization analysis & design》 使用https://vizhub.com/進行編程學習&#xff…

數據庫原理及應用【二】數據模型

層次模型 tree Record and fieldParent-Child relationship(PCR) 每個記錄類型只有一個父節點 無法表達多對多信息 采用虛記錄解決多對多 網狀數據模型 系&#xff1a;主記錄->屬記錄 主記錄和屬記錄都可以有好多個 關系模型 表&#xff1a;table/relation 擁有更高的…

數據可視化【二】HTML+CSS+SVG+D3

HTML、CSS和SVG學習實現代碼&#xff1a;https://vizhub.com/Edward-Elric233/89185eb96bc64a9d81777873a0ccd0b9 index.html <!DOCTYPE html> <html><head><title>Shapes with SVG and CSS</title><link rel"stylesheet" href&qu…

數據可視化【三】基本概念

Visualization is suitable when there is a need to augment human capabilities rather than replace people with computational decision-making methods. 當可以信賴的智能化的解決方案存在的時候&#xff0c;可視化是不必要的。 當不知道需要分析的問題是什么的時候&…

數據可視化【四】Bar Chart

Make a Bar Chart Representing a data table in JavaScriptCreating rectangles for each rowUsing linear and band scalesThe margin conventionAdding axes 以下學習內容參考博客&#xff1a;傳送門 select()選擇所有指定元素的第一個 selectAll()選擇指定元素的全部 上…

數據庫原理及應用【三】DBMS+SQL

DBMS Query LanguagesInterface and maintaining tools(GUI)APIsClass Library QL 不是圖靈完備的&#xff0c;不是一種編程語言。 QL SQL是一種非過程化的查詢語言。 DDL數據定義語言&#xff1a;表&#xff0c;視圖QL 查詢語言DML 數據操縱語言DCL 數據控制語言 Base t…

數據可視化【五】 Scatter Plot

Scatter Plot vizhub上實現的代碼&#xff1a; https://vizhub.com/Edward-Elric233/53807a1b35d94329b3689081cd2ea945 https://vizhub.com/Edward-Elric233/b9647d50899a4a0e8e917f913cd0a53a https://vizhub.com/Edward-Elric233/8c6b50cd81a04f048f490f48e4fe6264 由前…

數據可視化【六】Line Chart Area Chart

Line Chart vizhub代碼&#xff1a; https://vizhub.com/Edward-Elric233/094396fc7a164c828a4a8c2e13045308 實現效果&#xff1a; 這里先使用d3.line()設置每個點的x坐標和y坐標&#xff0c;然后再用這個東西設置path的d屬性&#xff0c;就可以得到曲線。 const lineGen…

數據可視化【七】 更新模式

Enter 以下面這個簡單的代碼進行分析 const svg d3.select(svg); // svg.style(background-color, red); testconst height svg.attr(height); // equals paresFloat() const width svg.attr(width);const makeFruit type >( {type} ); //這種寫法好像能夠直接得到一個…

數據可視化【八】根據數據類型選擇可視化方式

Marks:Rows PointsLinesAreas Channels:Columns PositionColorShape