Http使用端口是80
SMTP使用端口是25
TCP是首选协议,它提供有保证的传输、错误校正和缓冲。
System.Net.Sockets. TcpClient类封装了TCP链接,提供属性字段来控制链接,包括缓冲、缓冲区的大小和超时。
TCP提供很多功能来确保数据的传输,它还提供了错误校正以及当数据丢失或数据包损坏时重新传输的功能。TCP可缓冲传入和传出的数据,还保证在传输过程中,在把数据包传输给应用程序之前重组杂乱的一系列数据包。
发送端
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();
接受端
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, 通常在速度和性能要求比可靠性更高的应用程序中使用UDP,如视频流。
UDP是一个无连接的协议。
看别人的,调试,有点小问题,就当参考。
客户
//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 are you there";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, iPEnd);//将数据发送到指定的终结点
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);//将数据发送到指定的终结点Remote
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);//从Remote接受数据
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
}
Console.WriteLine("stop");
server.Close();
服务
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);
}
TCP与UDP
在后台都用到套接字Socket,当准确度要求高的时候,用TCP。若追求性能和速度,而且对准确度要求不高时,用UDP。
TCP协议和UDP协议连接过程的区别如下:
1.基于连接与无连接;
2.对系统资源的要求(TCP较多,UDP少);
3.UDP程序结构较简单;
4.TCP数据传输采用流模式与UDP则采用数据报模式 ;
5.TCP保证数据正确性,UDP可能丢包,TCP保证数据顺序,UDP不保证。