服务器端程序运行完private void target()(红色字体部分),就返回Form窗体界面,不往下执行了,我想它应该是等客户端连接;而客户端程序而执行到 private void ConnectCallback(IAsyncResult ar)函数里的 socket.EndConnect(ar)后(蓝色字体部分),抛出异常,说我所提供的本机IP地址是不可连接的网络,我看程序,找不出什么问题,麻烦大家帮帮忙,先谢谢了!
服务器端的程序:
namespace asyncSocket
{
public partial class Form1 : Form
{
private Socket SocketSV;
private Socket handler=null ;
private IPAddress myIp;
private IPEndPoint myhost;
private static ManualResetEvent myEvent=new ManualResetEvent (false );
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnBeginListen_Click(object sender, EventArgs e)
{
string ipstr=txbIPaddress .Text ;
string portstr=txbPort.Text ;
SocketSV = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
myIp = IPAddress.Parse(ipstr);
myhost = new IPEndPoint(myIp, Convert.ToInt32(myhost));
}
catch
{
MessageBox.Show("IP地址或端口输入格式错误!");
}
try
{
SocketSV.Bind(myhost);
SocketSV.Listen(50);
Thread accThread = new Thread(new ThreadStart(target));
accThread.Start();
}
catch
{
MessageBox.Show("绑定端口出错!");
}
}
private void target()
{
while (true)
{
myEvent.Reset();
try
{
SocketSV.BeginAccept(new AsyncCallback(AcceptCallback), SocketSV);
}
catch
{
MessageBox.Show("开始连接出错!");
}
myEvent.WaitOne();
}
}
private void AcceptCallback(IAsyncResult ar)
{
myEvent.Set();
Socket socket = (Socket)ar.AsyncState;
stateobject so = new stateobject();
handler = socket.EndAccept(ar);
txbServerState.Text = "已经建立连接!";
so.SocketAccept = handler;
Thread recvThread = new Thread(new ThreadStart(Receive));
recvThread.Start();
}
private void Receive()
{
stateobject so = new stateobject();
so.SocketAccept = handler;
try
{
handler.BeginReceive(so.bytesRecv, 0, so.BufferSize, 0, RecvCallback, so);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void RecvCallback(IAsyncResult ar)
{
stateobject so = (stateobject)ar.AsyncState;
Socket socket = so.SocketAccept;
int bytesRecv = socket.EndReceive(ar);
String str= System.Text.Encoding.BigEndianUnicode.GetString(so.bytesRecv);
rtbReceive.AppendText(str + "\r\n");
socket.BeginReceive(so.bytesRecv, 0, so.BufferSize, 0, new AsyncCallback(RecvCallback), so);
}
private void btnSend_Click(object sender, EventArgs e)
{
byte[] bytesSend=new byte [1024];
string wordsTosend = txbSend.Text;
bytesSend = System.Text.Encoding.BigEndianUnicode.GetBytes(wordsTosend);
stateobject so = new stateobject();
try
{
if (handler != null)
{
so.SocketAccept = handler;
handler.BeginSend(bytesSend, 0, wordsTosend.Length, 0, SendCallback, so);
}
}
catch
{
MessageBox.Show("发送数据失败!");
}
}
private void SendCallback(IAsyncResult ar)
{
stateobject so = (stateobject)ar.AsyncState;
Socket socket = so.SocketAccept;
try
{
int bytesSend = socket.EndSend(ar);
}
catch
{
MessageBox.Show("发送数据失败!");
}
}
private void btnStopListen_Click(object sender, EventArgs e)
{
if (SocketSV.Connected)
{
SocketSV.Shutdown(SocketShutdown.Both);
SocketSV.Close();
}
}
}
public class stateobject
{
public Socket SocketAccept;
public int BufferSize = 1024;
//public static StringBuilder sb;
public byte[] bytesRecv = new byte[1024];
public stateobject()
{
}
}
}
客户端的程序:
namespace asyncSocketClient
{
public partial class Form1 : Form
{
private Socket clientSk;
private byte[] bytesRecv = new byte[1024];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnConnect_Click(object sender, EventArgs e)
{
IPAddress myAddress = IPAddress.Parse(txbIPaddress.Text);
IPEndPoint myEndPoint = new IPEndPoint(myAddress, Convert.ToInt32(txbPort.Text));
clientSk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
clientSk.BeginConnect(myEndPoint, new AsyncCallback(ConnectCallback), clientSk);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void ConnectCallback(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
try
{
socket.EndConnect(ar);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
Thread recvThread = new Thread(new ThreadStart(Receive));
recvThread.Start();
}
private void Receive()
{
try
{
clientSk.BeginReceive(bytesRecv, 0, bytesRecv.Length, 0, new AsyncCallback(RecvCallback), clientSk);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void RecvCallback(IAsyncResult ar)
{
int count;
Socket socket=(Socket )ar.AsyncState ;
try
{
count = socket.EndReceive(ar);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
string str = System.Text.Encoding.BigEndianUnicode.GetString(bytesRecv);
rtbRecv.AppendText(str);
str = "";
socket.BeginReceive(bytesRecv, 0, bytesRecv.Length, 0, new AsyncCallback(RecvCallback), clientSk);
}
private void btnSend_Click(object sender, EventArgs e)
{
string str = txbSend.Text;
byte[] bytesSend = new byte[1024];
bytesSend = System.Text.Encoding.BigEndianUnicode.GetBytes(str);
try
{
clientSk.BeginSend(bytesSend, 0, str.Length, 0, new AsyncCallback(SendCallback), clientSk);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void SendCallback(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
try
{
socket.EndSend(ar);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void btnStopCon_Click(object sender, EventArgs e)
{
clientSk.Shutdown(SocketShutdown.Both);
clientSk.Close();
}
}
}