費勁的折騰,好幾次小細節沒處理好,真是醉了,不說了
**記錄下
服務端:
//fifo(first in, first out)
//有名管道
#include #include #include #include #include #include #include #include #include #include #include using namespace std;
#define file_mode (s_irusr | s_iwusr | s_irgrp | s_iroth)
#define fifo_0 "/tmp/fifo0"
const int size=256;
struct data
;int server(int &readfd,int &writefd);
//server
//作用:多個客戶端通過fifo向server傳來檔案路徑名,server返回路徑名的反序
int main()
readfd=open(fifo_0,o_rdonly,0);
writefd=open(fifo_0,o_wronly,0);
cout << "readfd=" << readfd << endl;
cout << "writefd=" << writefd << endl;
server(readfd,writefd);
return 0;
}int fifo_ret(data & data);
int server(int & readfd,int & writefd)
; size_t n=0;
size_t len=0;
char sbuf[256]= ;
while(true)
len = data.nlen;
if(read(readfd,&data.pid,sizeof(pid_t)) == sizeof(pid_t) && \
len == read(readfd,data.sbuf,len))
}}void reverse(char * from, char * to)
}int fifo_ret(data & data)
}int writefd;
sleep(3);
if( (writefd=open(pathname,o_wronly,0)) < 0)
perror("open fifo");
char stmp[256]=;
reverse(data.sbuf,stmp);
cout << stmp << endl;
write(writefd,stmp,strlen(stmp));
close(writefd);
}
客戶端:
#include #include #include #include #include #include #include #include #include #include #include using namespace std;
#define file_mode (s_irusr | s_iwusr | s_irgrp | s_iroth)
#define fifo_0 "/tmp/fifo0"
const int size=256;
struct data
;int main()
int readfd,writefd;
char pathname[size]="/tmp/fifo";
int pid=(int)getpid();
char sbuf[256]=;
memset(sbuf,0x00,sizeof(sbuf));
sprintf(sbuf,"%d",pid);
strcat(pathname,sbuf);
if(mkfifo(pathname,file_mode) < 0)
}//多客戶端向服務端傳送字串,伺服器返回反序
writefd=open(fifo_0,o_wronly,0);
sprintf(sbuf,"%d",pid);
char stmp[256]=;
data data;
while(1) //與伺服器通訊
{memset(sbuf,0x00,sizeof(sbuf));
memset(stmp,0x00,sizeof(stmp));
memset(&data,0x00,sizeof(data));
cout << "input:[# quit]" << endl;
cin >> data.sbuf;
if(data.sbuf[0] == '#')
break;
data.pid=pid;
data.nlen=strlen(data.sbuf);
write(writefd,&data,data.nlen+sizeof(data.nlen)+sizeof(data.pid));
readfd=open(pathname,o_rdonly,0);
read(readfd,stmp,sizeof(stmp));
cout << "recv:" <
nodejs 伺服器實現區分多客戶端請求服務
初始實現 var net require net 1 引入net模組 var chatserver net.createserver 建立net伺服器 var clientlist 儲存多個客戶端的陣列 chatserver.on connection function client chatser...
Python Http 多客戶端和伺服器(序列)
伺服器資訊 客戶端資訊列表 輪詢伺服器和客戶端發起http請求 myhttpconn.debuglevel 0 除錯模式開關 myhttpconn.timeout 3 設定連線超時等待 單位 s myhttpconn.request get httpserver filepath 資源路徑以 開始 ...
C Socket多客戶端和伺服器通訊
1 總述 關於socket通訊主要採用了c 程式設計,實現多執行緒通訊,多個客戶端同時和伺服器通訊,並支援伺服器以廣播的形式向所有客戶端傳送訊息,該歷程未加介面。具體的細節和原始碼見下分析 2 執行流程 伺服器端 執行程式後,輸入 s 啟動伺服器,並提示下一步該怎樣操作。此時可以連線客服端,當有客戶...