• Advertisement
Sign in to follow this  

WINSOCK - connection refused to localhost

This topic is 1981 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

hey,
I was following this tutorial [url="http://www.win32developer.com/tutorial/winsock/winsock_tutorial_1.shtm"]http://www.win32deve...tutorial_1.shtm[/url] and finally wanted to test whole code:
[CODE]
#include <iostream>
#include <winsock2.h>
#include <errno.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
// Initialise Winsock
WSADATA WsaDat;
if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
{
std::cout<<"Winsock error - Winsock initialization failed\r\n";
WSACleanup();
system("PAUSE");
return 0;
}

// Create our socket
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
std::cout<<"Winsock error - Socket creation Failed!\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Resolve IP address for hostname
struct hostent *host;
if((host = gethostbyname("localhost"))==NULL)
{
std::cout<<"Failed to resolve hostname.\r\n";
WSACleanup();
system("PAUSE");
return 0;
}
// Setup our socket address structure
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(8888);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
// Attempt to connect to server
if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) == -1)
{
std::cout<<"Failed to establish connection with server\r\n"<<WSAGetLastError();
WSACleanup();
system("PAUSE");
return 0;
}
// Display message from server
char buffer[1000];
memset(buffer,0,999);
int inDataLength=recv(Socket,buffer,1000,0);
std::cout<<buffer;
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
// Cleanup Winsock
WSACleanup();
system("PAUSE");
return 0;
}
[/CODE]
However, I'm getting error 10061 all the time, which means connection refused. I'm totally confused with this winsock. All tutorials are either obsolete, only unix or incomplete... I tried switching off my firewall and anti vir but that didn't help. Does anyone know why can't I connect to my localhost? I would be really garteful for help! And please don't hesitate to provide any advice as I'm really totally inexperienced with network programming. Edited by savail

Share this post


Link to post
Share on other sites
Advertisement
I've got no idea. I don't understand even if the port can be random or how to select a port to put there. Whole this code is from a tutorial and it's said there that I should be able to connect to localhost with it ;/

Share this post


Link to post
Share on other sites
If there is no program listening on that port, then that error code is expected. Either you need to write a server which listens on 8888 and have it running while starting the client or you need to find some program that listens on a port and sends a text string to any connecting client.

Edit: Back when I learned about networking I used [url=http://beej.us/guide/bgnet/output/html/multipage/index.html]Beej's Guide to Network Programming[/url]. To my knowledge, there still isn't anything better around for the bare bones. Edited by BitMaster

Share this post


Link to post
Share on other sites
ah true. Next lesson in thsi tut was creating a server... Though there was nothing said that in order to connect to localhost you need to create a server first and run it. For some really new users it might not be so obvious. Thanks for your help

Share this post


Link to post
Share on other sites
Hi savail, I am the author of the site.

Appologies, I should have been clearer.

Although, it does state on the first line of the tutorial;

[quote]This tutorial is to designed to get a feel for the most basic Winsock model, the blocking socket. By the end you will have a working client. The server can be found in Tutorial 2.[/quote]

But, yes, a client will always need a server to connect to. I am glad you have realised that tutorial 2 is the compliment of tutorial 1 (As is the trend through out the rest of the site). Edited by lonewolff

Share this post


Link to post
Share on other sites
hey, sorry I reply so late. I should have been more careful, didn't notice that line while reading it for a first time. Anyway your site has helped me a lot. It contains one of most recent tutorials on winsock. I have read plenty of tutorials before this on your site, but your has helped me most in understanding how TCP works. Good job with those tutorials ^^. Hope there will be more of them!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement