#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <process.h>
#include <WinSock2.h>//windows API
#pragma comment(lib,"ws2_32.lib")
#define IP "127.0.0.1"
#define SENDPORT 0
#define RECVPORT 3781
#define BROADCAST "255.255.255.255"
using namespace std;
struct ARGLIST{
SOCKET sock;
sockaddr_in addr;
};
void send_process( void *p)
{
ARGLIST send_process;
send_process = *((ARGLIST *)p);
char sendbuffer[4096];
while ( true )
{
gets_s(sendbuffer);
if (0 == strcmp (sendbuffer, "#" ))
{
_endthread();
}
if (SOCKET_ERROR == sendto(send_process.sock, sendbuffer, strlen (sendbuffer), 0, (sockaddr *)&send_process.addr, sizeof (send_process.addr)))
{
cout << "发送失败" << GetLastError() << endl;
_endthread();
}
else
{
cout << "发送成功" << endl;
}
}
}
void recv_process( void *p)
{
ARGLIST recv_process;
recv_process = *((ARGLIST *)p);
char recvbuffer[4096];
int nLen = sizeof (recv_process.addr);
while ( true )
{
int n = recvfrom(recv_process.sock, recvbuffer, sizeof (recvbuffer), 0, (sockaddr *)&recv_process.addr, &nLen);
if (SOCKET_ERROR == n)
{
cout << "接收失败" << GetLastError() << endl;
_endthread();
}
recvbuffer[n] = 0;
cout << "发送者IP:" << inet_ntoa(recv_process.addr.sin_addr) << " 端口:" << htons(recv_process.addr.sin_port) << endl;
cout << "接收:" << recvbuffer << endl;
}
}
int GetSendList(ARGLIST &sendlist)
{
WSADATA wd;
WSAStartup(0x0202, &wd);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (INVALID_SOCKET == sock)
{
cout << "创建套接字错误" << GetLastError() << endl;
}
sockaddr_in sin = { AF_INET };
//sin.sin_addr.s_addr = INADDR_BROADCAST;
sin .sin_addr.S_un.S_addr = inet_addr(IP);
sin .sin_port = htons(SENDPORT);
if (SOCKET_ERROR == bind(sock, (sockaddr *)& sin , sizeof ( sin )))
{
cout << "Send绑定套接字错误" << GetLastError() << endl;
return 0;
}
bool bOpt = true ;
//设置该套接字为广播类型
if (SOCKET_ERROR == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, ( char *)&bOpt, sizeof (bOpt)))
{
cout << "Send设置套接字错误" << GetLastError() << endl;
}
sendlist.sock = sock;
sockaddr_in sin_from = { AF_INET };
//sin.sin_addr.s_addr = INADDR_BROADCAST;
sin_from.sin_addr.S_un.S_addr = inet_addr(BROADCAST);
sin_from.sin_port = htons(RECVPORT);
sendlist.addr = sin_from;
return 1;
}
int GetRecvList(ARGLIST &recvlist)
{
WSADATA wd;
WSAStartup(0x0202, &wd);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
cout << "创建套接字错误" << GetLastError() << endl;
}
sockaddr_in sin = { AF_INET };
//sin.sin_addr.s_addr = INADDR_BROADCAST;
sin .sin_addr.S_un.S_addr = inet_addr(IP);
sin .sin_port = htons(RECVPORT);
if (SOCKET_ERROR == bind(sock, (sockaddr *)& sin , sizeof ( sin )))
{
cout << "Recv绑定套接字错误" << GetLastError() << endl;
return 0;
}
bool bOpt = true ;
//设置该套接字为广播类型
if (SOCKET_ERROR == setsockopt(sock, SOL_SOCKET, SO_BROADCAST, ( char *)&bOpt, sizeof (bOpt)))
{
cout << "Recv设置套接字错误" << GetLastError() << endl;
}
recvlist.sock = sock;
sockaddr_in sin_from = { AF_INET };
sin_from.sin_addr.s_addr = INADDR_BROADCAST;
//sa.sin_addr.S_un.S_addr = inet_addr(IP);
sin_from.sin_port = htons(SENDPORT);
recvlist.addr = sin ;
return 1;
}
int main()
{
ARGLIST sendlist,recvlist;
if (0 == GetSendList(sendlist))
{
return 0;
}
if (0 == GetRecvList(recvlist))
{
return 0;
}
char *hello = "Hello" ;
if (SOCKET_ERROR == sendto(sendlist.sock, hello, strlen (hello), 0, (sockaddr *)&sendlist.addr, sizeof (sendlist.addr)))
{
cout << "发送失败" << GetLastError() << endl;
return 0;
}
_beginthread(send_process, 0, &sendlist);
_beginthread(recv_process, 0, &recvlist);
while ( true )
{
}
return 0;
}
|