Home > Software engineering >  Double machine communication problems
Double machine communication problems

Time:09-23

Recently made an equipment, send UDP packets through the network interface, and the computer while on a link to debug the application directly with the computer through the Internet links, the application doesn't receive packets, can use wireshark to capture packets, link if the device does not directly and computing, but through the router and computer links, the application can receive packets,

The code below


//socket. CPP: Defines the entry point for the console application.
//

# include "stdafx. H"
#include
#include
#include
#include
#include
#include
#include

# pragma comment (lib, "Ws2_32. Lib")

using namespace std;

# define RESULT_ERROR 1
# define RESULT_OK 0

# define UDP_PORT 20000

The SOCKET g_socket;
BOOL g_bThreadGo;

Int startUDPConnection (uint16_t port, SOCKET * pSocket)
{
WSADATA wsa.
SOCKADDR_IN addr.
int ret;
Int rc;
Unsigned int opval=1;
DWORD dwBytesRet;
Ret=: : WSAStartup (MAKEWORD (2, 2), & amp; Wsa);
If (ret) {
Cout & lt; & lt;" Can 't satrt Windows Sock API "& lt; Return RESULT_ERROR;
}
* pSocket=: : socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
If (INVALID_SOCKET==* pSocket) {
Cout & lt; & lt;" Couldn 't create UDP socket "& lt; Return RESULT_ERROR;
}
Addr. Sin_family=AF_INET;
Addr. Sin_port=htons (port);
Addr. Sin_addr. S_un. S_addr=ADDR_ANY;
Rc=: : bind (* pSocket, (const sockaddr *) & amp; Addr, sizeof (SOCKADDR_IN));
If (SOCKET_ERROR==rc) {
Cout & lt; & lt;" The Bind didn 't work, the Error code: "& lt; Return RESULT_ERROR;
}
Return RESULT_OK;
}

Void stopUDPConnection (SOCKET * pSocket)
{
: : closesocket (* pSocket);
: : WSACleanup ();
}

DWORD WINAPI ThreadFunc (LPVOID pParam)
{
If (RESULT_ERROR==startUDPConnection (UDP_PORT, & amp; G_socket)) {
Cout & lt; & lt;" StartUDPConnection is faild "& lt; : : stopUDPConnection (& amp; G_socket);
: : system (" pause ");
return 0;
}

DWORD dwCounter=0;
SOCKADDR_IN remoteAddr;
Int32_t remoteAddrLen;
Int8_t udpPacketBuff [2048].
RemoteAddrLen=sizeof (SOCKADDR_IN);
While (g_bThreadGo) {
Int32_t rc=: : recvfrom (g_socket, udpPacketBuff (char *), 2048, 0, (sockaddr *) & amp; RemoteAddr,
& RemoteAddrLen);
If ((1)! Rc)={
DwCounter++;
Cout & lt; & lt;" The Receive Data Counter: "& lt; Cout & lt; }
}

StopUDPConnection (& amp; G_socket);
return 0;
}

Int _tmain (int arg c, _TCHAR * argv [])
{
DWORD dwThreadId;
G_bThreadGo=TRUE;
HANDLE hThread=: : CreateThread (NULL, 0, ThreadFunc, 0, 0, & amp; DwThreadId);

: : Sleep (10000);
G_bThreadGo=FALSE;

: : WaitForSingleObject (hThread, INFINITE);

: : system (" pause ");
return 0;
}

CodePudding user response:

Try: : socket (AF_INET, SOCK_DGRAM, 0 )

CodePudding user response:

Thank you for your reply, have tried, not the problem, the problem found, is a problem of firewall,

CodePudding user response:

Now there is a new problem, if the firewall does not allow the application through the firewall, how the application can penetrate firewall receive UDP send to come over?

CodePudding user response:

Baidu search "firewall penetration"

CodePudding user response:

Don't go through the socket, direct network card of the underlying contract, according to the agreement to bypass firewalls
  • Related