C# TCP与UDP
2021-03-27 14:26
标签:recv rem pre port welcome ade 接受 serve new t 接受端 C# TCP与UDP 标签:recv rem pre port welcome ade 接受 serve new t 原文地址:https://www.cnblogs.com/nanguoyezi/p/9351617.html 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();
//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);
}
1.基于连接与无连接;
2.对系统资源的要求(TCP较多,UDP少);
3.UDP程序结构较简单;
4.TCP数据传输采用流模式与UDP则采用数据报模式 ;
5.TCP保证数据正确性,UDP可能丢包,TCP保证数据顺序,UDP不保证。
下一篇:C# WebSocket