最簡單client和server通訊程式

2021-08-24 19:22:43 字數 1429 閱讀 1975

開兩個vc或者建兩個工程,先執行server再執行client

server端:

#include

#include

#pragma comment(lib,"ws2_32.lib")

void main()

else

socket sersocket=socket(af_inet,sock_stream,0);//建立了可識別套接字

//需要繫結的引數

sockaddr_in addr;

addr.sin_family=af_inet;

addr.sin_addr.s_un.s_addr=htonl(inaddr_any);//ip位址

addr.sin_port=htons(6000);//繫結埠

bind(sersocket,(sockaddr*)&addr,sizeof(sockaddr));//繫結完成

listen(sersocket,5);//其中第二個引數代表能夠接收的最多的連線數

////開始進行監聽

//sockaddr_in clientsocket;

int len=sizeof(sockaddr);

while (1)

}client端:

#include

#include

#pragma comment(lib,"ws2_32.lib")

void main()

else

socket clientsocket=socket(af_inet,sock_stream,0);

sockaddr_in clientsock_in;

clientsock_in.sin_addr.s_un.s_addr=inet_addr("127.0.0.1");

clientsock_in.sin_family=af_inet;

clientsock_in.sin_port=htons(6000);

//bind(clientsocket,(sockaddr*)&clientsock_in,strlen(sockaddr));//注意第三個引數

//listen(clientsocket,5);

connect(clientsocket,(sockaddr*)&clientsock_in,sizeof(sockaddr));//開始連線

char receivebuf[100];

recv(clientsocket,receivebuf,101,0);

printf("%s/n",receivebuf);

send(clientsocket,"hello,this is client",strlen("hello,this is client")+1,0);

closesocket(clientsocket);

wsacleanup();

}

簡單的server和client模型

本 主要有一下幾個技術的解決方案 埠復用技術 防止tcp流協議粘包 我們使用的是指定buf大小的方式 include include include include include include include include include define err exit m do while ...

scroll offset和client如何區分

title 比較 1.clientx與clienty window.nl ad function oevent.clientx是指滑鼠到可視區左邊框的距離。oevent.clienty是指滑鼠到可視區上邊框的距離。2.offsetwidth offsetheight offsetleft和offse...

scroll offset和client的區別

整體布局 12 title 比較 1.clientx與clienty window.onload function oevent.clientx是指滑鼠到可視區左邊框的距離。oevent.clienty是指滑鼠到可視區上邊框的距離。2.offsetwidth offsetheight offsetl...