1)client 從標準輸入讀入一行文字,並寫給伺服器。
2)伺服器從網路輸入讀入這行文字,並回射給客戶。
3)客戶從網路輸入讀入這行回射的文字,顯示在標準輸出上
標準輸入—–fgets—-> tcp 客戶—write——————-read–>tcp 伺服器
標準輸出<—-fputs—–tcp 客戶<—read——————-write—tcp 伺服器
client **
#include
#include /* see notes */
#include
#include
#include
#include
void str_cli(file *fp, int sockfd)
fputs(recvline, stdout);
}}int main(int argc , char** argv)
if((sockfd = socket(af_inet, sock_stream, 0)) < 0)
bzero(&seraddr, sizeof(seraddr));
seraddr.sin_family = af_inet;
seraddr.sin_port = htons(13000);
if((inet_pton(af_inet, argv[1], &seraddr.sin_addr)) < 0)
if(connect(sockfd, (struct sockaddr*)&seraddr, sizeof(seraddr)) < 0)
str_cli(stdin, sockfd);
return
0;}
server **
#include
#include /* see notes */
#include
#include
#include
#include
#include
#include
void str_echo(int sockfd)
if(n < 0)
}int main(int argc, char** argv)
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = af_inet;
servaddr.sin_port = htons(13000);
servaddr.sin_addr.s_addr = inet_addr("0.0.0.0");
if( bind(listenfd, (struct sockaddr* )&servaddr, sizeof(servaddr)) < 0)
if(listen(listenfd, 0) < 0)
for( ; ;)
close(connectfd);
}}
TCP客戶 伺服器程式示例
1.tcp回射示例 伺服器 include include include include include include in.h define srv port 8888 define maxline 4096 void str echo int fd int main int argc,cha...
TCP客戶 伺服器程式例項
1.概述 這章的tcp客戶 伺服器模型 2.tcp回射伺服器程式 1 main函式 include unp.h int main int argc,char argv close connfd parent closes connected socket 2 str echo函式 include u...
TCP客戶 伺服器程式例項
server.cpp include include include include include include include include include include include include include define ip 127.0.0.1 define port 888...