示例#1
0
文件: UdpServer.cpp 项目: 0cch/misc
UINT __stdcall UdpServerWorker(PVOID context)
{
	InitializeCriticalSection(&g_data_lock);
	UdpServer server;
	if (server.Init("127.0.0.1", (int)context)) {
		std::vector<UCHAR> buffer;
		buffer.resize(64 * 1024);
		for (;;) {
			sockaddr_in addr;
			int recv_length = server.Recv((char *)buffer.data(), buffer.size(), &addr);
			if (recv_length == -1) {
				break;
			}

			EnterCriticalSection(&g_data_lock);
			g_data_queue.push_back(std::vector<UCHAR>(buffer.begin(), buffer.begin() + recv_length));
			LeaveCriticalSection(&g_data_lock);
			SetEvent(g_data_event);

			server.Send("OK", 3, &addr);
		}
	}
	DeleteCriticalSection(&g_data_lock);
	return 0;
}
void ServerUDP()
{
    UdpServer server;
    Packet data;
    IP4Address source;
    //server.SetNetworkInterface(0);  // If you wanted to specifiy a network interface to receive on.
    data.SetByteOrder(CX_PACKET_BIG_ENDIAN);
    if(!server.InitializeMulticastSocket(PORT, MULTICAST_GROUP)) 
    {
        cout << "Unable to initialize on port " << PORT << endl;
        return;
    }
    cout << "Initialized UDP Server Socket on port " << PORT << endl;
    char key = 0;
    while(key != 27) 
    {

        //  Do non-blocking receive for data (wait 100 ms).
        if(server.Recv(data, 500, 100, &source) > 0) //  Receive up to 500 bytes to packet.
        { 
            cout << source.mString << " Sent (" << data.Size() << " bytes): ";
            char val;
            while(data.Read(val))
                cout << val;
            cout << endl;
        }
        key = GetChar();     //  Get key.
    }
}
示例#3
0
文件: UdpServer.cpp 项目: 0cch/misc
UINT __stdcall UdpServerForIpWorker(PVOID context)
{
	UdpServer server;
	if (server.Init("127.0.0.1", (int)context)) {
		std::vector<UCHAR> buffer;
		buffer.resize(0x1000);
		for (;;) {
			sockaddr_in addr;
			int recv_length = server.Recv((char *)buffer.data(), buffer.size(), &addr);
			if (recv_length == -1) {
				break;
			}

			server.Send("OK", 3, &addr);
		}
	}

	return 0;
}