Jump to content

  • Log In with Google      Sign In   
  • Create Account


WINSOCK - connection refused to localhost


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 savail   Members   -  Reputation: 333

Like
0Likes
Like

Posted 14 August 2012 - 03:45 AM

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.

Edited by savail, 14 August 2012 - 03:49 AM.


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3894

Like
0Likes
Like

Posted 14 August 2012 - 03:52 AM

Is there some kind of server listening on the port you try to connect to (8888)?

#3 savail   Members   -  Reputation: 333

Like
0Likes
Like

Posted 14 August 2012 - 03:58 AM

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 ;/

#4 BitMaster   Crossbones+   -  Reputation: 3894

Like
1Likes
Like

Posted 14 August 2012 - 04:05 AM

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.

Edited by BitMaster, 14 August 2012 - 04:10 AM.


#5 savail   Members   -  Reputation: 333

Like
-1Likes
Like

Posted 14 August 2012 - 04:11 AM

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

#6 DarkRonin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 15 August 2012 - 04:38 AM

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.


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, 15 August 2012 - 04:39 AM.


#7 savail   Members   -  Reputation: 333

Like
0Likes
Like

Posted 16 August 2012 - 12:05 PM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS