I am new to C # UDP coding and I have some 'weird' behavior when locating a UDP client locally on my PC Can use. I want to send UDP data to a port (11000) on my PC to another port (12000) on the same PC
This is a piece from my code.
Public Class Mikalas {// Creates an application client to read the incoming data. Private UdpClient udpClient; Personal thread thread; Private constant string IPDDR = "127.0.0.1"; Public MyClass () {udpClient = New Exclamation (11000); UdpClient.Connect (IPAddress.Parse (IPADDR), 12000); This.thread = new thread (new threadstart (this.Execute)); This.thread.Name = "Udp"; This.thread.Start (); SendData ("Quick Brown Fox jumps on the celestial dog"); } Execute private zeros () {try {// block messages from a remote host until the message returns on this socket. IPEndPoint remoteIpEndPoint = new IPPpoint (IPadation no, 0); Byte [] receivedbits = this.udpClient.Resive (ref RemotePointPoint); Console "Written" ("data received"); } Hold (exception e) {Console.WriteLine (e.ToString ()); }} Public Zero SendData (String Data) {Console.WriteLine ("Sending ..."); {Try this.udpClient.Send (System.Text.Encoding.ASCII.GetBytes (data), data.line); } Hold (exception e) {Console.WriteLine (String.Format ("exception {0}", e.Message)); }}}
If I run it, I get an exception:
Sending ... System.Net.Sockets.SocketException: An existing connection was forcibly turned off by remote host on System.Net.Sockets.Socket.ReceiveFrom (Byte [] buffer, Int32 offset, Int32 size, socketFlags socketFlags, EndPoint & amp; remoteEP) System.Net.Sockets.UdpClient On the Receive (IPEndPoint & amp; remoteEP) \ Temporary \ Test \ Class1.cs: Line 40
Exception occurs on this line:
Byte [] Received Bits = this.udpClient.Resive (Ref RemotePip Point);
At the moment of SendData (), the receiver is throwing exceptions. Not sending time, I do not get an exception. It seems that the dispatch is happening due to receiving it with the exception.
When I use the real IP address of my PC, I have the same behavior, however, when I use any other IP address to any PC (eg 192.168.10.10) Even if it has not been assigned, it is working well: this string sends and receives () waiting for incoming data
The error occurs, because there is no socket in the port you are sending.
To test the UDP communication just for the purpose, by changing the port you are sending, you can just fix your code example, that is Change it to connect ()
method call:
udpClient.Connect (IPAddress.Parse (IPADDR), 11000);
This will cause the socket to be sent to itself. Your code will only work successfully with this change
For the record, here is the simplest possible display code about the UdpClient
.
UdpClient Client = New UdpClient (0, AddressFamily.InterNetwork); Byte [] Datagram = Encoding. ASCII Gatebytes ("Hello World!"); IPEndPoint ipEndPoint = New IPPpoint (IPADress loopback, (IPEDP) client. Client.LocalendPoint.) Port); Client Sand (datagram, datagram, lang, ippepoint); Datagram = client Receiver (referee ipindpoint); Console.light line ("received: \" "+ encoding.assici.getstring (datagram) +" \ "");
Of course, in a real network I / O situation, you will have two end points, each will have its own local port, and the port of the other end point must be sent. So neither is your code example, nor is this the simplest one I am very useful as a real working program. But this allows you to learn a few things about how the Socket API works.
No comments:
Post a Comment