Problem using sockets

Problem using sockets

Post by Vlad Dogar » Mon, 15 Jan 2007 20:23:41


(originally posted to comp.lang.c, but was directed here since standard
C does not include support for sockets).

Hello,

I am trying to write a simple program to teach myself sockets. The
following bit of code fails with:
"connect: Socket operation on non-socket". What am I missing?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>

int main(int argc, char **argv)
{
int s1, s2; /* socket descriptors */
extern int errno;
struct sockaddr_in sa1, sa2;
char aux[MAX_STR_LEN];
struct hostent *hp;

if (argc != 5) {
printf("Usage: %s <host1> <port1> <host2> <port2>\n", argv[0]);
return 0;
}

if ((hp=gethostbyname(argv[1])) == 0) {
perror("gethostbyname");
return errno;
}

bzero(&sa1, sizeof sa1);
sa1.sin_family = AF_INET;
sa1.sin_port = htons(atoi(argv[2]));
sa1.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
perror("socket");
return errno;
}

if (connect(s1, (void *) &sa1, sizeof (sa1)) == -1) {
perror("connect");
return errno;
}
printf("Socket 1 connected");

Also, having two sockets open, can I use select() to read from them?
Can someone please point me to an example of using select() with
sockets?

Thanks in advance,
Vlad
 
 
 

Problem using sockets

Post by Bjn Augest » Mon, 15 Jan 2007 20:55:37


---------------------------------------------^
You have a bug here, don't you? ;-)

HTH
Bjn
[snip]

 
 
 

Problem using sockets

Post by loic-de » Mon, 15 Jan 2007 23:16:02

Hello,


Yes, looks like... a misplaced parenthesis... ;-)

Cheers,
Loic.
 
 
 

Problem using sockets

Post by Vlad Dogar » Tue, 16 Jan 2007 22:17:22


Wow, I guess that's what I get for using a small font. Thanks for the
tip -- I wasted half a day trying to figure out what went wrong.

Vlad
 
 
 

Problem using sockets

Post by Aggelos Mp » Fri, 19 Jan 2007 19:32:06

> Wow, I guess that's what I get for using a small font. Thanks for the


You 'll get used to it.
Classic anger management needed situation :P:P:P
 
 
 

Problem using sockets

Post by sam.barker » Sun, 30 Mar 2008 21:13:39

Hi,
I am trying to connect to a couple of serve[one after the other [in
case I cannot connect to one of them]

The code I am using is
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0)
{
printf("cannot bind port %u\n",SERVER_PORT);
perror("error ");
exit(1);
}

if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))
< 0) {
{
printf("cannot connect\n");
//exit(1);
return;
}

In some cases I can bind the port ,but I cannot connect.And the
program hangs there till it times out.How can can I avoid this?

Cheers,
Sam
 
 
 

Problem using sockets

Post by Rainer Tem » Mon, 31 Mar 2008 03:14:28


...
...

Binding a port to a local address is usually unnecessary. Exceptions
are those cases where the server program will only comunicate
with the client if the client uses a certain local port number (or
a certain range).

The connect call blocks until the connection is extablished or until
its clear that it has failed to connect. The only way around this
behaviour is to use the socket in nonblocking mode. In this case
connect() will return immediately with returncode -1 and errno set to
EINPROGRESS.

Hope this helps...Rainer
 
 
 

Problem using sockets

Post by Logan Sha » Mon, 31 Mar 2008 12:29:40


But you're still going to need a timeout. You send a packet to the
remote side, and it will reply back with a response eventually.
But how quickly? It depends on network latency, network congestion
(and dropout rates), and the load on the remote host. The only
thing a non-blocking connect() is going to get you is more control
over the timeout. Well, I guess it also gives you the ability
to try both connections simultaneously by doing two non-blocking
connect()s and then sharing one timeout for both of them.

- Logan
 
 
 

Problem using sockets

Post by Rainer Wei » Mon, 31 Mar 2008 21:46:49


XXXX@XXXXX.COM writes:

Only by not trying to connect. What you can do (as was hinted at in
the other two postings) is create two sockets, enable non-blocking
mode for both (=> fnctl(3)), call connect for both and then use 'a
synchronous I/O multiplexing call' (poll or select) to wait for one of
the sockets to become writable. As soon as it does, the connection has
been established and you can then use the established connection and
abandon the other attempt by closing the corresponding socket
descriptor.