Results 1 to 26 of 26

Thread: localhost: 10061 Connection is forcefully rejected

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    localhost: 10061 Connection is forcefully rejected

    I have a server program written in Java which is listening on a specific TCP port. And I have a client program written with Microsoft Access/VBA which uses the Winsock ActiveX control (MSWINSCK.OCX, Version 6.1.98.17) to communicate with the server program. Both programs are written by myself.
    Everything works well ... as long as the client and server programs run on different machines.
    If both programs reside on the same machine the client program gets the error code 10061 "Connection is forcefully rejected" when trying to connect to the server which I have started before.

    I use two computers, one with Windows XP and Access 2003 (also Access 2000 present), and the other with Windows 7 (32 bit) with Access 2010.
    The problem occurs on both computers. I verified that when the problem occurs I can nevertheless connect to the server from the other computer (So everything seems to be correct with the server).
    The use of Access 2000 instead of Access 2003 on the XP machine does not make any difference. Also the use of "localhost" or "127.0.0.1" as target host does not make a difference.

    In case of the successful operation the server may reside on the Windows 7 machine and the client on the XP machine, or vice versa.

    Any suggestions what's going wrong?

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    The problem is server side, not client side.

    Most common reasons:

    1) The server is not listening on the specified port
    2) Also, maybe there is a firewall problem

    Check your server code and make sure it is always in the listening mode.

    A lot of programmers do not allow the server to return back to the listening mode or not listening at all on startup, very common mistake in coding.

    The server should always be listening for a connection. When one comes in the server accepts the connection, opens up a new socket for that client, not the server's main socket which must be only used for listening and establishing the connection, and then the server must return back to the listening mode.

    Use socket(0) for the server to listen on
    Use socket(1) for the first connection
    Use socket(2) for the second connection
    etc, etc

    Posting your server code would really help us solve your problem.
    Last edited by jmsrickland; May 15th, 2012 at 10:13 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: localhost: 10061 Connection is forcefully rejected

    Thank you for the reply.

    The server waits in an endless loop for a client to connect.
    The accept method of the java.net.ServerSocket object is called in the loop.
    When it returns, a new client has connected and a new java.net.Socket object is returned. I start a new thread with this socket object as a parameter to process the connect being aware that multiple clients can be connected at a time.

    -- I just figured out that everything works well if the client uses the real IP address of the computer, not "localhost" or "127.0.0.1". But of course that is not what I want. I want to use "localhost" because in my case it is most common to have client and server on the same computer.
    Is there anything special in using "127.0.0.1" compared to the "real" IP address?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    What do you mean by real IP?

    Here is how it works (at least this is how it works in VB)

    Server listens on localhost (sometimes referred to as Internal IP) which is something like 192.nnn.1.1. Server does not listen on 127.0.0.1

    Client connects:
    1) Client on same computer as Server can use either localhost(127.0.0.1) or Internal IP
    2) Client on different computer but same network uses Internal IP of the computer Server is on.
    3) Client on remote computer uses External IP

    BTW: Are you writing a Java Applet or a Java Application?
    Last edited by jmsrickland; May 18th, 2012 at 01:47 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: localhost: 10061 Connection is forcefully rejected

    Does the Server machine have more than one Network Adapter? If so you should Bind the port and IP Address in your Java Application to make sure it's actually listening correctly.

    (In Winsock 'parlance' it'd be
    Code:
    Winsock.Bind 40002,"localhost"
    where 40002 is the Port Number)

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: localhost: 10061 Connection is forcefully rejected

    Thank you jmsrickland and Doogle for your support.

    I bound the listening server (in Java parlance) to InetAddress.getLocalHost() which indeed is not 127.0.0.1 but something like 192.168.x.y (That is what I called the "real" address).
    If I change the binding to 127.0.0.1 it works for the client on the same computer to connect to localhost. But then a client on another computer is not able to connect to the server ...

    jmsrickland, you wrote:
    "Server listens on localhost (sometimes referred to as Internal IP) which is something like 192.nnn.1.1. Server does not listen on 127.0.0.1"
    This is correct and how I implemented it.

    "Client connects:
    1) Client on same computer as Server can use either localhost or 127.0.0.1"

    This is the problem because this does not work with my programs.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    Personally I would never code an app to use the 127 address and would always use the IP of the network card as this is the only way a remote machine can connect and that is typically the goal of a server app.

    Are you still having a problem when using the proper IP or only when usng the 127 .... address?

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    I'm just going to try and cover all possibilities. I don't use Java too much and it's been years since I have done any socket programming so when you set up your server using InetAddress.getLocalHost() does this means that Java determines your "localhost" address and not you. Is this correct. If not, and it is you that determines the address how do you do it? Maybe you are not using the correct internal address. If your computer is on a network then you should know that each computer has a different internal IP like

    computer 1 has 192.168.x.1
    computer 2 has 192.168.x.2
    computer 3 has 192.168.x.3
    etc

    So as to get my head straight can you please refer to other computers as either on the same network or remote? That would help me understand a little bit more.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: localhost: 10061 Connection is forcefully rejected

    DataMiser,
    I have no problems if I use the IP address of the network card (The address the server is bound to).
    However, my intention was that the client can have "localhost" (or 127.0.0.1) in its standard configuration to connect to the server which fits in all cases the server runs on the same machine as the client. The client configuration only has to be changed in cases where the server is on a different machine.

    jmsrickland,
    if I speak of different computers I mean computers on the same network.
    My server program binds its service to the address returned by InetAddress.getLocalHost().
    If the server program runs on one computer InetAddress.getLocalHost() may return 192.168.1.1, and 192.168.1.2 if it runs on another computer. (I don't know what happens if the server computer has more than one active network cards.)

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    I have two computers on a LAN. Server is on computer1 and computer1 has a client and computer2 also has a client. On computer1 client connects to server on 127.0.0.1:8192 (also localhost will work) and client on computer2 connects to server on computer1's localhost:8192 but not 127.0.0.1. I have never had a 10061 error whether I use VB, C, or Java (I found an old Java app and tried it out). You say if your app works if server and client are on different computers but it does not work if server and client are on same computer.

    In your post #1 you say:

    Everything works well ... as long as the client and server programs run on different machines.
    If both programs reside on the same machine the client program gets the error code 10061 "Connection is forcefully rejected" when trying to connect to the server which I have started before.


    When client is on different computer you are connecting to server on the server's 'real' IP, is this correct? When client is on same computer you use localhost or real IP, right?
    Last edited by jmsrickland; May 18th, 2012 at 01:49 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    Quote Originally Posted by asterix48 View Post
    DataMiser,
    I have no problems if I use the IP address of the network card (The address the server is bound to).
    However, my intention was that the client can have "localhost" (or 127.0.0.1) in its standard configuration to connect to the server which fits in all cases the server runs on the same machine as the client. The client configuration only has to be changed in cases where the server is on a different machine.
    That really makes no sense to me, There is absolutely no need to use the 127 address. It does not matter if your client is on the same machine or a different machine when you use the proper IP address. If your intent is to have the client connect without requiring the user enter the IP when on the local machine then you can simply write a line of code that gets the machines IP and use that.

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    Personally, I think terms like 'real' IP and 'proper' IP are misleading. The terms are Internal IP, External IP, localhost and loop-back IP (same as 127.0.0.1).

    My Internal IP is 192.168.x.y
    My External IP is 72.87.16x.nn
    My localhost IP is 127.0.0.1
    My loop-back IP is 127.0.0.1

    I have no idea what my 'real' IP is nor the 'proper' IP
    Last edited by jmsrickland; May 18th, 2012 at 01:49 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    By proper IP I mean the Internal IP address, not the generic 127 address.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: localhost: 10061 Connection is forcefully rejected

    jmsrickland,
    thank you for clarifying some therms. But in one point I disagree:

    If I enter
    ping localhost
    on the command line I get the answer:
    "Pinging MyComputerName [127.0.0.1] with 32 bytes of data:"

    So I think localhost is a synonym for the loop-back IP.

    DataMiser,
    I already programmed the determination of the computers internal IP address in VBA (somewhat more than one line of code).
    The circumvention for my problem reads something like this:

    If the client program reads "localhost" or "127.0.0.1" from the configuration file it uses the determined internal IP
    else it uses the entry from the configuration file as IP address (or as host name). No so elegant, isn't it?


    Thank you to all for your support.

  15. #15
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    If I enter
    ping localhost
    on the command line I get the answer:
    "Pinging MyComputerName [127.0.0.1] with 32 bytes of data:"


    I stand corrected. Thanks for the above info.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    Not sure why you would need more than one line to determine the ip

    All you should need is something like this
    Code:
    Winsock1.Remotehost=Winsock1.LocalIP
    And why write the 127 to the config file. I would write the IP I am going to be using which in the case of a single PC would be the Winsock1.LocalIP


    From one of my server projects
    Code:
    Me.Caption = "IP=" & Winsock1(0).LocalIP & " - Port=" & Winsock1(0).LocalPort
    Gives me and address of 192.168.x.x

  17. #17
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    He is using Java, I believe.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  18. #18
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: localhost: 10061 Connection is forcefully rejected

    I suspect the problem lies at this "server."

    This doesn't inspire much confidence either:
    Quote Originally Posted by asterix48 View Post
    The server waits in an endless loop for a client to connect.
    The accept method of the java.net.ServerSocket object is called in the loop.
    A server shouldn't be looping at all, but be sitting with the socket "listening" while in some alertable wait state.


    More likely the issue here is timing and the use of the loopback address is very fast so it can "miss" those times when the TCP endpoint is available to accept a connection. Other forms of IP addresses or names go through a lengthier slower process that makes them somewhat more likely to get lucky and get a hit.

    The error says it all: there is no listener to connect to.

  19. #19
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    Quote Originally Posted by jmsrickland View Post
    He is using Java, I believe.
    He said

    I already programmed the determination of the computers internal IP address in VBA (somewhat more than one line of code).
    The circumvention for my problem reads something like this:
    I thought the looping sounded odd as well but then I do not use Java so I did not say anything.

    My server program uses a winsock control array with the first one being the listener and the others used to accept clients as they request a connection. Just need to make sure the IP address is reachable, the listener is actually listening and there is code to accept the connection requests.
    Last edited by DataMiser; May 18th, 2012 at 02:38 PM.

  20. #20
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    Quote Originally Posted by DataMiser View Post
    He said
    I already programmed the determination of the computers internal IP address in VBA (somewhat more than one line of code).
    The circumvention for my problem reads something like this:
    He also said

    My server program binds its service to the address returned by InetAddress.getLocalHost().

    is that not Java?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  21. #21
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: localhost: 10061 Connection is forcefully rejected

    Doesn't really matter. Sounds like his Java server is flaky to me.

  22. #22
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: localhost: 10061 Connection is forcefully rejected

    Sounds that way to me also


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  23. #23
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: localhost: 10061 Connection is forcefully rejected

    I assume he was talking about the client as that is the one that needs to know what IP address to connect to.

    I also believe the Java Server is a bit flaky

  24. #24
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: localhost: 10061 Connection is forcefully rejected

    Quote Originally Posted by dilettante View Post
    A server shouldn't be looping at all, but be sitting with the socket "listening" while in some alertable wait state.
    From what I've read about Java, the 'ServerSocket' class will wait until a connection request is made. Of course, if OP is not using that class and something else, then who knows what's going on.

  25. #25
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: localhost: 10061 Connection is forcefully rejected

    I think that Java class has an inhernet flaw that was necessary in order to use multithreading as a poor programmer's substitute for async operations.

    You call the accept() method which blocks and asks the TCP protocol stack to listen, and only continues once a connection is made. At that point no client can connect until another thread is spawned to block and offer the endpoint again.

    So you have a server that "goes up and down like a crazy monkey." Not only do you have tremendous overhead jerking the TCP stack around, you have the massive overhead of gratuitious multithreading, ... and of course you will miss client connection attempts.

    This is why the java.async.io package came along as far as I know. Of course it has the disadvantage that in many cases it only can simulate async operation on feature-poor OSs and JVMs.

    http://en.wikipedia.org/wiki/Asynchronous_I/O
    In an environment such as a Java Virtual Machine (JVM), asynchronous I/O can be synthesized even though the environment the JVM is running in may not offer it at all. This is due to the interpreted nature of the JVM. The JVM may poll (or take an interrupt) periodically to institute an internal flow of control change, effecting the appearance of multiple simultaneous processes, at least some of which presumably exist in order to perform asynchronous I/O. (Of course, at the microscopic level the parallelism may be rather coarse and exhibit some non-ideal characteristics, but on the surface it will appear to be as desired.)

    So in the instance at hand the real question is probably why taking the "slow road" that goes all the way out to the network card connects more reliably than using the faster and more efficient loopback address. Perhaps this is due to extra allowances made that far down the protocol stack for possible slow physical networks? I'm not sure we'll find the answer to that.

  26. #26

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    6

    Re: localhost: 10061 Connection is forcefully rejected

    To clarify the programming languages:
    The client program is written in VBA; it is a Microsoft Access program. The server program is written in Java.
    I use the terms 'client' and 'server' only in the sense of the socket communication. Logically both programs are peers.

    DataMiser,
    thank you for the one-liner. It works perfectly.

    So for me the problem is solved (Anyway I would have known why I can't connect to the loop-back address 127.0.0.1).

    Thanks to all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width