Solved

problwm with tcp connect port 80

Posted on 2006-06-22
32
655 Views
Last Modified: 2013-11-15
Hello,
I am trying to coonect to a host using the port 80.
I use the foolowing method in code;

struct      sockaddr_in      server;

server.sin_family = AF_INET;
server.sin_port   = htons(PortNumber);
server.sin_addr.s_addr = inet_addr(ServerAddress);

connect(sockfd, (struct sockaddr *) &server, sizeof(server));

I get then 10061 if I use WSAGetLastError(); after that.

What is the problem do you think?
What can I do for that?

Thank you.

0
Comment
Question by:e116470
  • 10
  • 9
  • 5
  • +4
32 Comments
 

Author Comment

by:e116470
ID: 16957692

The PortNumber is 80
and ServerAddress is like www.webpage.com or its ip
in the code..
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16957713
It seems you are trying to connect with some internet site.
The IP addresses in the internet are not static.
So, you cannot connect with them directly.
You need to make a local connection with you service provider.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16957830
Check out this tutorial :

http://johnnie.jerrata.com/winsocktutorial/

Scroll down to "Making your own connections", and check out the example. (also read the extra information about gethostbyaddr() etc.)
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 53

Expert Comment

by:Infinity08
ID: 16957849
btw, the 10061 error means "connection refused". Either the host you specified is wrong (or didn't resolve to the correct IP), or the server is not listening on port 80, or a firewall is blocking your access.
0
 
LVL 24

Expert Comment

by:fridom
ID: 16958203
rajev your stuff is simply wrong. Of course you did not have to connect to you service provider first. The best tip IMHO has com from infinity08 checking a tutorial on socket programming. The shown code does not contain the needed information.
Have you called the proper initilization routines?
What is what you really want to achieve. Connecting to port 80 means connecting to some web server. So you really want to do something els.

You can checkout the sources of libpar and/or curl on how they do the connection. Or even better just use those libraries.

Regards
Friedrich
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16960944
>> rajev your stuff is simply wrong.
I didn't speak anything wrong.
Look at my post, I said "It seems you are trying to connect to some internet site".
In that case you cannot make socket connection simply as we do in a local network.
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 16962077
> Look at my post, I said "It seems you are trying to connect to some internet site".
> In that case you cannot make socket connection simply as we do in a local network.

Interesting, never knew that. I thought if you use tcp/ip, the routing will happen automatically. But I guess I have to update my knowledge about networking.
0
 
LVL 7

Expert Comment

by:Chatable
ID: 16963308
I have a few ideas about this:
#1 - Since 10061 means "Connection Refused" did you check that the host you're trying to connect to is actually listening on the designated port (try "telnet ServerAddress 80")?
#2 - If you are using a Windows machine, did you remember to call WSAStartup() before any socket operation?
#3 - inet_addr acceps only IP addresses, not host names. If you want to resolve a host name to an IP address, you need to use gethostbyname(). Actually if you're using a malformed string (anything that is not formatted XXX.XXX.XXX.XXX) as input for inet_addr it will return zero or some random number - which is definitely not the IP you need so it's very likely that that random IP does not have port 80 open, therefore resulting in "connection refused".
0
 
LVL 24

Expert Comment

by:fridom
ID: 16966469
Well rajev is simply wrong on the socket connection stuff. It works always the same way you try to get you hands on the correct internet address and then you connect to this IP Address. Maybe rajeev means something else, but as written this is not correct.

I suggest the following books (you can use them on Windows also you just ahve to be aware of some Microsoft extras)
Unix network programmng (stevens)
Tcp/IP Illustrated (stevens)

Regards
Friedrich

0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966634
>> Well rajev is simply wrong on the socket connection stuff.
I know, I am not wrong.
Let me ask you a question.
How you will make a socket connection with host www.yahoo.com in port 80 ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966731
>> How you will make a socket connection with host www.yahoo.com in port 80 ?
retrieve the host for www.yahoo.com (using gethostbyname() eg.), and just make the connection :

    WORD sockVersion = MAKEWORD(1, 1);
    WSADATA wsaData;
    WSAStartup(sockVersion, &wsaData);
    LPHOSTENT hostEntry = gethostbyname("www.yahoo.com");
    if (hostEntry) {
      SOCKET theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (theSocket != INVALID_SOCKET) {
        SOCKADDR_IN serverInfo;
        serverInfo.sin_family = AF_INET;
        serverInfo.sin_addr = *((LPIN_ADDR) *hostEntry->h_addr_list);
        serverInfo.sin_port = htons(80);
        if (connect(theSocket, (LPSOCKADDR) &serverInfo, sizeof(struct sockaddr)) != SOCKET_ERROR) {
          // Successfully connected! You can now send a request to the server !!
        }
      }
      closesocket(theSocket);
    }
    WSACleanup();

What do you think should be different ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966815
>> What do you think should be different ?
Will it really work if you have gateway in between ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966828
Do you think that you PC (or whatever) is in the same network where the www.yahoo.com is hosted ?

Actually your PC is in the network of ISP, not the internet
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966843
>> Actually your PC is in the network of ISP, not the internet
So ? The gateway is configured on OS level ... all traffic towards an internet address is routed through that gateway automatically (if your system is configured correctly), so opening a socket to port 80 on any internet web server will happen transparently ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966849
If you don't believe me, try it ...

The only problem you can have is if your internet access goes through a proxy, but that's rarely done these days.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966850
Have you tried that ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966853
Because it never works for me :)
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966858
So, I do it in some other way.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966878
>> Have you tried that ?
Of course, I do it all the time

