一 UDP
1.获取本地IP地址
private void getLocalIP()
{
//获得IP地址容器
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
address = ip;
info.Text += "服务器的IP地址为:" + ip.ToString();
break;
}
}
if (address == null)
{
info.Text += "无法获得服务器IP地址" + "\n";
}
}
2.启动接收线程
ThreadStart start = new ThreadStart(Receive);
Thread t1 = new Thread(start);
t1.IsBackground = true;
t1.Start();
3.接收方法(UDP)
private void Receive()
{
//接受
byte[] buffer = new byte[1024];
//注册对面主机的终结点
EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8001);
int num = cs.ReceiveFrom(buffer, ref remoteEP);
//解析
while (num > 0)
{
//解码:以UTF-8字符串的方式获得传入buffer的数据
String s = Encoding.UTF8.GetString(buffer, 0, num);
//通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字
符串进行更改
log.Dispatcher.Invoke(() => { log.Text = log.Text + "消息为" +s
});
//进行循环的监听
num = cs.ReceiveFrom(buffer, ref remoteEP);
}
//禁用套接字的发送和接受
cs.Shutdown(SocketShutdown.Both);
//关闭套接字
cs.Close();
}
4.发送方法(UDP)
private void send()
{
//实例化目的终结点
IPAddress endAdreess = IPAddress.Parse("127.0.0.1");
EndPoint remoteEP = new IPEndPoint(endAdreess,8001);
String s = "你好";
//将要传输的字符串进行编码
byte[] buffer = Encoding.UTF8.GetBytes(s);
//使用套接字的SenTo方法进行发送
cs.SendTo(buffer, toEP);
}
5.将本地终结点与套接字绑定
private void BindLocal()
{
//获取套接字对象UDP
cs = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//它表示将侦听器 Socket 绑定到的本地终结点。
EndPoint localEP = new IPEndPoint(address, 8001);
cs.Bind(localEP)
}
成功实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UDP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IPAddress address;
Socket cs;
public MainWindow()
{
InitializeComponent();
}
private void Window_Initialized(object sender, EventArgs e)
{
getLocalIP();
BindLocal();
ThreadStart start = new ThreadStart(Receive);
Thread t1 = new Thread(start);
t1.IsBackground = true;
t1.Start();
}
private void Send()
{
EndPoint remoteEP = new IPEndPoint(address,8002);
String s = "你好";
byte[] buffer = Encoding.UTF8.GetBytes(s);
cs.SendTo(buffer,remoteEP);
}
//将本地终结点与套接字绑定
private void BindLocal()
{
cs = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
EndPoint localEp = new IPEndPoint(address, 8001);
cs.Bind(localEp);
}
private void Receive()
{
//接受
byte[] buffer = new byte[1024];
//注册对面主机的终结点
EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8002);
int num = cs.ReceiveFrom(buffer, ref remoteEP);
//解析
while (num > 0)
{
//解码:以UTF-8字符串的方式获得传入buffer的数据
String s = Encoding.UTF8.GetString(buffer, 0, num);
//通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字符串进行更改
msgText.Dispatcher.Invoke(() => {
msgText.Text = msgText.Text + "消息为" + s;
});
//进行循环的监听
num = cs.ReceiveFrom(buffer, ref remoteEP);
}
//禁用套接字的发送和接受
cs.Shutdown(SocketShutdown.Both);
//关闭套接字
cs.Close();
}
private void getLocalIP()
{
//获得IP地址容器
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
address = ip;
msgText.Text += "服务器的IP地址为:" + ip.ToString();
break;
}
}
if (address == null)
{
msgText.Text += "无法获得服务器IP地址" + "\n";
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Send();
}
}
}
二 TCP
TCP和UDP以套接字的方式实现的区别在于三点:1.TCP需要两个套接字实例,一个用于接受,一个用于与其他端相连接。2.TCP的Receive方法和Send方法与UDP有区别,其他几乎一样。3.多了一个连接方法
1.接收方法
private void Receive()
{
cs.Listen(10);
Task.Run(() => {
Socket client = cs.Accept();
//接受
byte[] buffer = new byte[1024];
//注册对面主机的终结点
EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8002);
msgText.Dispatcher.Invoke(() => {
msgText.Text = msgText.Text + "登录主机为" + remoteEP.ToString();
});
Task.Run(()=> {
while(true)
{
int num = client.ReceiveFrom(buffer, ref remoteEP);
//解析
while (num > 0)
{
//解码:以UTF-8字符串的方式获得传入buffer的数据
String s = Encoding.UTF8.GetString(buffer, 0, num);
//通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字符串进行更改
msgText.Dispatcher.Invoke(() => {
msgText.Text = msgText.Text + "消息为" + s;
});
//进行循环的监听
num = client.ReceiveFrom(buffer, ref remoteEP);
}
}
});
});
}
2.与本地终结点绑定方法
private void BindLocal()
{
cs = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint localEp = new IPEndPoint(address, 8001);
cs.Bind(localEp);
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
3.连接方法
public void Connect()
{
EndPoint remoteEP = new IPEndPoint(address, 8002);
c.Connect(remoteEP);
}
4.发送方法
private void Send()
{
String s = "你好";
byte[] buffer = Encoding.UTF8.GetBytes(s);
c.Send(buffer);
}
成功实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TCP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IPAddress address;
Socket cs;
Socket c;
public MainWindow()
{
InitializeComponent();
}
public void Connect()
{
EndPoint remoteEP = new IPEndPoint(address, 8002);
c.Connect(remoteEP);
}
private void Window_Initialized(object sender, EventArgs e)
{
getLocalIP();
BindLocal();
ThreadStart start = new ThreadStart(Receive);
Thread t1 = new Thread(start);
t1.IsBackground = true;
t1.Start();
}
private void Send()
{
String s = "你好";
byte[] buffer = Encoding.UTF8.GetBytes(s);
c.Send(buffer);
}
//将本地终结点与套接字绑定
private void BindLocal()
{
cs = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint localEp = new IPEndPoint(address, 8001);
cs.Bind(localEp);
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
private void Receive()
{
cs.Listen(10);
Task.Run(() => {
Socket client = cs.Accept();
//接受
byte[] buffer = new byte[1024];
//注册对面主机的终结点
EndPoint remoteEP = new IPEndPoint(IPAddress.None, 8002);
msgText.Dispatcher.Invoke(() => {
msgText.Text = msgText.Text + "登录主机为" + remoteEP.ToString();
});
Task.Run(()=> {
while(true)
{
int num = client.ReceiveFrom(buffer, ref remoteEP);
//解析
while (num > 0)
{
//解码:以UTF-8字符串的方式获得传入buffer的数据
String s = Encoding.UTF8.GetString(buffer, 0, num);
//通过Dispathcer.Invoke()取得对这个文本线程的访问权限,并用解码后的字符串进行更改
msgText.Dispatcher.Invoke(() => {
msgText.Text = msgText.Text + "消息为" + s;
});
//进行循环的监听
num = client.ReceiveFrom(buffer, ref remoteEP);
}
}
});
});
}
private void getLocalIP()
{
//获得IP地址容器
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ip in ips)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
address = ip;
msgText.Text += "服务器的IP地址为:" + ip.ToString();
break;
}
}
if (address == null)
{
msgText.Text += "无法获得服务器IP地址" + "\n";
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Send();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Connect();
}
}
}