#include#include#include#include#include#include#include#include int main(int argc, char** argv)
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = af_inet;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if((connect(socketfd, &server_addr, sizeof(server_addr))) == -1)
while(1)
close(socketfd);
exit(0);
}
#include#include#include#include#include#include#include#include #include int main(int argc, char** argv)
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = af_inet;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = htonl(inaddr_any);
if((bind(old_fd, &server_addr, sizeof(server_addr))) == -1)
//listen的第二個引數backlog為核心快取佇列長度,該佇列針對每個監聽套接字包含兩個佇列:
//一是未完成三次握手的,二是完成三次握手的
if(listen(old_fd, 10) == -1)
while(1)
while(1)
nbytes = recv(new_fd, recvbuf, sizeof(recvbuf), 0);
recvbuf[nbytes] = '\0';
printf("recv data is:%s\n",recvbuf);
}close(new_fd);
}close(old_fd);
exit(0);
}
Linux 網路程式設計 TCP
一 tcp通訊步驟 tcp伺服器部分 1 呼叫函式socket 建立乙個socket 2 設定sockaddr in資訊,如要連線伺服器的ip和埠等屬性 3 呼叫函式bind 繫結ip位址 埠等資訊到socket上 4 呼叫函式listen 設定允許的最大連線數 5 呼叫函式accept 等待來自客...
linux網路程式設計 初探TCP
伺服器端等待客戶端連線,連線成功後,列印客戶端的ip和port,然後迴圈接收資料,緩衝區無資料就阻塞待待。include include include include include include include include include include define maxbuf 10 ...
Linux下TCP網路程式設計流程
常見的兩種模型 c s模型 客戶端 伺服器端 和ptp 點對點 模型。下面為tcp網路流程程式設計兩台主機之間資料傳送的具體 示例。示例 檔案server.c include include include include include include include include void m...