LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   recv command in sock in c (https://www.linuxquestions.org/questions/programming-9/recv-command-in-sock-in-c-526291/)

mohtasham1983 02-07-2007 12:46 AM

recv command in sock in c
 
I guess I have made connection to a day time server successfully, but I don't know how I can receive day time from it and where I can find a day time server on the internet.

Code:

#include <sys/socket.h>
#include <sys/types.h>
#include <iostream>
#include <sys/time.h>       
#include <time.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
using namespace std;
#include <arpa/inet.h>
int main(int argc, char **argv)
{
        int    sockfd;
        struct sockaddr_in serv_addr;
        int bufsize=1024;        /* a 1K buffer */
        char str[INET_ADDRSTRLEN];
        char *buffer=malloc(bufsize);
//  struct hostent *server;
//  if (argc != 2)
//          err_quit("usage: a.out <IPaddress>");
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        bzero(&serv_addr, sizeof(serv_addr));//convert dest address to all zeros
        if (inet_pton(AF_INET, "72.14.207.99",&serv_addr.sin_addr)<0)
        {
                cout <<"Hostname is not valid"<< endl;
                exit(0);
        }
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(13);
        inet_ntop(AF_INET, &serv_addr.sin_addr, str, sizeof(str));
        if (connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) != 0)
            cout << "You are connected to " << str<< endl;
 
          recv(sockfd,buffer,bufsize,0);
    close(sockfd);
    cout <<"Connection is terminated"<< endl;
        exit(0);
}

When I compile this program I get the following error: invalid conversion from void* to char* in this line:
Code:

char *buffer=malloc(bufsize);
Moreover, I am not sure where I can display the result I will get from the day time server.

Any idea what's wrong with my code?

m0rg 02-07-2007 03:09 AM

char* buffer=(char*)malloc(size);

this should fix the problem, malloc returns void* so you have to cast the result.

mohtasham1983 02-07-2007 04:17 AM

thanks a lot. It s working fine now


All times are GMT -5. The time now is 02:57 PM.