My steps is like this:
1, create a socket mSckSender, mSckSender binding to their computer network IP 192.168.1.173 and port 3000,
2, create a receiving socket mSckReceiver, will mSckReceiver binding to the local port 3000,
3, open a receiving threads, thread used in mSckReceiver receiving data,
4, and then in the main thread by mSckSender send data to the server,
But I have not yet received the server returns to the data, the thread has been waiting for, use the test tool can measure there are data back to the server,
This code is mSckSender
BOOL CTestDlg: : CreateSender (void)
{
//DeleteSender ();
MSckSender=socket (AF_INET, SOCK_DGRAM, 0);
If (mSckSender!=INVALID_SOCKET)
{
BOOL flag=TRUE;
Int retr=setsockopt (mSckSender, SOL_SOCKET, SO_REUSEADDR,
(char *) & amp; Flag, sizeof (flag));//set the socket to address reuse
If (retr==SOCKET_ERROR)
{
DeleteReceiver ();
return FALSE;
}
int ret=0;
Sockaddr_in addr.
Memset ((char *) & amp; Addr, 0, sizeof (addr));
Char IP [20];
Char name [20].
GetHostInfo (IP name);//get the native IP and user name
Addr. Sin_addr. S_un. S_addr=inet_addr (IP);
Addr. Sin_family=AF_INET;
Addr. Sin_port=htons (3000);//local port, must pay attention to the port and listening ports are the same (answer will write below)
Ret=bind (mSckSender, (struct sockaddr *) & amp; Addr, sizeof (addr));//bind to send socket
If (ret==SOCKET_ERROR)
{
DeleteSender ();
return FALSE;
}
return TRUE;
}
return FALSE;
}
This code is mSckReceiver
BOOL CTestDlg: : CreateReceiver (void)
{
DeleteReceiver ();
//create a UDP transmission Socket
MSckReceiver=socket (AF_INET, SOCK_DGRAM, 0);
If (mSckReceiver!=INVALID_SOCKET)
{
//set parameters on the Socket: allow address reuse
BOOL flag=TRUE;
Int ret=setsockopt (mSckReceiver, SOL_SOCKET, SO_REUSEADDR,
(char *) & amp; Flag, sizeof (flag));
If (ret==SOCKET_ERROR)
{
DeleteReceiver ();
return FALSE;
}
//to bind the Socket to the local port
SOCKADDR_IN addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=htonl (INADDR_ANY);
Addr. Sin_port=htons (3000);//be sure to set the listener port and send to the same port
Ret=bind (mSckReceiver, (struct sockaddr *) & amp; Addr, sizeof (addr));
If (ret==SOCKET_ERROR)
{
DeleteReceiver ();
return FALSE;
}
return TRUE;
}
return FALSE;
}
This is to send data
void CTestDlg: : SendData publishes the event (char * pchar, long length)
{
Char * tt=new char [length + 1];
Memset (tt, '/' 0 ', the length + 1);
Memcpy (tt, pchar, length);
Sockaddr_in remote;
Memset ((char *) & amp; Remote, 0, sizeof (remote));
Remote. Sin_addr. S_un. S_addr=inet_addr (" 210.45.212.119 ");//to send server IP
Remote. Sin_family=AF_INET;
Remote. Sin_port=htons (7002);//server port
Sendto (mSckSender, tt, length, 0, (sockaddr *) & amp; Remote, sizeof (remote));
DeleteSender ();
The delete [] tt;
}
This is receiving data
void CTestDlg: : ReceivingLoop (void)
{
Struct sockaddr_in addr_cli;
Int addr_cli_len=sizeof (addr_cli);
Char buffer [MAX_PATH]={'/0};
Long bytes=0;
MIsReceiving=TRUE;
Cstrings tnote=L "";
//wait for receiving data
While (mIsReceiving)
{
Int addr_cli_len=sizeof (addr_cli);
Bytes=recvfrom (mSckReceiver, char * buffer, MAX_PATH, 0, (LPSOCKADDR) & amp; Addr_cli, (int *) & amp; Addr_cli_len);
If (bytes==SOCKET_ERROR | | bytes==0)
{
//if the Socket send error or Socket to disconnect, out of circulation
MIsReceiving=FALSE;
}
The else
{
}
}
}
I ask you, where is the problem, why I don't get the server data?
CodePudding user response:
Well, a great god?CodePudding user response:
Great god guidance, online etc.CodePudding user response:
Top oneCodePudding user response:
Remote. Sin_port=htons (7002);//server port, your server binding in 3000, you to send a MAO, 7002 received no,CodePudding user response:
Bool is_LAN_ok=(boo) (LAN test ok? ;
If (is_LAN_ok)
{
Bool is_NAT_ok=(bool) (network port mapping?)
If (is_NAT_ok)
{
//don't step into this.
}
The else
{
//port mapping, digging, p2p, upd digging, TCP communications, DMZ, upnp
}
}
The else
{
//test program
}
CodePudding user response:
Why a bundle multiple socket port, a socket to send and receive, will be finished probably server data in another socket bufferCodePudding user response: