UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-08-06 04:41 PM

sunilchauhan
Member
Registered: 2008-08-04
Posts: 4

Re: socket programming

hi
i'm trying to develop a simple downloader using socket programming
i'm able to connect to the local host using my client but if try to connect some other
IPadress or hostname it gives "network is unreachable"(on call to connect)
please help
thanks in advance

Offline

#2 2008-08-06 05:33 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: socket programming

Offline

#3 2008-08-06 07:58 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: socket programming

It might be a bug with filling in the remote IP...  But, if you're using the same logic
for filling in localhost and it works, then I'd say instead it's likely your routing is
screwed up or something...  Can you connect to remote IPs from that same host via
other standard apps (telnet, ssh, etc.)?  Specifically, if you can, try telnet to the same
port you're trying to connect to with your app...  If you can connect to other ports,
but not that one, then it's probably firewall rules somewhere between your endpoints
(maybe on your local host)...  If you can't connect to any port on an external IP, I'd
say your routing must be misconfigured...  If you can connect fine with telnet but not
your app, then it's got to be a bug in your app, and like jfriesne says, we'll need to
see your source to find it...

Offline

#4 2008-08-07 07:29 PM

sunilchauhan
Member
Registered: 2008-08-04
Posts: 4

Re: socket programming

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<string.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
int main()
	{  
	int sock,new;
	char url[100];
	struct sockaddr_in server,client;
	struct hostent *h;
	int len=sizeof(struct sockaddr);
	char msg[50]="first tcp sockets programme\n";
	char *p;
	if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
	   { 
	   perror("socket:");
	   exit(-1);
	   }
   		
	while(1)
	 {  printf("please enter a  the URL of the site to connect address::");
	fgets(url,100,stdin);
 	p=strchr(url,'\n');
	*p='\0';//to remove the excess '\n' stored in url buffer	


	  if ((strcmp(url,"exit"))==0)
	  {
		printf("\nthanks for using this service");
		printf("\napplication closing \n");
		exit(-1);
	  }  
	  //preparing to connect 
          client.sin_family=AF_INET;
 	  client.sin_port  =htons(2368);
	  h=gethostbyname(url);	  
	  client.sin_addr.s_addr=inet_addr(h->h_addr_list[0]  );
	  if (-1==(connect(sock,(struct sockaddr *)&client,sizeof(client))));
             {		
		printf("ip address is %s",inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
		perror("connect");
		exit(-1);
	     }
	
	  //printf("we will continue");
	  printf("ip address %s",inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
	  //send(sock,msg,50,0 );
 
	 }
	return 0;
	}

Offline

#5 2008-08-07 08:41 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: socket programming

Offline

#6 2008-08-15 12:48 PM

sunilchauhan
Member
Registered: 2008-08-04
Posts: 4

Re: socket programming

thanks for your help

Offline

#7 2009-01-28 03:00 AM

dmitriysorokin
Guest

Re: socket programming

Thanks for this information.
Do you have any suggestions as which one to be used for media files?

Board footer

Powered by FluxBB