UDP 广播
程序员文章站
2022-06-29 18:25:11
...
客户端:
// UDPBroadcastSend.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include <iostream>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
SOCKET sock;
sockaddr_in addrto;
WSADATA wsdata;
bool bsocket;
char smsg[100] = {0};
cout<<"input what message to send\n:"<<endl;
cin>>smsg;
//启动SOCKET库,版本为2.0
WSAStartup(0x0202,&wsdata);
sock=WSASocket(AF_INET,SOCK_DGRAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
bsocket=true;
//然后赋值给地址,用来从网络上的广播地址接收消息;
addrto.sin_family=AF_INET;
addrto.sin_addr.s_addr=INADDR_BROADCAST;
addrto.sin_port=htons(9000);
bool opt=true;
//设置该套接字为广播类型,
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char FAR *)&opt,sizeof(opt));
int nlen=sizeof(addrto);
char buf[100] = {0};
while(1)
{
addrto.sin_family=AF_INET;
addrto.sin_addr.s_addr=INADDR_BROADCAST;
addrto.sin_port=htons(9000);
Sleep(1000);
//从广播地址发送消息
int ret=sendto(sock,smsg, strlen(smsg) + 1,0,(sockaddr*)&addrto,nlen);
if(ret==SOCKET_ERROR)
{
printf("%d/n",WSAGetLastError());
}
else
{
printf("It'sock OK.\n");
}
ret = recvfrom( sock, buf, sizeof(buf), 0, (sockaddr*)&addrto, &nlen );
if (SOCKET_ERROR == ret)
{
printf("error on recv after send!\n");
}
else
{
printf("recv after send: %s\n", buf);
}
}
return 0;
}
服务器端:
C/C++ code
// UDPBroadcastRecv.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
SOCKET sock;
sockaddr_in from,a;
WSADATA wsdata;
bool optval;
//启动SOCKET库,版本为2.0
WSAStartup(0x0202,&wsdata);
optval=true;
//然后赋值给两个地址,一个用来绑定套接字,一个用来从网络上的广播地址接收消息;
a.sin_family=AF_INET;
a.sin_addr.s_addr=INADDR_ANY;
a.sin_port=htons(9000);
from.sin_family=AF_INET;
from.sin_addr.s_addr=INADDR_BROADCAST;
from.sin_port=htons(5050);
int fromlength=sizeof(SOCKADDR);
//用UDP初始化套接字
sock=socket(AF_INET,SOCK_DGRAM,0);
// 设置该套接字为广播类型,
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char FAR *)&optval,sizeof(optval));
// 把该套接字绑定在一个具体的地址上
bind(sock,(sockaddr *)&a,sizeof(sockaddr_in));
char buf[256];
while(1)
{//从广播地址接收消息,注意用来绑定的地址和接收消息的地址是不一样的
recvfrom(sock,buf,256,0,(struct sockaddr FAR *)&from,(int FAR *)&fromlength);
//Sleep(2000);
printf("%sock\n",buf);
sendto( sock, buf, strlen(buf) + 1, 0, (struct sockaddr FAR *)&from, fromlength );
ZeroMemory(buf,256);
}
return 0;
}
const int MAX_BUF_LEN = 255;
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
// 启动socket api
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return -1;
}
// 创建socket
SOCKET connect_socket;
connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(INVALID_SOCKET == connect_socket)
{
err = WSAGetLastError();
printf("socket error! error code is %d/n", err);
return -1;
}
char hostname[128];
struct hostent*pHost;
if(gethostname(hostname,128)==0)
{
printf("%s\n",hostname);//计算机名字
}
pHost = gethostbyname(hostname);
printf("ip %s\n",inet_ntoa(*(struct in_addr*)pHost->h_addr_list[0]));
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(321);
sin.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)pHost->h_addr_list[0]));
bool bOpt = true;
//设置该套接字为广播类型
setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
int nAddrLen = sizeof(SOCKADDR);
char buff[MAX_BUF_LEN] = "";
int nLoop = 0;
while(1)
{
nLoop++;
sprintf(buff, "%8d", nLoop);
// 发送数据
int nSendSize = sendto(connect_socket, buff, strlen(buff), 0, (SOCKADDR*)&sin, nAddrLen);
if(SOCKET_ERROR == nSendSize)
{
err = WSAGetLastError();
printf("sendto error!, error code is %d/n", err);
return -1;
}
printf("Send: %s\n", buff);
Sleep(500);
}
return 0;
}
广播客户端
const int MAX_BUF_LEN = 255;
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
// 启动socket api
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return -1;
}
// 创建socket
SOCKET connect_socket;
connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(INVALID_SOCKET == connect_socket)
{
err = WSAGetLastError();
printf("socket error! error code is %d\n", err);
return -1;
}
// 用来绑定套接字
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(321);
sin.sin_addr.s_addr = 0;
// 用来从网络上的广播地址接收数据
SOCKADDR_IN sin_from;
// 绑定套接字
err = bind(connect_socket, (SOCKADDR*)&sin, sizeof(SOCKADDR));
if(SOCKET_ERROR == err)
{
err = WSAGetLastError();
printf("bind error! error code is %d\n", err);
return -1;
}
int nAddrLen = sizeof(SOCKADDR);
char buff[MAX_BUF_LEN] = "";
int nLoop = 0;
while(1)
{
// 接收数据
int nSendSize = recvfrom(connect_socket, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
if(SOCKET_ERROR == nSendSize)
{
err = WSAGetLastError();
printf("recvfrom error! error code is %d/n", err);
return -1;
}
buff[nSendSize] = '\0';
printf("Recv: %s\n", buff);
}
return 0;
}
转载于:https://my.oschina.net/lopo/blog/260891
上一篇: nodejs中使用mysql
下一篇: UDP广播