Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 666
  • Last Modified:

problwm with tcp connect port 80

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
e116470
Asked:
e116470
  • 10
  • 9
  • 5
  • +4
2 Solutions
 
e116470Author Commented:

The PortNumber is 80
and ServerAddress is like www.webpage.com or its ip
in the code..
0
 
rajeev_devinCommented:
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
 
Infinity08Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Infinity08Commented:
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
 
fridomCEO/ProgrammerCommented:
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
 
rajeev_devinCommented:
>> 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
 
cwwkieCommented:
> 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
 
ChatableCommented:
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
 
fridomCEO/ProgrammerCommented:
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
 
rajeev_devinCommented:
>> 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
 
Infinity08Commented:
>> 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
 
rajeev_devinCommented:
>> What do you think should be different ?
Will it really work if you have gateway in between ?
0
 
rajeev_devinCommented:
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
 
Infinity08Commented:
>> 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
 
Infinity08Commented:
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
 
rajeev_devinCommented:
Have you tried that ?
0
 
rajeev_devinCommented:
Because it never works for me :)
0
 
rajeev_devinCommented:
So, I do it in some other way.
0
 
Infinity08Commented:
>> 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
 
Infinity08Commented:
>> So, I do it in some other way.
What way ?
0
 
rajeev_devinCommented:
Actually I have a proxy in between.
0
 
rajeev_devinCommented:
So, I connect with the proxy in port 80.
0
 
Infinity08Commented:
>> 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
 
fridomCEO/ProgrammerCommented:
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
 
WebstormCommented:
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
 
Infinity08Commented:
And don't forget that for HTTP/1.1, the Host header is required
0
 
fridomCEO/ProgrammerCommented:
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
 
WebstormCommented:
This code is not valuable without my corrections  http:#16969605
And http:#16969629
0
 
fridomCEO/ProgrammerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 9
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now