我的代码如下 是不是代码有什么毛病
private void ConnectButton_Click(object sender, EventArgs e)
{
try
{
string strIP = IPTextBox.Text.Trim();
IPHostEntry ServerInfor = Dns.Resolve(strIP);
ServerIP = ServerInfor.AddressList[0];
port = Int32.Parse(PortTextBox.Text.Trim());
}
catch(Exception ex)
{
MessageBox.Show("输入的主机地址格式不正确,请重新输入!");
}
try
{
handler = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
ServerEP = new IPEndPoint(ServerIP,port);
handler.BeginConnect(ServerEP,new AsyncCallback(ConnectCallback),handler);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} //连接回调函数
private void ConnectCallback(IAsyncResult ar)
{
Socket ConnSocket = (Socket)ar.AsyncState;
Control.CheckForIllegalCrossThreadCalls = false;
ConnectStateTextBox.Text = "已经连接上主机";
handler.EndConnect(ar);
}
以上是代码
程序走到handler.EndConnect(ar); 就会报出由于目标机器积极拒绝无法连接
是什么原因 有知道的么
我是在一台机器上做的监听根发送 发送也是发送本机 就出现这个问题
private void ConnectButton_Click(object sender, EventArgs e)
{
try
{
string strIP = IPTextBox.Text.Trim();
IPHostEntry ServerInfor = Dns.Resolve(strIP);
ServerIP = ServerInfor.AddressList[0];
port = Int32.Parse(PortTextBox.Text.Trim());
}
catch(Exception ex)
{
MessageBox.Show("输入的主机地址格式不正确,请重新输入!");
}
try
{
handler = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
ServerEP = new IPEndPoint(ServerIP,port);
handler.BeginConnect(ServerEP,new AsyncCallback(ConnectCallback),handler);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
} //连接回调函数
private void ConnectCallback(IAsyncResult ar)
{
Socket ConnSocket = (Socket)ar.AsyncState;
Control.CheckForIllegalCrossThreadCalls = false;
ConnectStateTextBox.Text = "已经连接上主机";
handler.EndConnect(ar);
}
以上是代码
程序走到handler.EndConnect(ar); 就会报出由于目标机器积极拒绝无法连接
是什么原因 有知道的么
我是在一台机器上做的监听根发送 发送也是发送本机 就出现这个问题
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货