Solved

problwm with tcp connect port 80

Posted on 2006-06-22
32
650 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
Have you tried that ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Because it never works for me :)
0
 
LVL 12

Expert Comment

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

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
>> So, I do it in some other way.
What way ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Actually I have a proxy in between.
0
 
LVL 12

Expert Comment

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

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
And don't forget that for HTTP/1.1, the Host header is required
0
 
LVL 24

Expert Comment

by:fridom
Comment Utility
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
Comment Utility
This code is not valuable without my corrections  http:#16969605
And http:#16969629
0
 
LVL 24

Expert Comment

by:fridom
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now