---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
学习内容:Socket 服务端与客户端的交互
源代码:
服务端:
Thread thread = null;
Thread thread1 = null;
Socket socket = null;
private void button1_Click(object sender, EventArgs e)
{
try
{
showMes("正在启动...");
//创建 服务端 负责监听的套接字 参数(使用ip4寻址协议,使用流式连接,使用Tcp协议传输数据)
socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//获取文本框中的IP地址对象
IPAddress ipAddress = IPAddress.Parse(ip.Text.Trim());
//创建IP和网络端口的网络节点对象
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress,int.Parse(duankou.Text));
//将负责监听的套接字 绑定到唯一的端口上
socket.Bind(ipEndPoint);
//设置监听队列的长度
socket.Listen(10);
thread = new Thread(Lianjie);
thread.IsBackground = true;
thread.Start();
showMes("服务启动成功!");
button1.Enabled = false;
}
catch(SocketException ex)
{
showMes(ex.Message);
}
catch (Exception ex)
{
showMes(ex.Message);
}
}
Dictionary<string,Socket> LianjieList=new Dictionary<string,Socket>();
public void Lianjie()
{
while (true)
{
try
{
Socket socketconnection = socket.Accept();
LianjieList.Add(socketconnection.RemoteEndPoint.ToString(), socketconnection);
zaixian.Items.Add(socketconnection.RemoteEndPoint.ToString());
thread1 = new Thread(jieshou);
thread1.IsBackground = true;
thread1.Start(socketconnection);
showMes(socketconnection.RemoteEndPoint.ToString() + "连接成功!");
}
catch (SocketException ex)
{
showMes("异常:" + ex.Message);
break;
}
catch (Exception ex)
{
showMes("异常:" + ex.Message);
break;
}
}
}
public void jieshou(object newsocket)
{
Socket socketd = newsocket as Socket;
while (true)
{
try
{
byte[] ReceiveMesbyte = new byte[1024 * 1024 * 2];
int ReceiveMesLength = socketd.Receive(ReceiveMesbyte);
string ReceiveMes = System.Text.Encoding.UTF8.GetString(ReceiveMesbyte, 0, ReceiveMesLength);
showMes(ReceiveMes);
}
catch (SocketException ex)
{
showMes("异常:" + ex.Message);
break;
}
catch (Exception ex)
{
showMes("异常:" + ex.Message);
break;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string SendStrMes = SendText.Text;
byte[] SendMes = System.Text.Encoding.UTF8.GetBytes(SendStrMes);
string o=zaixian.Text;
try
{
LianjieList[o].Send(SendMes);
}
catch (SocketException ex)
{
showMes("异常:" + ex.Message);
}
catch (Exception ex)
{
showMes("异常:" + ex.Message);
}
showMes("给" + LianjieList[o].RemoteEndPoint.ToString() + "发送了数据" + SendStrMes);
}
private void button3_Click(object sender, EventArgs e)
{
string SendStrMes = SendText.Text;
byte[] SendMes = System.Text.Encoding.UTF8.GetBytes(SendStrMes);
foreach (KeyValuePair<string, Socket> so in LianjieList)
{
so.Value.Send(SendMes);
}
showMes("发送完毕!");
}
public void showMes(string Mess)
{
Mes.Text = "系统消息:" + Mess + "\r\n" + Mes.Text;
}
}
客户端:
Socket socket = null;
Thread thred = null;
private void button1_Click(object sender, EventArgs e)
{
showMes("正在连接。。。");
IPAddress ipAddress = IPAddress.Parse(ip.Text.Trim());
IPEndPoint ipEndpoint = new IPEndPoint(ipAddress,int.Parse(duankou.Text));
socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
socket.Connect(ipEndpoint);
thred = new Thread(KhLianjie);
thred.IsBackground = true;
thred.Start();
showMes("连接服务器成功!");
}
catch (SocketException ex)
{
showMes("异常:"+ex.Message);
}
catch(Exception ex)
{
showMes("异常:" + ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
string SendStrMes = SendText.Text;
byte[] SendMes = System.Text.Encoding.UTF8.GetBytes(SendStrMes);
socket.Send(SendMes);
showMes("给" + socket.RemoteEndPoint.ToString() + "发送了数据" + SendStrMes);
}
public void KhLianjie()
{
while (true)
{
byte[] ReceiveMesbyte = new byte[1024 * 1024 * 2];
try
{
int ReceiveMesLength = socket.Receive(ReceiveMesbyte);
string ReceiveMes = System.Text.Encoding.UTF8.GetString(ReceiveMesbyte, 0, ReceiveMesLength);
showMes(ReceiveMes);
}
catch (SocketException ex)
{
showMes("异常:" + ex.Message);
break;
}
catch (Exception ex)
{
showMes("异常:" + ex.Message);
break;
}
}
}
public void showMes(string Mess)
{
LogText.Text ="系统消息:"+ Mess + "\r\n" + LogText.Text;
}
}
总结:服务器端的Socket(至少需要两个)
1. 一个负责接受客户端连接请求(但不负责于客户端通信)
2.为每个连接成功的客户端创建一个对应的Socket.
客户端的SOcket
1.必须指定要连接的服务器地址和端口。
2.通过创建一个Socket对象来初始化一个到服务器的TCP连接。
遇到的问题:
每当关闭socket [socket.close();]时,就提示异常“一个封锁操作被对 WSACancelBlockingCall 的调用中断!”。
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------