#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Socket programming in c on windows


    Host OS:windows 7
    Guest OS:Linux Ubuntu

    I am getting error in socket programming on windows.
    That error is: Connection() failed
    Error No. is :10061

    Where as I am able to connect from Guest to Host
    but reverse it is showing error.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    "Guest"? What's that? I know what a host is, but I suspect that that's different from what you think it is. The standard terminology is "client" and "server", wherein it's the server that creates a listening socket with the listen() function and it's the client that calls connect() to connect to the server. BTW, both the client and the server are called "hosts".

    So then, which is running under Windows 7, the client or the server? And which is running under Linux? The error that you gave us indicates that the client is running under Windows, which is the opposite of what I had assumed you had meant by your odd terminology. Please explain the situation using standard terminology.

    Also, please show us your code so that we can tell what you are doing. What you are expecting us to do here is to read your mind. We cannot do that, since in order to do the Vulcan mind-meld with you you have to be within arm's length of us. That would be a position you would not want to be in, because the first thing we'd do would be to try to slap some sense into you for trying to force us to play stupid guessing games with you.

    Please post the code. Use code tags! HTML strips out extra white space, including leading spaces and tabs. That means that HTML will completely destroy your code's formatting and render it unreadable. We have as much problems with unreadable code as we do with stupid guessing games.

    Here's how you do it:
    [code] copy-and-paste your formatted code here [/code]

    I've heard that there is a button in the advanced editor that does the same thing, but since I'm a long-time HTML geek, I just do it by hand.

    Here's a stupid question: have you tried the same thing on the same machine? Of course you'd have to address localhost (127.0.0.1). In other words, did you have a successfully running program on one system that you then ported without success to the other system?

    Originally Posted by dxr
    Where as I am able to connect from Guest to Host
    but reverse it is showing error.
    What the frak are you talking about? Clients connect to servers, not vice versa. At least not in the regular client/server model. You are obviously withholding absolutely essential information from us. That also will fail to endear you to us.

    The bottom line is that you want and need help and we should be able to provide that help and are willing to provide it (since we are here), but only if you will help us to help you. You need to show us what you are doing (that would be by providing properly formatted code listing along with other essential information). You need to tell us exactly what error indications you are getting (which you did do, a point in your favor).

    We get far too many totally inept first posts here. Yours is far better than the vast majority of those. Such as telling us us what systems are involved and exactly what error indication you got. Plus it is in clear English, which can be surprisingly rare here (I suspect that English is not your native language, since we've found that native English speakers are generally the worst at writing in English). But there are many things that you need to do to make your post better and actually capable of enabling us to help you. My purpose in being perhaps a bit harsh with you has been to inform you of what you need to do and why it is necessary (namely, I have described how you appear to us by not doing what is really necessary).

    Please help us to help you.

    BTW, it is nearly 2300h in my time zone. I will be logging off in a few minutes. I suspect that your time zone is nearly 12 hours different from mine. The time lag can be bothersome, but if you post the necessary information then others will also be able to step in and help you. Until you do, they will just look at your meager post and move on. You can count how many have done that.

    PS

    BTW, a bit over a decade ago I put together a series of web pages on TCP/IP and sockets programming. You can find it at http://pgm.dwise1.net/sockets/index.html. Hopefully in the meantime you can find something useful there.

    Ooh! Ooh! (obscure reference to an early 1970's USA sit-com, Welcome Back, Kotter!, where John Travolta got his big start). Could this maybe answer your question?: So Why do They Need to be on the Same Network?
    OK, that procedure works fine when the IP addresses have been set correctly, but what happens when an address is wrong? Specifically, what happens when a host that's physically connected to the local network is given an IP address that is not on the network?

    Let's get even more specific with what must be the most common mistake, the one that prompted me to write this section:

    With a cross-over Ethernet cable, you connect two PCs together. However, you set them to IP addresses that are not on the same network.
    So here's what happens in this case:
    The IP Layer determines that the destination IP address is not on the local network.
    NOTE: the IP Layer assumes that its own host's IP address is on the local network.
    The IP Layer commands the Data Link Layer to send the packet to the router.
    Assuming that an address was entered for the gateway router, the Data Link Layer tries to resolve the router's IP address.
    Since there is no router on this network, the ARP fails and the packet is never sent.
    The skinny is that you can never find that peer, even though you can plainly see that it is physically connected to your computer. Your host never even tries to look for it, because it already "knows" that it's not on the local network.
    This is a major "got'cha!" that beginners keep falling for. When you network two PCs together, or you add a new host to an existing network, make sure that its IP address is on the local network. And if the other hosts cannot find that new one, this should be the first thing you check.
    How are you connecting the two hosts -- this is the standard definition of "host" that I am using, which means "any computer that is on a network"? Through a hub? Or a wireless router? Or directly with a cross-over cable? Do you know the difference between a straight cable and a cross-over and where each type of cable is needed? What are their IP addresses? And the subnet mask?

    Please to follow that link, So Why do They Need to be on the Same Network?. It gets into the Address Resolution Protocol (ARP), which is an important concept to know. You address a host with an IP address, but the packets actually travel over Ethernet (usually) which knows absolutely nothing whatsoever about IP addresses. All Ethernet knows about are Machine Address Control (MAC) addresses, each one of which is unique for each particular Network Interface Card (NIC). So when you want to send an IP packet over Ethernet, you have to find out the MAC address for the host you are addressing. That's when ARP requests get broadcast over the local network asking literally "Who has this IP address?" (run a packet sniffer such as WireShark (free!) on your network and see for yourself). But that request only gets broadcast if the target address is on the local network, which is determined by the IP address and the subnet mask (read the details at Finally, How to Tell Whether Two Addresses are on the Same Network, though you may need to read through the preceding sections to be able to understand it). If the target IP address is not on the same local network, then the packet gets sent to the gateway router.

    So if the host (a computer connected to a network) is physically connected to the network but its IP address is not on the local network, then it cannot be accessed. You should check this also. Do please post the IP addresses and subnet masks involved; since they should be on a local private network, there shouldn't be any security concerns.
    Last edited by dwise1_aol; September 26th, 2013 at 02:25 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    ok.....here I am using linux as server and windows as client.
    when I am trying to connect by using socket programming ,
    it shows error as connection failed.
    and Error no is 10061.

    Below is snippet of code:

    if(connect(socket_send,(struct sockaddr*)&address,sizeof(address))<0)
    {
    printf("\nConnetion failed:-%d",WSAGetLastError());
    return 1;
    }
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Still tells us nothing.

    What are the IP addresses? Are they on the same network? Whether they are physically connected is absolutely meaningless. The network bits of the IP addresses need to match. We would also need to know the subnet mask for the local network.

    When you originally wrote the client app on Linux, did it run successfully? Did you not originally write the client app on Linux? I don't know, because I cannot read your mind!

    Where's the rest of the code? If we needed to test-run your code ourselves, we'd be completely out of luck and completely unable to help you! The same applies for all the warnings you could be getting and ignoring! If you insist on withholding valuable information that we need in order to help you, then we cannot help you! Why is that such an impossible thing to understand?

    And where the frak are the code tags????????

    PS

    If you don't know how to look up network information, go to the command line and enter a command: on Windows, that would be IPCONFIG /ALL and on Linux that would be ifconfig. Be sure to get both the IP address and the subnet mask.

    Also, verify that both the client and server programs are using the same protocol, either tcp or udp. A tcp client cannot talk to a udp server, nor a udp client to a tcp server. They both have to be either tcp or udp. I know that that's a stupid question, but I've seen beginners get tripped up by it. And I know that the minimal code fragment you posted looks like the client uses tcp, but without the rest of the code I can't tell for sure.
    Last edited by dwise1_aol; September 26th, 2013 at 03:32 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    ok.....here I am using linux as server and windows as client.
    when I am trying to connect by using socket programming ,
    it shows error as connection failed.
    and Error no is 10061.
    You may want to use the following code snippet to get additional info on your error condition...

    Code:
    int errCode = WSAGetLastError();
    
        LPSTR errString = NULL;  
      
        int size = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                     FORMAT_MESSAGE_FROM_SYSTEM, 
                     0,       // 0 since source is internal message table
                     errCode,                           
                     0,       
                     (LPSTR)&errString, 
                     0,                 
                     0 );               
         printf( "WSA_Error code %d:  %s\n\n", errCode, errString ) ;
         //LocalFree has to be called because we use FORMAT_MESSAGE_ALLOCATE_BUFFER
         LocalFree( errString ) ;
    In your case, the details are as follows...

    Error code 10061: No connection could be made because the target machine actively refused it.
    MSDN provides info on this error at WSA error 10061

    This may also be a firewall issue. Is there a firewall running on the Linux box? If so, is the port being used by your server in the range of ports locked down by the firewall?

    Also, verify that your server is running on the Linux box by executing the netstat -a command. This will display your server name and the port that it is using if the server is active. The 10061 error can also be thrown if the server is not running or possibly in some questionable state due to poor programming.

    Finally, what Windows OS are you using?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by BobS0327
    You may want to use the following code snippet to get additional info on your error condition...

    Code:
    int errCode = WSAGetLastError();
    
        LPSTR errString = NULL;  
      
        int size = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                     FORMAT_MESSAGE_FROM_SYSTEM, 
                     0,       // 0 since source is internal message table
                     errCode,                           
                     0,       
                     (LPSTR)&errString, 
                     0,                 
                     0 );               
         printf( "WSA_Error code %d:  %s\n\n", errCode, errString ) ;
         //LocalFree has to be called because we use FORMAT_MESSAGE_ALLOCATE_BUFFER
         LocalFree( errString ) ;
    In your case, the details are as follows...



    MSDN provides info on this error at WSA error 10061

    This may also be a firewall issue. Is there a firewall running on the Linux box? If so, is the port being used by your server in the range of ports locked down by the firewall?

    Also, verify that your server is running on the Linux box by executing the netstat -a command. This will display your server name and the port that it is using if the server is active. The 10061 error can also be thrown if the server is not running or possibly in some questionable state due to poor programming.

    Finally, what Windows OS are you using?
    I already disabled firewall on LINUX.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    yes it's executed netstat -a command and firewall is disabled.
    I am using Windows 7 OS.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    So are both computers on the same network? This is the third time I've had to ask you this question. You need to look at both computers' IP addresses and the network subnet to determine that. If you would provide us with that information, as I've already requested repeatedly, then we could determine that for you.

    You absolutely refuse to provide us with any useful information. You obviously do not want us to help you.


    PS
    Until you give us useful information, we cannot help you.

    The fact as to whether you had run both client and server on the same system successfully and then ported on one over to the other system would help immensely.

    Also, the source code would contain a wealth of useful information for us, including enable us to test the code for ourselves to reveal even more useful information.

    Keeping such information from us only serves to prevent us from helping you. If you are in a hurry to solve this problem, then why are you wasting valuable time?
    Last edited by dwise1_aol; September 30th, 2013 at 04:08 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0
    Sorry for that,
    but yesterday only I had solve this problem, now it's working properly.
    The problem was in port, just need to enable that port.
    Another thing,here I am using only one computer instead of using two different computer.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally Posted by dxr
    The problem was in port, just need to enable that port.
    Whatever that is supposed to mean. Even your solutions are devoid of useful information.

    A lot of people find our messages here when they search for solutions to their own problems. Somebody with the same problem as you had could very well find this thread, read through it, and walk away having learned nothing. You are doing that person a disservice.

IMN logo majestic logo threadwatch logo seochat tools logo