September 26th, 2013, 12:27 AM
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.
September 26th, 2013, 12:59 AM
"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?
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.
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?
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 01:25 AM.
September 26th, 2013, 06:37 AM
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:
September 26th, 2013, 09:49 AM
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????????
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 02:32 PM.
September 26th, 2013, 10:31 PM
You may want to use the following code snippet to get additional info on your error condition...
In your case, the details are as follows...
int errCode = WSAGetLastError();
LPSTR errString = NULL;
int size = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
0, // 0 since source is internal message table
printf( "WSA_Error code %d: %s\n\n", errCode, errString ) ;
//LocalFree has to be called because we use FORMAT_MESSAGE_ALLOCATE_BUFFER
LocalFree( errString ) ;
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?
September 30th, 2013, 06:57 AM
I already disabled firewall on LINUX.
Originally Posted by BobS0327
September 30th, 2013, 07:03 AM
yes it's executed netstat -a command and firewall is disabled.
I am using Windows 7 OS.
September 30th, 2013, 09:26 AM
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.
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 03:08 PM.
October 1st, 2013, 02:02 AM
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.
October 1st, 2013, 09:33 AM
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.