Sponsored Content
Top Forums Programming Looping connect call for a non blocking socket Post 302704937 by satish@123 on Monday 24th of September 2012 03:15:02 AM
Old 09-24-2012
Looping connect call for a non blocking socket

will there be any unexpected results on looping connect call for a non blocking socket to determine the connection based on error code. I am getting connection unsuccessful intermittently and so wondering whether is the timeout 500 millisec not sufficient or looping connect cause any unexpected.

//sample piece of code that I used


Code:
int type,x, MaxTries=0;
type = SOCK_STREAM;
x = fcntl(s,F_GETFL, 0);
fcntl(s,F_SETFL, x | O_NONBLOCK);
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
  while( (errno == EALREADY && MaxTries < 5) || (errno == EINPROGRESS && MaxTries < 5) )
                {
                        poll(NULL,0,100); //100 millisec sleep for 5 retries max.
                        connect(s, (struct sockaddr *)&sin, sizeof(sin));
                        MaxTries = MaxTries + 1;
                }
                if(errno == EISCONN)
                      printf("Connected");
                else
                      printf("Not Connected - %d",errno);
}


I know the Otherway is using SELECT. This would be better solution than looping but I doubt any significant difference in intermittent connections.

Code:
x = fcntl(s,F_GETFL, 0);
   fcntl(s,F_SETFL, x | O_NONBLOCK);

   FD_ZERO(&writeFDs);
   FD_SET(s, &writeFDs);

   timeout1.tv_sec = 0;
   timeout1.tv_usec = 500000;
   
   connect(s, (struct sockaddr *)&sin, sizeof(sin));
   if(select (maxFDs, (fd_set *)NULL, &writeFDs, (fd_set *)NULL, (struct timeval *)(&timeout1))  > 0 )   
        printf("Connected");
    else
       printf("Not Connected -%d", errno);

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Blocking ftp users to connect using telnet

Hi everybody ! We have all flavors of Unix / Linux and we want to restrict ftp users to telnet our servers. We can't disable telnet because we have other users using it. :confused: Are there any thing that could be done to solve this thing ??? Best regards, Julio Moreira (11 Replies)
Discussion started by: juliocdrm
11 Replies

2. Shell Programming and Scripting

Looping connect to different server

