Sponsored Content
Full Discussion: fcntl on socket
Top Forums Programming fcntl on socket Post 302434201 by migurus on Thursday 1st of July 2010 08:07:38 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
MAXDB_FETCH_FIELDS(3)							 1						     MAXDB_FETCH_FIELDS(3)

maxdb_fetch_fields - Returns an array of resources representing the fields in a result set

       Procedural style

SYNOPSIS
mixed maxdb_fetch_fields (resource $result) DESCRIPTION
Object oriented style mixed maxdb_result::fetch_fields (void ) This function serves an identical purpose to the maxdb_fetch_field(3) function with the single difference that, instead of returning one resource at a time for each field, the columns are returned as an array of resources. RETURN VALUES
Returns an array of resources which contains field definition information or FALSE if no field information is available. Object properties +-----------+---------------------------------------------------+ | Property | | | | | | | Description | | | | +-----------+---------------------------------------------------+ | name | | | | | | | The name of the column | | | | |max_length | | | | | | | The maximum width of the field for the result | | | set. | | | | | type | | | | | | | The data type used for this field | | | | | decimals | | | | | | | The number of decimals used (for integer fields) | | | | +-----------+---------------------------------------------------+ EXAMPLES
Example #1 Object oriented style <?php $maxdb = new maxdb("localhost", "MONA", "RED", "DEMODB"); /* check connection */ if (maxdb_connect_errno()) { printf("Connect failed: %s ", maxdb_connect_error()); exit(); } $query = "SELECT name, cno from hotel.customer ORDER BY cno"; if ($result = $maxdb->query($query)) { /* Get field information for all columns */ $finfo = $result->fetch_fields(); foreach ($finfo as $val) { printf("Name: %s ", $val->name); printf("Table: %s ", $val->table); printf("max. Len: %d ", $val->max_length); printf("Flags: %d ", $val->flags); printf("Type: %d ", $val->type); } $result->close(); } /* close connection */ $maxdb->close(); ?> Example #2 Procedural style <?php $link = maxdb_connect("localhost", "MONA", "RED", "DEMODB"); /* check connection */ if (maxdb_connect_errno()) { printf("Connect failed: %s ", maxdb_connect_error()); exit(); } $query = "SELECT name, cno from hotel.customer ORDER BY cno"; if ($result = maxdb_query($link, $query)) { /* Get field information for all columns */ $finfo = maxdb_fetch_fields($result); foreach ($finfo as $val) { printf("Name: %s ", $val->name); printf("Table: %s ", $val->table); printf("max. Len: %d ", $val->max_length); printf("Flags: %d ", $val->flags); printf("Type: %d ", $val->type); } maxdb_free_result($result); } /* close connection */ maxdb_close($link); ?> The above example will output something similar to: Name: NAME Table: max. Len: 10 Flags: -1 Type: 2 Name: CNO Table: max. Len: 4 Flags: -1 Type: 0 SEE ALSO
maxdb_num_fields(3), maxdb_fetch_field(3), maxdb_fetch_field_direct(3). PHP Documentation Group MAXDB_FETCH_FIELDS(3)
All times are GMT -4. The time now is 03:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy