C# socket,Tcp和Udp
转自http://www.cnblogs.com/crazyxu/archive/2012/09/10/2678250.html
应用程序可以通过 TCPClient、TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务。这些协议类建立在 System.Net.Sockets.Socket 类的基础之上,负责数据传送的细节。(也就是说TCPClient、TCPListener 和 UDPClient 类是用来简化Socket)
1.TcpClient
using System; using System.Net.Sockets; using System.Text; using System.Net; namespace Tcpclient { public class TcpTimeClient { private static int portNum = 11000; private static string hostName = Dns.GetHostName().ToString(); public static void Main(String[] args) { try { TcpClient client = new TcpClient(hostName, portNum); NetworkStream ns = client.GetStream(); byte[] bytes = new byte[1024]; int bytesRead = ns.Read(bytes, 0, bytes.Length); Console.WriteLine(Encoding.ASCII.GetString(bytes, 0, bytesRead)); client.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.Read(); } } }
2.TcpListener
using System; using System.Net.Sockets; using System.Text; namespace Tcplistener { public class TcpTimeServer { private const int portNum = 11000; public static int Main(String[] args) { bool done = false; TcpListener listener = new TcpListener(portNum); listener.Start(); while (!done) { Console.Write("Waiting for connection..."); TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Connection accepted."); NetworkStream ns = client.GetStream(); byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString()); try { ns.Write(byteTime, 0, byteTime.Length); ns.Close(); client.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } listener.Stop(); return 0; } } }
3.UdpClient
-
使用远程主机名和端口号作为参数创建 UdpClient 类的实例。
-
创建 UdpClient 类的实例,然后调用
Connect 方法。
监听端:
using System; using System.Net.Sockets; using System.Text; using System.Net; namespace Udpclient2 { class Program { static void Main(string[] args) { UdpClient udpClient = new UdpClient(12000); try { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); while (returnData != "") { udpClient.Connect(RemoteIpEndPoint.Address.ToString(), Convert.ToInt32(RemoteIpEndPoint.Port)); Byte[] sendBytes = Encoding.ASCII.GetBytes("I'm here."); udpClient.Send(sendBytes, sendBytes.Length); Console.WriteLine("This is the message you received " + returnData.ToString()); Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()); udpClient.Close(); break; } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.Read(); } } }
连接端:
using System; using System.Net.Sockets; using System.Text; using System.Net; namespace Udpclient { class Program { static void Main(string[] args) { // This constructor arbitrarily assigns the local port number. UdpClient udpClient = new UdpClient(11000); try { udpClient.Connect(Dns.GetHostName().ToString(), 12000); // Sends a message to the host to which you have connected. Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?"); udpClient.Send(sendBytes, sendBytes.Length); //IPEndPoint object will allow us to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // Uses the IPEndPoint object to determine which of these two hosts responded. Console.WriteLine("This is the message you received " + returnData.ToString()); Console.WriteLine("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()); udpClient.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.Read(); } } }