locked
No connection could be made because the target machine actively refused it

    Question

  • Trying to connect to a local host TCP port 3380 (the port number chosen arbitrarily): 10.1.10.176:3380 I got this error message:

    No connection could be made because the target machine actively refused it

    What shall I do to convince the target machine not to refuse it the next time?

    I used a

    sock.Connect(serverEndPoint);

    statement.

    Thanks
    Thursday, November 17, 2005 5:11 PM

Answers

  • It should work... 

    1. The receive is a blocking call. Is the other end sending...

    2. Did you initialize totalBytesRcvd to 0?

    Thursday, November 17, 2005 10:23 PM
  • What is the size of the buffer you are reading the data into?  If the size of the buffer is larger than the size of the data that the server is going to send, then the receive call will block once it has read all of the data the server is sending. 

    In other words:  If you buffer is 128 bytes long and the server is only sending 124 bytes, then you will read the 124 bytes from the server and then issue another Receive call waiting for the remaining 4 bytes that you think the server is going to send (which the server will never send).  This could result in an apparent hang due to the fact that the client and server aren't in agreement of how much data is being transmitted.  You could try setting the ReceiveTimeout on the client socket.

    In V2.0 there is a ReceiveTimeout property.  In previous versions you can call


    socket.SetSocketOption(SocketOptionLevel.Socket,
                                      SocketOptionName.ReceiveTimeout, timeoutValue);


     

    Thursday, November 17, 2005 10:57 PM

All replies

  • This means that there is no TCP Port *LISTENING* for requests at the port 3380
    you mentioned. It could also mean that there is a firewall of some sorts that is
    not allowing incoming connections on that port eventhough you might have a server listening on that port
    Thursday, November 17, 2005 5:18 PM
  • Thanks a lot.

    I just ordered a Windows Server TCP/IP Reference book you've mentioned and a host of others at Amazon.com.

    No matter how many books we read we will keep asking stupid questions though.

    Thursday, November 17, 2005 6:54 PM
  • You were right. When I chose a post where a server was listening a connection was made right away, however,

    while (totalBytesRcvd < byteBuffer.Length)
      {
    if ((bytesRcvd = sock.Receive(byteBuffer, totalBytesRcvd,
           byteBuffer.Length - totalBytesRcvd,
    SocketFlags.None)) == 0)
           {
    Console.WriteLine("Connection closed prematurely.");
            break;
      }
      totalBytesRcvd += bytesRcvd;
    }

    At the IF statement the program stopped responding (in debug) for unknown reason and without giving me any identifyable error message or demand for Console input. 11 bytes were sent to the server. Any insight please?

    Thursday, November 17, 2005 7:51 PM
  • It should work... 

    1. The receive is a blocking call. Is the other end sending...

    2. Did you initialize totalBytesRcvd to 0?

    Thursday, November 17, 2005 10:23 PM
  • What is the size of the buffer you are reading the data into?  If the size of the buffer is larger than the size of the data that the server is going to send, then the receive call will block once it has read all of the data the server is sending. 

    In other words:  If you buffer is 128 bytes long and the server is only sending 124 bytes, then you will read the 124 bytes from the server and then issue another Receive call waiting for the remaining 4 bytes that you think the server is going to send (which the server will never send).  This could result in an apparent hang due to the fact that the client and server aren't in agreement of how much data is being transmitted.  You could try setting the ReceiveTimeout on the client socket.

    In V2.0 there is a ReceiveTimeout property.  In previous versions you can call


    socket.SetSocketOption(SocketOptionLevel.Socket,
                                      SocketOptionName.ReceiveTimeout, timeoutValue);


     

    Thursday, November 17, 2005 10:57 PM
  • Answering your second question (and I appreciate both) - yes it was initialized o 0.

    I can answer the first question with 85% certainty only. A system process Winnt\system32\MStask.exe was attached to this port and I realized a few minutes after I posted this that probably the other side was either not sending anything or the process was attached to this port as a perfunctory measure for some unknown contingencies.

    Those dynamic considerations escape newbies, as you know. It takes time.

    I will make sure there is a full duplex communication via a port next time.

    Thanks.
    Friday, November 18, 2005 2:24 PM
  • Thanks, this is great. I had vaguely those considerations in mind. You've opened the road for me now. I will implement it.
    Friday, November 18, 2005 2:27 PM
  • The buffer size was actually 32 bytes. Is it too small for this purpose?

    Thanks.
    • Proposed as answer by Vishal Shukla Thursday, September 10, 2009 5:46 PM
    Friday, November 18, 2005 2:31 PM
  • The buffer can be of any size.. Since you were waiting in the loop till you get the whole byteBuffer.Length,  you will block if the other end is sending data less than byteBuffer.Length and has not closed the socket (On a socket closure receive will return 0 bytes)


    while (totalBytesRcvd < byteBuffer.Length)
      {
    if ((bytesRcvd = sock.Receive(byteBuffer, totalBytesRcvd,
           byteBuffer.Length - totalBytesRcvd,
    SocketFlags.None)) == 0)
           {
    Console.WriteLine("Connection closed prematurely.");
            break;
      }
      totalBytesRcvd += bytesRcvd;
    }
    Friday, November 18, 2005 7:21 PM

  • HI Guys

    It's very easy. please first understand, what will to do try.?????  u try send a byte of data server to Client cliet to Server. So those two type of work, whether one is  Do the Job as the receiver. So

    We must Start that Receiver , rather than start the Sender.

    if u confuse.

    Please start Server first,

    after start Client Do

    it's work.

    tnx Guys.,

    Sunday, July 22, 2007 10:45 AM