SocketUDP
自己即是服務器又是客戶端 ,在發消息只需要改成對方ip和端口號即可
前提對方必須開啟服務器 socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.107.72"), 8080));
控件:Button,TextBox,RichTextBox
打開自己服務器
public Form1()
{InitializeComponent();//1創建一個服務器 綁定的是ip和端口號 192.168.107.83, 8080// 張三的終端 以后誰想跟張三聊的時候 發這個ip和端口號socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);socket.Bind(new IPEndPoint(IPAddress.Any, 8080));startReceive();
}
Socket socket;
void startReceive()
{byte[] bs = new byte[1024];Task.Run(() =>{while (true){int count = socket.Receive(bs);string s = Encoding.UTF8.GetString(bs,0, count);this.Invoke((Action)(() =>{richTextBox1.AppendText(s + "\t\n");}));}});
}
發送按鈕事件
private void button1_Click(object sender, EventArgs e)
{//發消息的一定要注意iphe端口號socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),new IPEndPoint(IPAddress.Parse("192.168.107.73"), 8082));
}
Socket組播服務器
組播需要使用組播地址,在 IPv4 中它的范圍從 224.0.0.0 到 239.255.255.255,
并被劃分為局部鏈接多播地址、預留多播地址和管理權限多播地址三類
224.0.0.0 ~ 224.0.0.255: 局部鏈接多播地址:是為路由協議和其它用途保留的地址, ?
只能用于局域網中,路由器是不會轉發的地址 224.0.0.0 不能用,是保留地址 ?
224.0.1.0 ~ 224.0.1.255: 為用戶可用的組播地址(臨時組地址),可以用于 Internet 上的。
224.0.2.0 ~ 238.255.255.255: 用戶可用的組播地址(臨時組地址),全網范圍內有效 ?
239.0.0.0 ~ 239.255.255.255: 為本地管理組播地址,僅在特定的本地范圍內有效
public Form1(){InitializeComponent();}Socket socket;private void button1_Click(object sender, EventArgs e){//1創建socket對象socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//2 綁定ip和端口號socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.107.83"), 8080));//3 加入組播地址//SetSocketOption 添加套接字可配置選項//參數1.支持協議類型,//參數2 添加組播地址的功能//參數3 要組播的地址socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,new MulticastOption(IPAddress.Parse("224.0.0.188")));//4以后大家要是接受的話 接受組內發來的消息的時候 必須添接受組內代碼//5 接收數據startReceive();
}
void startReceive()
{new Thread(() =>{byte[] buffer = new byte[1024];while (true){int count = socket.Receive(buffer);string s = Encoding.UTF8.GetString(buffer, 0, count);richTextBox1.Invoke((Action)(() =>{richTextBox1.AppendText(s + "\t\n");}));}}).Start();
}//發送消息
private void button2_Click(object sender, EventArgs e)
{//發消息指定組地址進行發送,以后要求接受消息端口號和此處端口號保持一致socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),new IPEndPoint(IPAddress.Parse("224.0.0.188"), 10086));
}
socket組播客戶端
控件:
兩個按鈕(客戶端打開接收消息,發送),textbox,richtextBox
public Form1(){InitializeComponent();}Socket socket;private void button1_Click(object sender, EventArgs e){//1創建socket對象socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//2 綁定ip和端口號socket.Bind(new IPEndPoint(IPAddress.Any, 10086));//3 設置組地址 對客戶端加入指定組播地址內socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,new MulticastOption(IPAddress.Parse("224.0.1.188")));//4 接受消息Task.Run(() =>{while (true){byte[] buffer = new byte[1024];//int count = socket.Receive(buffer);EndPoint ip = new IPEndPoint(IPAddress.None, 0); //定義endpoint類型變量,終端類型//ReceiveFrom()接受數據的方法 從哪個終端發來的消息//參數3是發來消息的地址 類型是endpointint count = socket.ReceiveFrom(buffer, 0, ref ip);IPEndPoint i1 = ip as IPEndPoint; // 把endpoint類型 轉成IPEndPointstring s = Encoding.UTF8.GetString(buffer, 0, count);richTextBox1.Invoke((Action)(() =>{richTextBox1.AppendText(i1.Address+":"+s + "\t\n");richTextBox1.SelectionStart=richTextBox1.Text.Length; richTextBox1.ScrollToCaret();}));}});}private void button2_Click(object sender, EventArgs e){//socket.SendTo(Encoding.UTF8.GetBytes("hello world"),// new IPEndPoint(IPAddress.Parse("192.168.107.83"), 8080));if (Encoding.UTF8.GetBytes(this.textBox1.Text).Length>1024){return;}socket.SendTo(Encoding.UTF8.GetBytes(this.textBox1.Text),new IPEndPoint(IPAddress.Parse("224.0.1.188"), 10086));}