Hi there. I am attempting to write a script that will read through a flat file, get the server and directory that I want the size for, connect to the server, get the directory size, and send it to an output file. Here is what I have: while read LINE do NAME=`echo $LINE | awk -F'~' '{print... (5 Replies)
Discussion started by: la_womn
5 Replies

3. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

4. Programming

connect() socket API

Is there any relation between the connect() socket API and the TCP's Transmission Control Block. Also how does connect detect that a socket is in use i.e., EADDRINUSE (1 Reply)
Discussion started by: sunaina
1 Replies

5. Solaris

Non-blocking connect readability & writeability

Hello, When using a non-blocking connect, is it _guaranteed_ that connection completion can be detected by selecting for writeability? I have encountered situations where the socket has returned both readable and writeable at the same time - having trawled the net, I have seen some old posts... (1 Reply)
Discussion started by: tristan12
1 Replies

6. Programming

non blocking connect

OS : solaris 10 X86 I created stream socket, tries to connect to port 7 on the remote machine. After doing the non blocking connect call I did select with time out value is 3 secs. I am always getting timed out though I am writing prior to select. code: x=fcntl(S,F_GETFL,0);... (1 Reply)
Discussion started by: satish@123
1 Replies

7. Programming

write on Non Blocking Socket

How to know whether socket is ready for write. select(maxfds, (fd_set *)NULL, &writefds, NULL, &timeout); By default socket is set for write without checking whether it would block or not? If so how do I know my FD is ready for writing. (3 Replies)
Discussion started by: satish@123
3 Replies

8. Programming

socket accept() keeps looping

I'm using C/ C++ with gcc on Linux. I've a server socket where accept() is called on the socket inside a while() loop. The problem I am facing is that the first call to accept is blocking (i.e., the program waits for the first connection) but as soon as I fork afterwards (so that the child process... (2 Replies)
Discussion started by: jaywalker
2 Replies

9. Programming

Looping problems socket programming in C

Sorry if I posted 2 separate questions. I'm currently doing socket programming on my current task. As you can see below in the client side. I've tried to do a loop so I will be able to get prompt for input over and over again. This is the code. do{ printf("Please your name > ");... (10 Replies)
Discussion started by: aLHaNz
10 Replies

10. UNIX for Dummies Questions & Answers

Cannot connect to socket

solaris client report "cannot to socket(code 25)" while trying to backup on veritas netbackup 6.5.5 server (1 Reply)
Discussion started by: EUGINIAM
1 Replies
connect(2)							System Calls Manual							connect(2)

NAME
connect - Connects two sockets SYNOPSIS
#include <sys/socket.h> int connect ( int socket, const struct sockaddr *address, socklen_t address_len ); [XNS4.0] The definition of the connect() function in XNS4.0 uses a size_t data type instead of a socklen_t data type as specified in XNS5.0 (the previous definition). [Tru64 UNIX] The following definition of the connect() function does not conform to current standards and is supported only for backward compatibility (see standards(5)): int connect ( int socket, struct sockaddr *address, int address_len ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: connect(): XNS5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Specifies a file descriptor for the socket. Points to a sockaddr structure, the format of which is determined by the domain and by the behavior requested for the socket. The sockaddr structure is an overlay for a sockaddr_in or sockaddr_un structure, depending on which of the supported address families is active. [Tru64 UNIX] If the compile-time option _SOCKADDR_LEN is defined before the sys/socket.h header file is included, the sockaddr structure takes 4.4BSD behavior, with a field for specifying the length of the socket address. Otherwise, the default 4.3BSD sock- addr structure is used, with the length of the socket address assumed to be 14 bytes or less. If _SOCKADDR_LEN is defined, the 4.3BSD sockaddr structure is defined with the name osockaddr. Specifies the length of the sockaddr structure pointed to by the address parameter. DESCRIPTION
The connect() function requests a connection between two sockets. The kernel sets up the communications links between the sockets; both sockets must use the same address format and protocol. The connect() function performs a different action for each of the following types of initiating sockets: If the initiating socket is SOCK_DGRAM, the connect() function establishes the peer address. The peer address identifies the socket where all datagrams are sent on subsequent send() functions. It also identifies the socket from where datagrams can be received; datagrams from other peer addresses are not delivered. If address is a null address for the protocol, the socket's peer address is reset. No connections are made by this connect function. If the initiating socket is SOCK_STREAM, the connect() function attempts to make a connection to the socket specified by the address parameter. Each communication space interprets the address parameter differ- ently. If the function fails for a connection mode socket, applications should use the close() function to deallocate the socket and descriptor. If attempting to reinitiate the connection, applications should create a new socket. If the connection cannot be established immediately, one of the following occurs: If the socket file descriptor is marked non-blocking, the connect() function fails and sets errno to [EINPROGRESS]. However, the connection request is not aborted; the connection will be estab- lished asynchronously. If you make a call to connect() for the same socket before the connection is established, the function fails and sets errno to [EALREADY]. If the socket file descriptor is marked blocking, the connect() function blocks for an unspecified amount of time until the connection is established. If the timeout interval expires before the connection is established, the function fails and aborts the connection attempt. While blocked, if connect() is interrupted by a signal, it fails and sets errno to [EINTR]. However, the connection request is not aborted; the connection is established asynchronously. RETURN VALUES
Upon successful completion, the connect() function returns a value of 0 (zero). Otherwise, a value of -1 is returned and errno is set to indicate the error. ERRORS
If the connect() function fails, errno may be set to one of the following values: Search permission is denied for a component of the path prefix; or write access to the named socket is denied. [XNS4.0] This error applies to AF_UNIX sockets only. The specified address is already in use. The specified address is not avail- able from the local machine. The addresses in the specified address family cannot be used with this socket. A connection request is already in progress for the specified socket. The socket parameter is not valid. The attempt to connect was rejected. The remote host reset the connection request. The address parameter is not in a readable part of the user address space. The specified host is not reachable. O_NONBLOCK is set for the file descriptor for the socket and the connection cannot be immediately estab- lished; the connection will be established asynchronously. The connect() function was interrupted by a signal while waiting for the connection to be established. The connection establishment may continue asynchronously. The value of the address_len parameter is invalid for the specified address family; or the sa_family field in the socket address structure is invalid for the protocol. For an AF_UNIX socket, an I/O error occurred while reading from or writing to the file system. The socket is already connected. For an AF_UNIX socket, too many symbolic links were encountered in translating the pathname in address. A component of the pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters. The local network connection is not operational. No route to the network or host is present. Insufficient resources are available in the system to complete the call. For an AF_UNIX socket, a component of the pathname does not name an existing file or the pathname is an empty string. The available STREAMS resources were insufficient for the operation to complete. For an AF_UNIX socket, a component of the path prefix of the pathname in address is not a directory. The socket parameter refers to a file, not a socket. The socket is listening and cannot be connected. The specified address has a different type than the socket bound to the specified peer address. The establishment of a connection timed out before a connection was made. [Tru64 UNIX] The socket is marked nonblocking, so the connection cannot be immediately completed. The application program can select the socket for writing during the connection process. RELATED INFORMATION
Functions: accept(2), bind(2), socket(2), getsockname(2), select(2), send(2). Standards: standards(5). delim off connect(2)
All times are GMT -4. The time now is 01:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy