For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > August 2005 > connect...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author connect...
Daniel W.

2005-08-28, 6:57 pm

Hello,

can anybody tell me why in this source connect say that a connect is
possible while it is sure not!

I've done a calculation error with the port and I've spend hours to find
out why I got a SIGPIPE signal in my program.

It's the same if the socket is blocking or non-blocking.

greetings,
daniel


int c_rv = connect(pCli->sockPort, (struct
sockaddr*)&pCli->sockaddr_in_port, sizeof(struct sockaddr_in));
if( c_rv<0 ) {
int n;
struct timeval tv={3,0};
fd_set fd;
FD_ZERO(&fd);
FD_SET(pCli->sockPort, &fd);
n=select(pCli->sockPort+1, 0,&fd,0, &tv);
if( n!=1 ) {
TRACE(" - CONNECT TIMEOUT\n");
close(pCli->sockPort);
goto end;
}
TRACE(" - port connected\n");
rv = 1;
}
else if( c_rv == 0 ) {
rv = 1;
TRACE(" - port connected\n");
}
else {
TRACE("connect port FAIL, err %s\n", strerror(errno));
}

end:
return rv;
Barry Margolin

2005-08-28, 9:56 pm

In article <43122958$0$27164$91cee783@newsreader01.highway.telekom.at>,
"Daniel W." <daw...@aon.at> wrote:

> Hello,
>
> can anybody tell me why in this source connect say that a connect is
> possible while it is sure not!
>
> I've done a calculation error with the port and I've spend hours to find
> out why I got a SIGPIPE signal in my program.


Maybe there's another server listening on the mistaken port you're
connecting to.

>
> It's the same if the socket is blocking or non-blocking.
>
> greetings,
> daniel
>
>
> int c_rv = connect(pCli->sockPort, (struct
> sockaddr*)&pCli->sockaddr_in_port, sizeof(struct sockaddr_in));
> if( c_rv<0 ) {
> int n;
> struct timeval tv={3,0};
> fd_set fd;
> FD_ZERO(&fd);
> FD_SET(pCli->sockPort, &fd);
> n=select(pCli->sockPort+1, 0,&fd,0, &tv);
> if( n!=1 ) {
> TRACE(" - CONNECT TIMEOUT\n");
> close(pCli->sockPort);
> goto end;
> }
> TRACE(" - port connected\n");
> rv = 1;
> }
> else if( c_rv == 0 ) {
> rv = 1;
> TRACE(" - port connected\n");
> }
> else {
> TRACE("connect port FAIL, err %s\n", strerror(errno));
> }
>
> end:
> return rv;


--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Nils O. Selåsdal

2005-08-29, 3:56 am

Daniel W. wrote:
> Hello,
>
> can anybody tell me why in this source connect say that a connect is
> possible while it is sure not!
>
> I've done a calculation error with the port and I've spend hours to find
> out why I got a SIGPIPE signal in my program.
>
> It's the same if the socket is blocking or non-blocking.
>
> greetings,
> daniel
>
>
> int c_rv = connect(pCli->sockPort, (struct
> sockaddr*)&pCli->sockaddr_in_port, sizeof(struct sockaddr_in));
> if( c_rv<0 ) {
> int n;
> struct timeval tv={3,0};
> fd_set fd;
> FD_ZERO(&fd);
> FD_SET(pCli->sockPort, &fd);
> n=select(pCli->sockPort+1, 0,&fd,0, &tv);
> if( n!=1 ) {
> TRACE(" - CONNECT TIMEOUT\n");
> close(pCli->sockPort);
> goto end;
> }

Don't assume it is a timeout just because n != 1.
Check for errors, as always.
Nils O. Selåsdal

2005-08-29, 3:56 am

Nils O. Selåsdal wrote:
> Daniel W. wrote:
>
>
> Don't assume it is a timeout just because n != 1.
> Check for errors, as always.


Oh, it would help quite a bit if you pinpoint where your code received
the SIGPIPE, use a debugger.
(Mind you , it is pretty useless to try to help by this
little snippet, your datatypes and initialization part is missing.
)
Villy Kruse

2005-08-29, 3:56 am

On Mon, 29 Aug 2005 09:08:16 +0200,
Nils O. Selåsdal <NOS@Utel.no> wrote:


> Don't assume it is a timeout just because n != 1.
> Check for errors, as always.


Also, don't assume that when connect returns an error that connect is
in progress.


Villy
Maxim Yegorushkin

2005-08-29, 7:56 am


Daniel W. wrote:
> Hello,
>
> can anybody tell me why in this source connect say that a connect is
> possible while it is sure not!


It does not say so. You don't check errors correctly. A socket is
marked as ready for read/write if it has an outstanding error.

> I've done a calculation error with the port and I've spend hours to find
> out why I got a SIGPIPE signal in my program.


This is because select() tells you that the socket is ready for write
not because it has connected, rather because it has an outstanding
error.

> int c_rv = connect(pCli->sockPort, (struct
> sockaddr*)&pCli->sockaddr_in_port, sizeof(struct sockaddr_in));
> if( c_rv<0 ) {


Replace the check with if( c_rv<0 && EINPROGRESS == errno )

> int n;
> struct timeval tv={3,0};
> fd_set fd;
> FD_ZERO(&fd);
> FD_SET(pCli->sockPort, &fd);
> n=select(pCli->sockPort+1, 0,&fd,0, &tv);
> if( n!=1 ) {
> TRACE(" - CONNECT TIMEOUT\n");
> close(pCli->sockPort);
> goto end;
> }


You need a check here:

int err;
socklen_t err_len = sizeof err;
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len)
|| err_len != sizeof err
|| err
)
// handle connect error

> TRACE(" - port connected\n");
> rv = 1;
> }
> else if( c_rv == 0 ) {
> rv = 1;
> TRACE(" - port connected\n");
> }
> else {
> TRACE("connect port FAIL, err %s\n", strerror(errno));
> }
>
> end:
> return rv;


Anton Petrusevich

2005-08-29, 7:01 pm

Daniel W. wrote:

> I've done a calculation error with the port and I've spend hours to find
> out why I got a SIGPIPE signal in my program.


Because you write to an invalid socket and have not blocked SIGPIPE signal.
signal(SIGPIPE, SIG_IGN) will help you not to get SIGPIPE, but -1 as return
value and errno with error code. Others commented on you connection code.

--
Anton Petrusevich
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com