WINSOCK - connection refused to localhost

Started by
5 comments, last by savail 11 years, 8 months ago
hey,
I was following this tutorial http://www.win32deve...tutorial_1.shtm and finally wanted to test whole 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;
}

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.
Advertisement
Is there some kind of server listening on the port you try to connect to (8888)?
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 ;/
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 Beej's Guide to Network Programming. To my knowledge, there still isn't anything better around for the bare bones.
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
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;

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).
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!

This topic is closed to new replies.

Advertisement