C # TCP and UDP

Source: Internet
Author: User
Tags getstream sendmsg

HTTP use port is 80SMTP using port is 25TCP is the preferred protocol, it provides guaranteed transmission, error correction and buffering. System.Net.Sockets. TcpClient class encapsulates a TCP link that provides attribute fields to control links, including buffering, buffer size, and timeouts. TCP provides a number of features to ensure data transfer, it also provides error correction and the ability to retransmit when data loss or packet corruption occurs. TCP buffers incoming and outgoing data and also ensures that a cluttered array of packets is reorganized before the packet is transmitted to the application during transmission. Send Side
            TcpClient TcpClient = new TcpClient (TextBox1.Text, Int32.Parse (TextBox2.Text));            NetworkStream ns = Tcpclient.getstream ();            FileStream fs = File.Open ("Send.txt", FileMode.Open);            int data = fs. ReadByte ();            while (Data! =-1)            {                ns. WriteByte ((byte) data);                data = fs. ReadByte ();            }            Fs. Close ();            Ns. Close ();            Tcpclient.close ();        

Receiving end

            IPAddress localaddr = Ipaddress.parse ("127.0.0.1");            Int32 port = 2112;            System.Net.Sockets.TcpListener TcpListener = new TcpListener (localaddr, port);            Tcplistener.start ();            TcpClient TcpClient = Tcplistener.accepttcpclient ();            NetworkStream ns = Tcpclient.getstream ();            StreamReader sr = new StreamReader (NS);            string result = Sr. ReadToEnd ();            Invoke (New Updatedisplaydelegate (Updatedisplay), new object[] {result});            Tcpclient.close ();            Tcplistener.stop ();        

  

UDP, which typically uses UDP in applications where speed and performance requirements are higher than reliability, such as video streaming. UDP is a non-connected protocol. Look at others, debugging, a little bit of a problem, as a reference. Customer
System.Net.Sockets.UdpClient udpclient = new UdpClient ();            String sendmsg = "Hello Echo Server";            byte[] sendbytes = Encoding.ASCII.GetBytes (sendmsg);            Udpclient.send (Sendbytes, Sendbytes.length, "Udptest.net", 7);            IPEndPoint endPoint = new IPEndPoint (Ipaddress.any, 9093);            byte[] Rcvbytes = udpclient.receive (ref endPoint);            String rcvmessage = Encoding.ASCII.GetString (rcvbytes, 0, rcvbytes.length);            Console.WriteLine (Rcvmessage);            byte[] data = new byte[1024];            String Intput, StringData;            IPEndPoint ipend = new IPEndPoint (Ipaddress.parse ("127.0.0.1"), 9095);            Socket Server = new socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP);            String welcome = "Hello,where is you There";            data = Encoding.ASCII.GetBytes (welcome); Server. SendTo (data, data. Length, Socketflags.none, ipend);//Send data to the specified endpoint IPEndPoint Sender = new IPEndPoint (ipaddress.any, 0);            EndPoint Remote = (EndPoint) sender;            data = new byte[1024]; int recv = Server.            ReceiveFrom (data, ref Remote);            Console.WriteLine ("Message Received from{0}", remote.tostring ());                while (true) {intput = Console.ReadLine ();                if (Intput = = "text") {break; } server.                SendTo (Encoding.ASCII.GetBytes (intput), remote);//Send data to the specified endpoint Remote data = new byte[1024]; recv = Server. ReceiveFrom (data, ref Remote), or//from Remote StringData = Encoding.ASCII.GetString (+/-0, data).                Length);            Console.WriteLine (StringData);            } Console.WriteLine ("Stop"); Server. Close ();

  

Service
int recv;            byte[] data = new byte[1024];            IPEndPoint ipEp = new IPEndPoint (Ipaddress.any, 9095);            Socket socket = new socket (addressfamily.internetwork, Sockettype.dgram, protocoltype.udp);            Socket. Bind (ipEp);            Console.WriteLine ("Wait ....");            IPEndPoint sender = new IPEndPoint (Ipaddress.any, 9095);            EndPoint Remote = (EndPoint) (sender);            recv = socket. ReceiveFrom (data, ref Remote);            Console.WriteLine ("Message receive from {0}", remote.tostring ());            String welcome = "Welcome to my server";            data = Encoding.ASCII.GetBytes (welcome);            Socket. SendTo (data, data. Length, Socketflags.none, Remote);            while (true)            {                data = new byte[1024];                recv = socket. ReceiveFrom (data, ref Remote);                Socket. SendTo (data, recv, Socketflags.none, Remote);            }

  

Both TCP and UDP use socket sockets in the background, with TCP when the accuracy requirement is high.  If the pursuit of performance and speed, and the accuracy of the requirements are not high, with UDP. The difference between the TCP protocol and the UDP protocol connection process is as follows:
1. Based on connection and no connection;
2. Requirements for system resources (more TCP, less UDP);
3.UDP program structure is relatively simple;
4.TCP data transmission using streaming mode and UDP using the datagram mode;
5.TCP guarantees data correctness, UDP may drop packets, TCP guarantees data order, UDP is not guaranteed.

C # TCP and UDP

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.