UDP enables point-to-point message sending and forwarding from the Intranet to the network

Source: Internet
Author: User

UDP implements point-to-point message sending and forwarding from the Intranet to the network. The TCP connection mentioned in the previous article is a little different. TCP can keep the link. then communicate. after UDP is sent, the server immediately closes the message after receiving the message and then calls the listener again to release the resource immediately. then I am in the listening status again. I guess there is a problem with this implementation. but now I can only achieve this.

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Using system. net. Sockets;
Using system. Threading;
Using system. net;

namespace P2P
{< br> Public partial class form1: form
{< br> private thread th;
private udpclient tcpl;
Public bool listenerrun = true;
socket S;
// if the value of listenerrun is true, the connection request is accepted. If the value of false is true, the Program

Public form1 ()
{< br> initializecomponent ();

}
Public void stop ()
{
Tcpl. Close ();
Th. Abort (); // terminate the thread
MessageBox. Show ("Stop listening ....");
}

Private void form1_load (Object sender, eventargs E)
{

}

Private void button#click (Object sender, eventargs E)
{
Th = new thread (New threadstart (Listen); // create a thread for listening
Th. Start (); // open a new thread
}

Private void listen ()
{
Try
{
Tcpl = new udpclient (5656); // create a tcplistener object on port 5656
Ipendpoint groupep = new ipendpoint (ipaddresses. Any, 5656 );
MessageBox. Show ("listening ....");
While (listenerrun) // start listening
{
Byte [] stream = new byte [80];
Stream = tcpl. Receive (ref groupep );

MessageBox. Show (groupep. tostring () + system. Text. encoding. utf8.getstring (Stream ));
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}

}

Private void button3_click (Object sender, eventargs E)
{
Th = new thread (New threadstart (send); // create a thread for listening
Th. Start (); // open a new thread
}

Public void send ()
{
Try
{
S = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP );
IPaddress broadcast = IPaddress. parse ("127.0.0.1 ");
Byte [] sendbuf = encoding. utf8.getbytes ("Where are Chinese ");
Ipendpoint Ep = new ipendpoint (broadcast, 5270 );
S. Connect (EP );
S. sendto (sendbuf, EP );
Byte [] DATA = new byte [2048];
Int rect = S. Receive (data );
Byte [] chat = new byte [rect];
Buffer. blockcopy (data, 0, chat, 0, rect );
MessageBox. Show (system. Text. encoding. Default. getstring (CHAT ));
S. Close ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );

}< br> finally
{< br>
}

Private void button2_click (Object sender, eventargs E)
{
Stop ();
}

}
}

ServerCode:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Using system. net. Sockets;
Using system. Threading;
Using system. net;

Namespace p2pserver
{
Public partial class form1: Form
{
Private thread th;
Private udpclient tcpl;
Public bool listenerrun = true;

Public form1 ()
{
Initializecomponent ();
This. button2.enabled = false;
}
Private void listen ()
{
Try
{
Tcpl = new udpclient (5270); // create a tcplistener object on port 5270
Ipendpoint groupep = new ipendpoint (ipaddresses. Any, 5270 );
While (listenerrun) // start listening
{
String MSG = "welcome ";
Byte [] stream = new byte [80];
Stream = tcpl. Receive (ref groupep );
MessageBox. Show (groupep. tostring () + system. Text. encoding. utf8.getstring (Stream ));
Byte [] bytesmsg = encoding. Default. getbytes (MSG );
Tcpl. Connect (groupep );
Tcpl. Send (bytesmsg, bytesmsg. Length );
Tcpl. Close ();
Listen ();

}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{

}
}
Public void stop ()
{
Try
{
Tcpl. Close ();
Th. Abort (); // terminate the thread
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}

Private void button#click (Object sender, eventargs E)
{
This. button1.enabled = false;
This. button2.enabled = true;
Th = new thread (New threadstart (Listen); // create a thread for listening
Th. Start (); // open a new thread
}

Private void button2_click (Object sender, eventargs E)
{
This. button2.enabled = false;
This. button1.enabled = true;
Stop ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.