>> Because it never works for me :)
What's your network setup ? What happens if you try this :

    telnet www.yahoo.com 80

and this :

    nslookup yahoo.com

and this :

    ping yahoo.com
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966882
>> So, I do it in some other way.
What way ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966901
Actually I have a proxy in between.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16966913
So, I connect with the proxy in port 80.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16966937
>> Actually I have a proxy in between.
:) As I said :) proxy's indeed require special treatment in the application.

If ping and telnet work, then you're not behind a proxy, and you can directly contact the web server from your application.
0
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
ID: 16967163
Well how about:

#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <resolv.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>



int main(void) {
  struct hostent *host;
  int sd;
  int i_rval;
  struct servent *srv;
  struct sockaddr_in addr;
  char * dotted_ip;
  host = gethostbyname("www.yahoo.com");
  dotted_ip = inet_ntoa(*(struct in_addr*)host->h_addr_list[0]);
  printf("IP Address of www.yahoo.com: %s\n", dotted_ip);
  srv = getservbyname("http", "tcp");
  printf("%s: port=%d\n", srv->s_name, ntohs(srv->s_port));
 
  memset(&addr, 0, sizeof(addr));    /* create & zero struct */
  addr.sin_family = AF_INET;    /* select internet protocol */
  addr.sin_port = srv->s_port;         /* set the port # */
  addr.sin_addr.s_addr = inet_addr(dotted_ip);
     /* set the addr */
  errno = 0;
  sd = socket(PF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    fprintf(stderr, "Error retrieving a socket, giving up\n");
    exit(EXIT_FAILURE);
  }
  i_rval = connect(sd, (struct sockaddr* )&addr, sizeof(addr));         /* connect! */
  if (i_rval < 0 ) {
    perror(NULL);
  }



  char s[200];
  FILE *fp;
  fp = fdopen(sd, "r+");         /* convert into stream */
  fprintf(fp, "GET / \r\n\r\n");      /* send request */
  fflush(fp);               /* ensure it got out */
  while ( fgets(s, sizeof(s), fp) != 0 )  /* while not EOF ...*/
    fputs(s, stdout);           /*... print the data */
  fclose(fp);
  return 0;
}


However I would use libcurl for the connection and fetching of the page I'm really interested in.
Regards
Friedrich
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 250 total points
ID: 16969605
Hi,

>> fprintf(fp, "GET / \r\n\r\n");

you need to specify HTTP version :
    fprintf(fp, "GET / HTTP/1.0\r\n\r\n");
or
    fprintf(fp, "GET / HTTP/1.1\r\n\r\n");
HTTP/0.9 doesn't specify version but is old (rarely still used) and only need a single \r\n
    fprintf(fp, "GET /\r\n");

If you are behind a proxy, then connect the proxy address instead, and use absolute URI:
    fprintf(fp, "GET http://www.yahoo.com/ HTTP/1.1\r\n\r\n");
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16969629
And don't forget that for HTTP/1.1, the Host header is required
0
 
LVL 24

Expert Comment

by:fridom
ID: 16970284
I suppose you are right about that, and in fact I had reall HTTP/1.0 specified. That was another version which however did has worked. Thanks for the hints nevertheless.

Regards
Friedrich
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17117969
This code is not valuable without my corrections  http:#16969605
And http:#16969629
0
 
LVL 24

Expert Comment

by:fridom
ID: 17120036
Well the only point I can make is that it simply has worked as posted. But of course doing it "right" is always better then just "doing" it ;-)

Regards
Friedrich
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question