本程序僅僅是為了演示基於TCP協議
服務器端代碼
#include
#include
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(
err = WSAStartup( wVersionRequested
if ( err !=
{
return;
}
if ( LOBYTE( wsaData
HIBYTE( wsaData
{
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET
SOCKADDR_IN addrSrv;
addrSrv
addrSrv
addrSrv
bind(sockSrv
listen(sockSrv
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(
{
SOCKET sockConn=accept(sockSrv
char sendBuf[
sprintf(sendBuf
send(sockConn
char recvBuf[
recv(sockConn
printf(
closesocket(sockConn);
}
}
『注意』本程序使用了動態連接庫文件Ws
客戶端代碼
#include
#include
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(
err = WSAStartup( wVersionRequested
if ( err !=
{
return;
}
if ( LOBYTE( wsaData
HIBYTE( wsaData
{
WSACleanup( );
return;
}
SOCKET sockClient=socket(AF_INET
SOCKADDR_IN addrSrv;
addrSrv
一個回路地址
addrSrv
addrSrv
connect(sockClient
char recvBuf[
recv(sockClient
printf(
send(sockClient
closesocket(sockClient);
WSACleanup();
}
同服務器端一樣
From:http://tw.wingwit.com/Article/program/net/201311/13794.html