Sign in to follow this  

Sockets Hate Me

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know you're thinking "quit your whining." But they really do. At this point, I'm just trying to get two computers on the network to notice eachother. The server code *seems* to be working ok, but the client cannot find it. The server code returns no errors. The Client code keeps returning the "Server Refused Connection Error" (10061). Any help would be greatly appreciated. #define HOST_IP "192.168.1.101" //this is the Host's Internal IP address. //I dunno if using an interal address might have something //to do with my problems //Client Code void ClientThread(void* p) { SocketInfo* si; char str[200]; int ErrorCode; si = (SocketInfo*)p; //Get a Socket si->m_Socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sprintf(str, "Socket = %d", si->m_Socket); //AddMessageToSocketInfo is a printing function AddMessageToSocketInfo(si, str); //Set up the socket to connect to the Server ZeroMemory(&si->ServerAddress, sizeof(si->ServerAddress)); si->ServerAddress.sin_family = AF_INET; si->ServerAddress.sin_addr.S_un.S_addr = inet_addr(HOST_IP); si->ServerAddress.sin_port = htons(MY_PORT); do{ //Connect to the Server sprintf(str, "Attempting to Connect to %s", HOST_IP); AddMessageToSocketInfo(si, str); ErrorCode = connect(si->m_Socket, (sockaddr*)&si->ServerAddress, sizeof(si->ServerAddress)); sprintf(str, "connect() ErrorCode = %d", ErrorCode); AddMessageToSocketInfo(si, str); ErrorCode = WSAGetLastError(); sprintf(str, "WSA: %d", ErrorCode); AddMessageToSocketInfo(si, str); }while(ErrorCode!=0); //Loop until a connection has been established } //ServerCode void HostThread(void* p) { int ErrorCode; char str[200]; SocketInfo* si; si = (SocketInfo*) p; si->m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sprintf(str, "Socket = %d", si->m_Socket); AddMessageToSocketInfo(si, str); memset(&si->ServerAddress, 0, sizeof(si->ServerAddress)); si->ServerAddress.sin_family = AF_INET; si->ServerAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY); si->ServerAddress.sin_port = MY_PORT; sprintf(str, "Attempting to connect socket to local address"); AddMessageToSocketInfo(si, str); //Bind ErrorCode = bind(si->m_Socket, (sockaddr*)&si->ServerAddress, sizeof(sockaddr)); sprintf(str, "Error Code: %d", ErrorCode); AddMessageToSocketInfo(si, str); ErrorCode = WSAGetLastError(); sprintf(str, "WSA: %d", ErrorCode); AddMessageToSocketInfo(si, str); //Listen ErrorCode = listen(si->m_Socket, MAX_PENDING); sprintf(str, "listen() Error Code: %d", ErrorCode); AddMessageToSocketInfo(si, str); ErrorCode = WSAGetLastError(); sprintf(str, "WSA: %d", ErrorCode); AddMessageToSocketInfo(si, str); //Accept sockaddr_in ClientSocket; int len = sizeof(ClientSocket); ErrorCode = accept(si->m_Socket, (sockaddr*)&ClientSocket, &len); sprintf(str, "accept() ErrorCode = %d", ErrorCode); AddMessageToSocketInfo(si, str); ErrorCode = WSAGetLastError(); sprintf(str, "WSA: %d", ErrorCode); AddMessageToSocketInfo(si, str); }

Share this post


Link to post
Share on other sites
Its probably nothing, but in my socket code I use sockaddr_in::sin_addr.s_addr = htonl(address);
I.e. not the S_un.S_addr member.

Are the client and the server on the same machine? (I guess not since the IP isn't 127.0.0.1)
What port are you using? Try one > 1024 (windows can be fussy sometimes)
Do you have a firewall? Such as the one windows XP comes with? Connection Refused means that the server isn't running on the target machine (or the target address is wrong and its looking in the wrong place), or the packets didn't reach it (because its firewalled).

Share this post


Link to post
Share on other sites
Quote:
Original post by DJohnson


//CLIENT
ZeroMemory(&si->ServerAddress, sizeof(si->ServerAddress));
si->ServerAddress.sin_family = AF_INET;
si->ServerAddress.sin_addr.S_un.S_addr = inet_addr(HOST_IP);
*****>si->ServerAddress.sin_port = htons(MY_PORT);


//Server
memset(&si->ServerAddress, 0, sizeof(si->ServerAddress));
si->ServerAddress.sin_family = AF_INET;
si->ServerAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
*****>si->ServerAddress.sin_port = MY_PORT;

}


Bingo,there seems to be your problem.You are not using htons on both the server and clinet sides.If you dont do that then there are going to be problems.

I had this trouble when I was coding my first chat server.I did not use htons() at all.The need to use htons is that computers store number in a diffrent fasion at the hardware level,and so will be diffrent on some comps.htons() helps convert the port number into a proper one which can be compatible between diffrent comps.

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this