开发者

TCP stuff never works for me

I am trying to get a little client/server thing going just to learn how to do it... But after using many samples, following several tutorials even on msdn, none of them have ever worked.

I keep getting the following exception:

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

and the code i have is:

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());
    }
}

Can someone please help me understand why this won't work??

Thank you :)


Have you started the server, before trying to connect with the client?

Also, make sure that the port you are using (8000) isn't blocked by a firewall or occupied by another process.


Basically, the error message says the target machine (the one you're trying to connect to) answered the SYN (connection request packet) with an RST (reset, i.e. no connection possible in this case). What it means is, there's no server process listening on the other end.

Client-server software comes in two parts, the client which is connecting to the second part, the server. The server process needs to be listening on a tcp port in order to accept connections, and the client must connect to the servers IP address, along with the port it's listening on.


Well, if all you are trying to do is test creating a socket connection, use "google.com" as your host name and 80 as your port. That is guaranteed to always be listening. And if it's not, then, well, that would probably mean that the world has stopped.


I figured out why it wasn't working. Recently a little application (which I had no idea about) was installed along with a program that I chose to install, and that app was using up several ports. I've managed to get rid of that app, and everything's working just fine :)

Thank you all for your help and suggestions!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