LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2007, 12:46 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
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?
 
Old 02-07-2007, 03:09 AM   #2
m0rg
Member
 
Registered: Jun 2003
Location: France
Distribution: slackware 11.0
Posts: 80

Rep: Reputation: 15
char* buffer=(char*)malloc(size);

this should fix the problem, malloc returns void* so you have to cast the result.
 
Old 02-07-2007, 04:17 AM   #3
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
thanks a lot. It s working fine now
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pthreads and recv sayarsoft Linux - Kernel 2 08-11-2006 04:06 AM
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
Can't recv() Ephracis Programming 2 01-04-2005 02:49 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM
recv() buffer linuxanswer Programming 1 03-22-2004 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration