方法/步驟
1
1.伺服器端的程式碼:
void
SocketServer::CreateConnect(){std::cout<<"startCreate
Socket!"<<std::endl;m_nSocket=-1;structsockaddr_in
server_addr;structsockaddr_inclient_addr;int
portnumber=4321;if((m_nSocket=socket(AF_INET,SOCK_STREAM,0))==-1)
{std::cout<<"socketCreate
Failed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"CreateSocketFinish!"<<std::endl;/*
伺服器端填充sockaddr結構*/bzero(&server_addr,sizeof(struct
sockaddr_in));server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
server_addr.sin_port=htons(portnumber);/*捆綁sockfd描述符*/
if(bind(m_nSocket,(structsockaddr*)(&server_addr),sizeof(struct
sockaddr))==-1){std::cout<<"ServerBind
std::cout<<"bindSocketFinish!"<<std::endl;/*
監聽sockfd描述符*/if(listen(m_nSocket,5)==-1){
std::cout<<"Listen
std::cout<<"listenSocketFinish!"<<std::endl;
while(1){/*伺服器阻塞,直到客戶程式建立連線*/
std::cout<<"listenstart..."<<std::endl;int
nNewFd=-1;size_tnSize=sizeof(structsockaddr_in);
if((nNewFd=accept(m_nSocket,(structsockaddr
*)(&client_addr),&(nSize)))==-1){
std::cout<<"accept
Failed!"<<strerror(errno)<<std::endl;return;
}std::cout<<"serverGetConnect
from:"<<inet_ntoa(client_addr.sin_addr)<<std::endl;
while(1){charbuf[256]="0";size_t
len=recv(nNewFd,buf,256,0);if(len>0)
std::cout<<"接受的訊息為:"<<buf<<std::endl;
else{if(len<0)
std::cout<<"錯誤為:"<<strerror(errno)<<std::endl;
else
std::cout<<"客戶端斷開:"<<std::endl;break;
}}/*這個通訊已經結束*///close(nNewFd);
/*迴圈下一個*/}}
2
2.客戶端程式碼:
//
建立連結voidSocketClient::CreateConnect(){charbuffer[1024];struct
sockaddr_inserver_addr;std::cout<<"socketcreate
start!"<<std::endl;/*客戶程式開始建立sockfd描述符*/
if((m_nSocket=socket(AF_INET,SOCK_STREAM,0))==-1){
std::cout<<"ClientsocketCreate
Failed!!"<<strerror(errno)<<std::endl;return;}
std::cout<<"socketcreatefinshed!"<<std::endl;/*
客戶程式填充服務端的資料*/intportnumber=4321;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");/*
客戶程式發起連線請求*/if(connect(m_nSocket,(structsockaddr
*)(&server_addr),sizeof(structsockaddr))==-1){
std::cout<<"Clientconnect
Fialed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"connectfinshed!"<<std::endl;std::string
str="start...";while(!str.empty()){
std::cout<<"starting....!"<<std::endl;
std::cin>>str;size_tlen=send(m_nSocket,str.c_str(),
str.length(),0);if(len<0){
std::cout<<"訊息傳送失敗"<<str<<strerror
(errno)<<std::endl;}else{
std::cout<<"訊息傳送成功"<<str<<std::endl;}}
close(m_nSocket);}
3
3.編譯完成後,啟動伺服器端程式碼和客戶端程式碼就可以進行通訊了。
4
4.
普及一下TCP/IP協議的部分知識,大家都知道TCP是面向連結的協議,即在進行通訊前伺服器是被動連結,客戶端是主動連結,那麼客戶端與伺服器端的連
接需要經過三次握手才能建立連結。在整個通訊過程中客戶端與伺服器端的進行的通訊都有確認機制,保證包能順利的到達對方,但這也不是100%,中間的路由
等中間橋出現問題,也有可能造成資料包的丟失,而任何一方沒有收到確認包,都會一直處於等待和重傳的過程中...
伺服器:
建立socket->bind->listen->recv(send)->close
客戶端:
建立socket->connect->recv(send)->close
方法/步驟
1
1.伺服器端的程式碼:
void
SocketServer::CreateConnect(){std::cout<<"startCreate
Socket!"<<std::endl;m_nSocket=-1;structsockaddr_in
server_addr;structsockaddr_inclient_addr;int
portnumber=4321;if((m_nSocket=socket(AF_INET,SOCK_STREAM,0))==-1)
{std::cout<<"socketCreate
Failed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"CreateSocketFinish!"<<std::endl;/*
伺服器端填充sockaddr結構*/bzero(&server_addr,sizeof(struct
sockaddr_in));server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
server_addr.sin_port=htons(portnumber);/*捆綁sockfd描述符*/
if(bind(m_nSocket,(structsockaddr*)(&server_addr),sizeof(struct
sockaddr))==-1){std::cout<<"ServerBind
Failed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"bindSocketFinish!"<<std::endl;/*
監聽sockfd描述符*/if(listen(m_nSocket,5)==-1){
std::cout<<"Listen
Failed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"listenSocketFinish!"<<std::endl;
while(1){/*伺服器阻塞,直到客戶程式建立連線*/
std::cout<<"listenstart..."<<std::endl;int
nNewFd=-1;size_tnSize=sizeof(structsockaddr_in);
if((nNewFd=accept(m_nSocket,(structsockaddr
*)(&client_addr),&(nSize)))==-1){
std::cout<<"accept
Failed!"<<strerror(errno)<<std::endl;return;
}std::cout<<"serverGetConnect
from:"<<inet_ntoa(client_addr.sin_addr)<<std::endl;
while(1){charbuf[256]="0";size_t
len=recv(nNewFd,buf,256,0);if(len>0)
std::cout<<"接受的訊息為:"<<buf<<std::endl;
else{if(len<0)
std::cout<<"錯誤為:"<<strerror(errno)<<std::endl;
else
std::cout<<"客戶端斷開:"<<std::endl;break;
}}/*這個通訊已經結束*///close(nNewFd);
/*迴圈下一個*/}}
2
2.客戶端程式碼:
//
建立連結voidSocketClient::CreateConnect(){charbuffer[1024];struct
sockaddr_inserver_addr;std::cout<<"socketcreate
start!"<<std::endl;/*客戶程式開始建立sockfd描述符*/
if((m_nSocket=socket(AF_INET,SOCK_STREAM,0))==-1){
std::cout<<"ClientsocketCreate
Failed!!"<<strerror(errno)<<std::endl;return;}
std::cout<<"socketcreatefinshed!"<<std::endl;/*
客戶程式填充服務端的資料*/intportnumber=4321;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");/*
客戶程式發起連線請求*/if(connect(m_nSocket,(structsockaddr
*)(&server_addr),sizeof(structsockaddr))==-1){
std::cout<<"Clientconnect
Fialed!"<<strerror(errno)<<std::endl;return;}
std::cout<<"connectfinshed!"<<std::endl;std::string
str="start...";while(!str.empty()){
std::cout<<"starting....!"<<std::endl;
std::cin>>str;size_tlen=send(m_nSocket,str.c_str(),
str.length(),0);if(len<0){
std::cout<<"訊息傳送失敗"<<str<<strerror
(errno)<<std::endl;}else{
std::cout<<"訊息傳送成功"<<str<<std::endl;}}
close(m_nSocket);}
3
3.編譯完成後,啟動伺服器端程式碼和客戶端程式碼就可以進行通訊了。
4
4.
普及一下TCP/IP協議的部分知識,大家都知道TCP是面向連結的協議,即在進行通訊前伺服器是被動連結,客戶端是主動連結,那麼客戶端與伺服器端的連
接需要經過三次握手才能建立連結。在整個通訊過程中客戶端與伺服器端的進行的通訊都有確認機制,保證包能順利的到達對方,但這也不是100%,中間的路由
等中間橋出現問題,也有可能造成資料包的丟失,而任何一方沒有收到確認包,都會一直處於等待和重傳的過程中...
伺服器:
建立socket->bind->listen->recv(send)->close
客戶端:
建立socket->connect->recv(send)->close