fcntl on socket


 
Thread Tools Search this Thread
Top Forums Programming fcntl on socket
# 1  
Old 07-01-2010
fcntl on socket

I have very simple client - server setup, where client connects, exchanges some messages with the server and then closes socket and exits. Everythink works OK, except when I am trying to change socket to be non-blocking AND the shell happens to be plain old Bourne. It runs no problem under ksh or bash. But when I run it under sh the program completes and then it kills the shell and kicks me out.
Below is my code, for simplicity I left the connection part of it as it is enough to highlight the problem:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/netinet/in.h>
#include <sys/netinet/tcp.h>
#define         MAXSIZE         1024
const   int     srvr_port = 5777;
const   char    *locl_addr = "127.0.0.1";
 
main(int argc, char *argv[])
{
  struct sockaddr_in    saddr;
  int   val;
  int   sh;
  int   sd;
  int   rc;
        sd = socket(AF_INET, SOCK_STREAM, 0);
        memset(&saddr, 0, sizeof(saddr));
        saddr.sin_family        = AF_INET;
        saddr.sin_addr.s_addr   = inet_addr(locl_addr);
        saddr.sin_port          = htons(srvr_port);
        sh = connect(sd, (struct sockaddr *)&saddr, sizeof(saddr));
        if(sh != -1)
        {
                printf("Connected OK\n");
                val = fcntl(sh, F_GETFL);
                printf("orig val=%i\n", val);
                val = val | O_NONBLOCK;
                printf("set  val=%i\n", val);
                rc = fcntl(sh, F_SETFL, val); /* if I comment out */
                printf("set  rc=%i\n", rc); /* the SETFL line all is OK */
        }
        else
        {
                printf("Connect Failed, errno=%i\n", errno);
                return(1);
        }
        close(sd);
        printf("sd closed\n");
        return(0);
}

I run under OpenServer 5.0.7, native compiler.

To avoid having to re-login I start Bourne shell from bash, below is my run session:

Code:
bash-2.03$ sh
$ 
$ zx; echo "RC=$?"
Connected OK
orig val=2
set  val=130
set  rc=0
sd closed
RC=0
$ bash-2.03$

See, how the sh is terminated and I am back to bash

As I indicated above, if I don't do fcntl(sh, F_SETFL, val) problem disappears. The server side is not affected in any case.

Any pointers would be appreciated.
# 2  
Old 07-02-2010
What OS are you running?
# 3  
Old 07-02-2010
If the connect call works, you're operating on file descriptor zero:

Code:
        sh = connect(sd, (struct sockaddr *)&saddr, sizeof(saddr));
        if(sh != -1)
        {
                printf("Connected OK\n");
                val = fcntl(sh, F_GETFL);
                printf("orig val=%i\n", val);
                val = val | O_NONBLOCK;
                printf("set  val=%i\n", val);
                rc = fcntl(sh, F_SETFL, val); /* if I comment out */
                printf("set  rc=%i\n", rc); /* the SETFL line all is OK */
        }

The variable sh contains the status of the connect call, not the socket itself.

That should be:

Code:
        sh = connect(sd, (struct sockaddr *)&saddr, sizeof(saddr));
        if(sh != -1)
        {
                printf("Connected OK\n");
                val = fcntl(sd, F_GETFL);
                printf("orig val=%i\n", val);
                val = val | O_NONBLOCK;
                printf("set  val=%i\n", val);
                rc = fcntl(sd, F_SETFL, val); /* if I comment out */
                printf("set  rc=%i\n", rc); /* the SETFL line all is OK */
        }

This User Gave Thanks to achenle For This Post:
# 4  
Old 07-02-2010
Quote:
What OS are you running?
OpenServer 5.0.7
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Block with fcntl

Good evening, friends I'm learning with a book: Programming Linux by Kurt Wall (Prentice Hall) The code below could run in two windows (./lockit /tmp/foo in both for example). There is not problem with the read block (first byte) but when one, apply the write block while in the other is... (1 Reply)
Discussion started by: joe_cosmo
1 Replies

2. Shell Programming and Scripting

File Locking with fcntl on Darwin Mac OSX

Hello I have a Perl script that works on non-darwin Mac OS X environments and I think I have narrowed down the issue to a file locking problem. In other linux environments, the flock struct is defined differently. I have adjusted this via the reference for Mac OS X fcntl(2) man page. The... (4 Replies)
Discussion started by: flagman5
4 Replies

3. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

4. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

5. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

6. Programming

fcntl works in linux but not in mac os x

Hi, Unless I am missing some serious differences in Mac and linux in terms of C programming, I dont know why this would happen. Please take a look at the following piece of code fragment: bool add_input_to_db(Cons *new_data) { // Set the attributes of the lock struct flock fl =... (3 Replies)
Discussion started by: newhere
3 Replies

7. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

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

9. Programming

I have no clue what's the point - fcntl() ?

hi, I need to know how to lock a file. I used the following code, but after executing the program the file 'write.txt' remined empty, and I have no idea why.Maybe I'm not using the corresponding syntax for blocking a file. But I deleted then the blocking part and the problem persisted. see to... (2 Replies)
Discussion started by: atticus
2 Replies

10. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies
Login or Register to Ask a Question