windows socket编程UDP篇

winsock的UDP编程示例:

当不指定参数时将以serveryunxing

指定参数运行,如:udp.exe localport remoteIp remotePort时以client运行。

详细参阅另一篇关于TCP的http://blog.csdn.net/guo8113/article/details/26448011

#include 
#include 
#include 
using namespace std;
#pragma comment(lib, "ws2_32.lib")
//usage: app [localport] [remote ip] [remote port] 
int main(int argc,char* argv[])
{
	const int DEFAULT_PORT = 5050;
	const int DATA_BUFFER_LENGTH = 1024;
	SOCKET locatesocket;
	SOCKADDR_IN locateAddr,remoteAddr;
	int locatePort,remotePort;

	int err;
	int iLen,iSend,iRecv;				//length
	char apply_Message[]="I am a Client!\n";//request for connect info
	char send_buf[DATA_BUFFER_LENGTH]="\0",recv_buf[DATA_BUFFER_LENGTH]="\0";
	bool clientFlag = false;			//客户端标志


	WORD wVsersionRequested;
	WSADATA wsaData;

	//acccording the argvs to set socket parameter
	if(argc<4){
		cout<<"server waitting "<0){
			cout<<"Message:"<0){
			cout<<"Receive:"<0){
			cout<<"Message:"<
书籍:《TCP/IP--协议分析与应用编程》李峰等编著7.6

转载注明:http://blog.csdn.net/guo8113/article/details/26448011

你可能感兴趣的:(windows socket编程UDP篇)