昨天弄了sublime之後沒有弄輸入中文的了,學生黨來著,剛好可以練練英語(英語渣渣,還要考六級),所以注釋都寫英文的(語法什麼的就別太深究了)
伺服器端:
/*start from the very beginning,and create greatness
@name:chuangwei lin
@e-mail:[email protected]
@brief:a ****** example about server/client link(server c file)
*/#include
#include
#include
#include
#include
#include
#include
#define port 8888 //port to listen
#define backlog 2 //the length of listen queue
void process_conn_server(int s);
int main(int argc, char
const *argv)
//set the server address
bzero(&server_addr,sizeof(server_addr)); //clear
server_addr.sin_family = af_inet; //protocol family
server_addr.sin_addr.s_addr = htonl(inaddr_any);//local address
server_addr.sin_port = htons(port); //server port
//binging
err = bind(ss,(struct sockaddr*)&server_addr,sizeof(server_addr));
if (err < 0)
//listening
err = listen(ss,backlog);
if (err < 0)
//the main loop
for (;;)
//create a new process to process the connecting
pid = fork();
if (0 == pid)
else
}return0;}
//the process function
void process_conn_server(int s)
sprintf(buffer,"%d bytes altogether\n",size); //send the number of data return
write(s,buffer,strlen(buffer)+1);
}}
客戶端:
/*start from the very beginning,and create greatness
@name:chuangwei lin
@e-mail:[email protected]
@brief:a ****** example about server/client link(client c file)
*/#include
#include
#include
#include
#include
#include
#include
#define port 8888 //port to listen
void process_conn_client(int s);
int main(int argc, char
const *argv)
//set the server address
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = af_inet; //protocol family
server_addr.sin_addr.s_addr = htonl(inaddr_any); //local address
server_addr.sin_port = htons(port); //server port
//change the user input char ip to int
inet_pton(af_inet,argv[1],&server_addr.sin_addr);
//conneting the server
connect(s,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
process_conn_client(s);
close(s);
return0;}
void process_conn_client(int s)
}}
很簡單的一段,只是建立連線然後客戶端傳送字元然後伺服器統計後返回 TCP伺服器 客戶端程式
server tcpserver.cpp檔案 include common initsock.h include cinitsock initsock 初始化winsock庫 int main 填充sockaddr in結構 sockaddr in sin sin.sin family af ine...
TCP主機客戶端與Linux伺服器
1.關閉雙方防火牆 service iptables stop 2.檢視雙方ip位址 l ifconfig w ipconfig 3.雙方 ping linux可適當重新整理橋接 客戶端 1.設立通訊關鍵字 qtcpsocket 2.主動鏈結 connecttohost ip,埠 3.傳送訊息 qt...
伺服器和客戶端 tcp通訊
在vs2013的環境下,伺服器 如下 include include stdafx.h include pragma comment lib,ws2 32.lib using namespace std define buffer rev 1024 define buffer send 1024 i...