基于UDP的网络通讯(socket)

1、 无连接编程(对等编程UDP)

     UDP编程流程

①  UDP套接字创建(socket)

②    地址与端口的绑定(bind)

③    数据收发       (sendto\recvfrom)

④    套接字关闭   (close)


CLIENT;

#include <iostream>
#include <windows.h>
using namespace std;

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

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	
	wVersionRequested = MAKEWORD( 2, 2 );
	
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		return;
	}
	
	/* Confirm that the WinSock DLL supports 2.2.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.2 in addition to 2.2, it will still return */
	/* 2.2 in wVersion since that is the version we      */
	/* requested.                                        */
	
	if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		WSACleanup( );
		return; 
	}
	
	/* The WinSock DLL is acceptable. Proceed. */
	
	SOCKET sockCli;
	sockCli = socket(AF_INET,SOCK_DGRAM,0);
	
	SOCKADDR_IN addrSer,addrCli;
	addrCli.sin_family = AF_INET;
	addrCli.sin_port = htons(5050);
	addrCli.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	
	char sendBuf[256];
	char recvBuf[256];
	int len = sizeof(SOCKADDR);
	while(1)
	{
		cout<<"Cli :> ";
		cin>>sendBuf;
		sendto(sockCli,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrCli,sizeof(SOCKADDR));
		recvfrom(sockCli,recvBuf,256,0,(SOCKADDR*)&addrCli,&len);
		cout<<"Ser :> "<<recvBuf<<endl;
		
		
				
	}
	closesocket(sockCli);
	WSACleanup();
}
SERVER:

#include <iostream>
#include <windows.h>
using namespace std;

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

void main()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	
	wVersionRequested = MAKEWORD( 2, 2 );
	
	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		return;
	}
	
	/* Confirm that the WinSock DLL supports 2.2.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.2 in addition to 2.2, it will still return */
	/* 2.2 in wVersion since that is the version we      */
	/* requested.                                        */
	
	if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
		/* Tell the user that we could not find a usable */
		/* WinSock DLL.                                  */
		WSACleanup( );
		return; 
	}
	
	/* The WinSock DLL is acceptable. Proceed. */

	SOCKET sockSer;
	sockSer = socket(AF_INET,SOCK_DGRAM,0);
	
	SOCKADDR_IN addrSer,addrCli;
	addrSer.sin_family = AF_INET;
	addrSer.sin_port = htons(5050);
	addrSer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	bind(sockSer,(SOCKADDR*)&addrSer,sizeof(SOCKADDR));

	char sendBuf[256];
	char recvBuf[256];
	int len = sizeof(SOCKADDR);
	while(1)
	{
		recvfrom(sockSer,recvBuf,256,0,(SOCKADDR*)&addrCli,&len);
		cout<<"Cli :> "<<recvBuf<<endl;
		cout<<"Ser :> ";
		cin>>sendBuf;	
		sendto(sockSer,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrCli,sizeof(SOCKADDR));		
	}
	closesocket(sockSer);
	WSACleanup();
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值