码迷,mamicode.com
首页 > 其他好文 > 详细

基于UDP的网络通讯(socket)

时间:2015-08-08 16:37:17      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:socket   udp协议   网络通讯   

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();
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

基于UDP的网络通讯(socket)

标签:socket   udp协议   网络通讯   

原文地址:http://blog.csdn.net/zhongqi0808/article/details/47359261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!