Thread: why cant i get a responce from the server?

  1. #1
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33

    why cant i get a responce from the server?

    i cant get response from server program just stalls.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <iostream>
    using namespace std;
    
    #define PORT 80 
    #define MAXDATASIZE 100 // max number of bytes we can get at once 
    
        int main(int argc, char *argv[])
        {
            int sockfd, numbytes;  
            char buf[MAXDATASIZE];
            struct hostent *he;
            struct sockaddr_in their_addr; // connector's address information 
    		
    		if (argc != 2) {
                fprintf(stderr,"usage: client hostname\n");
                exit(1);
            }
    		
    		
            
     		he=gethostbyname(argv[1]);
    		
    		cout<<"Host name  : "<<he->h_name<<endl;
            cout<<"IP Address : "<<inet_ntoa(*((struct in_addr *)he->h_addr))<<endl;
    		
            sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
            their_addr.sin_family = AF_INET;    // host byte order 
            their_addr.sin_port = htons(PORT);  // short, network byte order 
            their_addr.sin_addr = *((struct in_addr *)he->h_addr);
            memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct 
    		
    		if (connect(sockfd, (struct sockaddr *)&their_addr,
                                                  sizeof(struct sockaddr)) == -1) {
                perror("connect");
                exit(1);
            }
            
    		
    		char *msg = "GET	/";
    		int len;
    		
    		len = strlen(msg);
    		
    		if (send(sockfd, msg, len, 0) == -1)
                        perror("send");
    		
    		
    		if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
                perror("recv");
                exit(1);
            }
    
            buf[numbytes] = '\0';
    
            cout<<"Received: "<<buf;
    
            close(sockfd);
    
            return 0;
        }
    is there something wrong with the code?

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Try finding an FTP (port 21) or SMTP (port 25) and connect to that, and just try to read the input they send once you connect. The problem with http servers is that they normally expect a large ammount of text to be sent and your probably just not giving it enough information (A good search on the net will show you what needs to be sent) so its stalled waiting.

    If you can connect to these other servers and they send you information, then its probably just a lack of information bieng sent to the http server. Otherwise, if the FTP and SMTP servers arent working either, then theres probably a problem in your code.

    203.43.167.172 is the IP of my school, which runs both an FTP server and an SMTP server if you don't know any of your own, they wont mind.

    ~ Paul

    Edit: I once set up a server to run on port 80 to listen to a request from internet explorer, and it was sent at least 10-15 lines worth or information

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    End your GET request with '\n'
    Like "GET /\n"

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    lol or do that ^

  5. #5
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    thx every one \n solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  3. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM