socket 编程通信实例:
TCPserver:
, ServerThread, ;
WSADATA wsaData;
,), ;
}
;
}
} ;
gethostname ( szHostName, DEF_BUF_SIZE ) ;
hostent);
memcpy ( ], pHost;
}
;
}
} ;
) ;
)
{
OutputMessage ( L ) ;
)
{
OutputMessage ( L ;
}
TCPclient:
,), } ;
gethostname ( szHostName, DEF_BUF_SIZE ) ;
hostent], pHost ;
WSADATA wsaData;
,), ;
} ;
);
RemoteAddr.sin_addr.s_addr } ;
sprintf ( szData, ) ;
)
{
OutputMessage ( L ) ;
)
{
OutputMessage ( L, ServerThread, , NULL ) ;
;
WSADATA wsaData;
,), ;
}
;
}
} ;
gethostname ( szHostName, DEF_BUF_SIZE ) ;
hostent ;
sockaddr_in SrvAddr, ClientAddr ;
SrvAddr.sin_family );
memcpy ( ], pHost;
}
} ;
, (sockaddr )
{
OutputMessage ( L ;
}
UDPclient:
;
WSADATA wsaData;
,), } ;
);
SrvAddr.sin_addr.s_addr } ;
, (sockaddr )
{
OutputMessage ( L"发送数据错误!", GetLastError() ) ;
return ;
}
// 关闭套接字句柄
closesocket ( LocalSock ) ;
WSACleanup();
}