Udp通信 暑期学习笔记(三)
有了笔记(二)中的积累,udp的感觉就简单多了。
Code
#region usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
#endregion
namespace NetStudy
{
delegate void AppendItem(object o);
public partial class MyTalkUdp : Form
{
#region Vars
Thread serverThread;
EndPoint remoteIpep;
Socket serverSock, clicentSock;
AppendItem AddItem;
void addItem(object o)
{
listBox.Items.Add(o);
}
string message;
#endregion
#region Constructions
public MyTalkUdp()
{
InitializeComponent();
AddItem = new AppendItem(addItem);
}
#endregion
#region Events
private void btSent_Click(object sender, EventArgs e)
{
IPAddress[] ipalist = Dns.GetHostAddresses(txtTarget.Text.Trim());
if (ipalist.GetLength(0) <= 0)
{
listBox.Items.Add("Invalid Target!");
return;
}
IPAddress ipa = ipalist[0];
remoteIpep = new IPEndPoint(ipa, 8989);
message = txtMessage.Text;
txtMessage.Clear();
SentMessage();
listBox.Items.Add("TO " + remoteIpep.ToString() + ":" + message);
}
private void MyTalkUdp_Load(object sender, EventArgs e)
{
serverThread = new Thread(new ThreadStart(StartListen));
serverThread.Start();
}
#endregion
#region Methods
void StartListen()
{
serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
serverSock.Bind(new IPEndPoint(IPAddress.Any, 8989));
byte[] buf=new byte[1024];
IPEndPoint remotePoint=null;
int bufLen = 0;
bool isKeepAlive=true;
string data;
while (isKeepAlive)
{
try
{
bufLen = serverSock.Available;
if (bufLen != 0)
{
serverSock.ReceiveFrom(buf, SocketFlags.None,ref remoteIpep);
data=System.Text.Encoding.Unicode.GetString(buf, 0, bufLen);
if (!string.IsNullOrEmpty(data))
{
listBox.Invoke(AddItem, "FROM " + remoteIpep.ToString() + ":" + data);
}
}
}
catch (SocketException sexc)
{
listBox.Invoke(AddItem, sexc.Message);
}
}
}
void SentMessage()
{
if (string.IsNullOrEmpty(message)) return;
byte[] buf=new byte[1024];
buf = System.Text.Encoding.Unicode.GetBytes(message);
clicentSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
clicentSock.SendTo(buf, remoteIpep);
}
catch (SocketException sexc)
{
listBox.Items.Add(sexc.Message);
}
}
#endregion
}
}
#region usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
#endregion
namespace NetStudy
{
delegate void AppendItem(object o);
public partial class MyTalkUdp : Form
{
#region Vars
Thread serverThread;
EndPoint remoteIpep;
Socket serverSock, clicentSock;
AppendItem AddItem;
void addItem(object o)
{
listBox.Items.Add(o);
}
string message;
#endregion
#region Constructions
public MyTalkUdp()
{
InitializeComponent();
AddItem = new AppendItem(addItem);
}
#endregion
#region Events
private void btSent_Click(object sender, EventArgs e)
{
IPAddress[] ipalist = Dns.GetHostAddresses(txtTarget.Text.Trim());
if (ipalist.GetLength(0) <= 0)
{
listBox.Items.Add("Invalid Target!");
return;
}
IPAddress ipa = ipalist[0];
remoteIpep = new IPEndPoint(ipa, 8989);
message = txtMessage.Text;
txtMessage.Clear();
SentMessage();
listBox.Items.Add("TO " + remoteIpep.ToString() + ":" + message);
}
private void MyTalkUdp_Load(object sender, EventArgs e)
{
serverThread = new Thread(new ThreadStart(StartListen));
serverThread.Start();
}
#endregion
#region Methods
void StartListen()
{
serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
serverSock.Bind(new IPEndPoint(IPAddress.Any, 8989));
byte[] buf=new byte[1024];
IPEndPoint remotePoint=null;
int bufLen = 0;
bool isKeepAlive=true;
string data;
while (isKeepAlive)
{
try
{
bufLen = serverSock.Available;
if (bufLen != 0)
{
serverSock.ReceiveFrom(buf, SocketFlags.None,ref remoteIpep);
data=System.Text.Encoding.Unicode.GetString(buf, 0, bufLen);
if (!string.IsNullOrEmpty(data))
{
listBox.Invoke(AddItem, "FROM " + remoteIpep.ToString() + ":" + data);
}
}
}
catch (SocketException sexc)
{
listBox.Invoke(AddItem, sexc.Message);
}
}
}
void SentMessage()
{
if (string.IsNullOrEmpty(message)) return;
byte[] buf=new byte[1024];
buf = System.Text.Encoding.Unicode.GetBytes(message);
clicentSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
clicentSock.SendTo(buf, remoteIpep);
}
catch (SocketException sexc)
{
listBox.Items.Add(sexc.Message);
}
}
#endregion
}
}