我想获得一个小小的客户端/服务器的事情,只是为了学习如何做到这一点......但使用了许多样本后,即使在MSDN上的几个教程,他们都没有工作过。TCP的东西从来没有为我工作
我不断收到以下异常:
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 220.101.27.107:8000
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at t.MainForm.toolStripButton1344_Click(Object sender, EventArgs e) in C:\Users\Jason\Documents\Visual Studio 2008\Projects\t\t\MainForm.cs:line 1648
和代码,我是:
private void toolStripButton1344_Click(object sender, EventArgs e)
{
String strHostName;
string ipaddy;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
ipaddy = addr[i].ToString();
}
Socket st = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(addr[0], 8000);
try
{
st.Connect(ipe);
}
catch (ArgumentNullException ae)
{
MessageBox.Show("ArgumentNullException : {0}" + ae.ToString());
}
catch (SocketException se)
{
MessageBox.Show("SocketException : {0}" + se.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Unexpected exception : {0}" + ex.ToString());
}
}
是否有人可以帮助我理解为什么这是不行的?
谢谢:)
你还没有显示8000端口正在监听... – 2010-02-11 06:26:53
@Jon:我猜他不知道什么应该(没有冒犯) – falstro 2010-02-11 06:31:57
@Tommy:也许你应该读一些关于TCP(例如:http: //en.wikipedia.org/wiki/Transmission_Control_Protocol)了解TCP/IP基础知识。 – 2010-02-11 07:32:30