non blocking connect


 
Thread Tools Search this Thread
Top Forums Programming non blocking connect
# 1  
Old 09-05-2010
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:

Code:
x=fcntl(S,F_GETFL,0);
fcntl(s,F_SETFL,x | O_NONBLOCK);
rc=connect(s, (struct sockaddr *)&sin, sizeof(sin));
if( rc == 0)
  write(s,"service",7);
else
{
   if( errno == EINPROGRESS)
   {
        write(s,"service",7);
     FD_ZERO(&writeFDs);
     FD_SET(s, &writeFDs);
     rc=select(maxFDs, &writeFDs, (fd_set *) NULL, (fd_set *)NULL, 
                                                                (struct timeval *)(&timeout));
     if( rc ==0 || rc == -1)
       printf(" Timed out -- Not connected even after 3 secs wait");
    else
      printf(" connected and written");
}

My doubt is write on non blocking socket will happen or write fails and select will timeout. What do I have to do to connection successful

Moderator's Comments:
Mod Comment After more than 40 posts you should know how and when to apply code tags


---------- Post updated 09-05-10 at 01:11 PM ---------- Previous update was 09-04-10 at 11:56 PM ----------

I found wrong in my code. I should move my write to the else part and I should set read flags as NULL and write flags as my socket.

---------- Post updated at 01:15 PM ---------- Previous update was at 01:11 PM ----------

my code should be

Code:
if( errno == EINPROGRESS)
{        
     FD_ZERO(&writeFDs);
     FD_SET(s, &writeFDs);
     rc=select(maxFDs, (fd_set *) NULL, &writeFDs,  (fd_set *)NULL, 
                                                                (struct timeval *)(&timeout));
     if( rc ==0 || rc == -1)
       printf(" Timed out -- Not connected even after 3 secs wait");
    else
       write(s,"service",7);


Last edited by pludi; 09-05-2010 at 06:51 AM..
# 2  
Old 09-08-2010
I am connecting to port 7(echo service) in the remote machine.
suppose port 7 is not running in the remote machine I do not want to wait in connect call for long time.. so I am using non blocking connect with timeout of 3 seconds. But I am always getting the return code 1 from select call though the echo service is not running incase of write falgs set.

Code:
x=fcntl(S,F_GETFL,0);
fcntl(s,F_SETFL,x | O_NONBLOCK);
rc=connect(s, (struct sockaddr *)&sin, sizeof(sin));
if( errno == EINPROGRESS)
{        
     FD_ZERO(&writeFDs);
     FD_SET(s, &writeFDs);
     rc=select(maxFDs, (fd_set *) NULL, &writeFDs,  (fd_set *)NULL, 
                                                                (struct timeval *)(&timeout));
     if( rc ==0 || rc == -1)
       printf(" Timed out -- Not connected even after 3 secs wait");
    else
    {
	if(FD_ISSET(s,&writeFDs))
       	write(s,"service",7);
    }
}

In the above FD_ISSET is returning true and when it tries to write it retrun EPIPE, because of not connected. How we decide that service is ready for writing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Programming

Which are blocking and non-blocking api's in sockets in C ?

among the below socket programming api's, please let me know which are blocking and non-blocking. socket accept bind listen write read close (2 Replies)
Discussion started by: VSSajjan
2 Replies

3. Cybersecurity

When i start CSF i cant connect VPS or download any data into it It appears i cant connect Linux VP?

It appears i cant connect linux VPS server via SSH or i cant SCP any file to it and i cant wget any file TO it (from inside it) while CSF (Config Server Firewall, LFD is running. Just after isntall in default configuration and after changing TESTING mode to LIVE mode. Trying to wget & install... (1 Reply)
Discussion started by: postcd
1 Replies

4. Programming

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. ... (7 Replies)
Discussion started by: satish@123
7 Replies

5. AIX

AIX Remote Connect Fail With “No more multiple IP addresses to connect” Error

We have a production server at a client site running AIX. And recently when users are trying to connect to it via telnet, it prompts "No more multiple IP addresses to connect". Can I know what does this error mean? and how to rectify this? Thanks. (2 Replies)
Discussion started by: a_sim
2 Replies

6. UNIX for Advanced & Expert Users

ps blocking

Hi Folks I have been debugging a script that is called every thirty seconds. Basically it is doing a ps, well two actually, one to file (read by the getline below) and the other into a pipe. The one into the pipe is: - V_SYSVPS=/usr/sysv/bin/ps $V_SYSVPS -p$PIDLIST -o$PSARGS... (0 Replies)
Discussion started by: steadyonabix
0 Replies

7. 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

8. IP Networking

blocking DHCP

I've got a legit DHCP server on my network. I've got a 3550 as my VTP server providing 4 vlans to 4 2950 switches. If somebody were to plug into one of those vlans with a DHCP server configured then it would throw off my whole network. How could i block the DHCP server that could plug into the... (2 Replies)
Discussion started by: byblyk
2 Replies

9. 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

10. UNIX for Dummies Questions & Answers

blocking domains

Dear All , Kindly note I have sun solaries 7 . I want to block a domain who keep sending emails to my domain and users . thanks (1 Reply)
Discussion started by: tamemi
1 Replies
Login or Register to Ask a Question