Keikoman
Keikoman

Reputation: 73

Udp packets to machine behind nat

First of all sry about my english. My problem is i have electronic circuit on press machine. This circuit sending data to my server without a problem. After i recieve data i need to send command to circuit. In LAN its working like a charm. But out of lan circuit dont recieving my command somehow. Any idea how i can send to udp pocked to machine behind NAT ? my code is bellow , ty for any help.

 private void ReceiveMessage()
        {
            while (true)
            {
                try
                {
                    var remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    var content = _udpClient.Receive(ref remoteIpEndPoint);
                    if (content.Length > 0)
                    {
                        var message = Encoding.ASCII.GetString(content);
                        var smsg = message.Split('|');
                        var pin1 = int.Parse(smsg[13]);
                        var pin2 = int.Parse(smsg[14]);

                        var isChanged = CheckChanges(smsg[1]);
                        if (isChanged == 1)
                        {

                            **var recvpt = new IPEndPoint(remoteIpEndPoint.Address, remoteIpEndPoint.Port);
                            var client = new UdpClient();
                            var cmd1 = "CP1S" + _pin1Time + "F";
                            var cmd2 = "CP2S" + _pin2Time + "F";
                            var senddata1 = Encoding.UTF8.GetBytes(cmd1);
                            var senddata2 = Encoding.UTF8.GetBytes(cmd2);
                            client.Send(senddata1, senddata1.Length, recvpt);
                            client.Send(senddata2, senddata2.Length, recvpt);
                            client.Close();**
                            //  UpdateChanges(smsg[1]);
                        }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToStrin<wbr ></wbr>g());
                }
            }
        }
}

Upvotes: 0

Views: 223

Answers (1)

Tim Meers
Tim Meers

Reputation: 928

Computers that are behind a NAT router are not directly accessible from a remote network. There would need to be rules put in place on your firewall/router to route this traffic to the remote machine. For explicit directions see your WAN/LAN admin. If you don't have one you could try asking over on Server Fault with more information about your network.

Upvotes: 1

Related Questions