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.

  • Index
  • » C++
  • » C++ Socket Program, Connect function

#1 2008-11-16 03:26 PM

SmithPam
Member
Registered: 2008-11-15
Posts: 2

Re: C++ Socket Program, Connect function

Hello All,

I am trying to connect LINUX server from my windows machine through SSH and trying to use 'connect (int s, (struct sockaddr *)&sin, sizeof(sin))' .
and it returns -1. If I try to use the same program from LINUX machine directly, it works fine.

Can anybody give an idea how to resolve it to make it worked from my windows machine(through SSH)?

Thanks..

Offline

#2 2008-11-16 05:03 PM

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

Re: C++ Socket Program, Connect function

One possibility is that the sin structure isn't being properly filled out.  You might do a memset(&sin, 0, sizeof(sin)) at the beginning just to make sure it doesn't have any garbage bytes in it.

-Jeremy

Offline

#3 2008-11-16 08:02 PM

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

Re: C++ Socket Program, Connect function

What do you mean by "through SSH"??  Do you have an SSH tunnel setup, and
your code is doing a connect() to some port on localhost, which is tunnelled over
SSH to the real remote host?

What is the specific error when connect() fails?  On Unix, you'd print out errno or do
perror() or something; I'm not sure about Windoze, but I think there it's something
different (WSAGetLastError()?)...

If that doesn't point out the problem to you, then post your code...  Otherwise, the best
we can possibly do is guess blindly at what you're doing wrong...

Offline

#4 2008-11-16 08:59 PM

SmithPam
Member
Registered: 2008-11-15
Posts: 2

Re: C++ Socket Program, Connect function

Thank you guys for your reply. Its really urgent for me.

here is my code.

char *host = "[ipaddress of the server]", *service = "[port number]", *protocol = "tcp";

struct hostent	*phe; 
	struct servent	*pse;
	struct protoent	*ppe;
	struct sockaddr_in 	sin;
	int s, type;
	int i = 0;
	bzero((char *)&sin, sizeof (sin));
	sin.sin_family = AF_INET;
	if (pse = getservbyname (service, protocol) )
		sin.sin_port = pse ->s_port;
	else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0)
		printf ("can’t get %s service\n", service);

	if (phe = gethostbyname (host) )
		bcopy(&sin.sin_addr, phe->h_addr, phe->h_length);
	else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
		printf("can’t get %s host\n", host);
	if ( (ppe = getprotobyname (protocol)) == 0)
		printf ("can’t get %s host\n",host);
	if (strcmp (protocol, "tcp") == 0)   
		type = SOCK_STREAM;
	else   	type = SOCK_DGRAM;
	if( (s = socket (PF_INET, type, ppe->p_proto)) < 0) 
		printf ("Can&#8217;t create a socket: %d\n", strerror(errno));
	int c = connect (s, (struct sockaddr *)&sin, sizeof(sin));
	if (c < 0)
		printf ("can&#8217;t connect to remote server: %d\n",strerror(errno));


My machine has windows and my server runs on Linux and my program is in C++ (UNIX). I connect another Linux machine(different then the server) through SSH.

I hope this information helps. Let me know if you still need some information. Can you tell me what is the function to display the error because I am getting error number '1076041180' by using 'strerror(int errno)' and I can't figure out what the problem is.

Thanks ..

Offline

#5 2008-11-17 03:08 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: C++ Socket Program, Connect function

Compile with warnings enabled (-Wall -W)...

Use %s to print out the error, not %d.

Are you saying that when you run your program on the server without SSH,
it works, but it doesn't when you run it via SSH? That would be strange.
You could run the program with strace to see what's different between the two
runs.

Offline

#6 2008-11-17 01:00 PM

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

Re: C++ Socket Program, Connect function

At a guess, I'm going to say the errno is likely ECONNREFUSED or EHOSTUNREACH
or something along those lines...  Because of this bug in your code:

bcopy(&sin.sin_addr, phe->h_addr, phe->h_length);

You're treating bcopy() as if it were memcpy()...  When, in fact, the source and
destination buffers are completely reversed between the two...  So, the above is
copying FROM your sockaddr_in INTO the hostent...  Not what you want at all...
Either reverse your args, or better yet just use standard memcpy() and memset(),
and get rid of the stupid old bcopy() and bzero() crap...  (Why that nonsense even
exists anymore, I have no idea...)

Offline

  • Index
  • » C++
  • » C++ Socket Program, Connect function

Board footer

Powered by FluxBB