Sponsored Content
Top Forums Programming Is this case is normal when programming in socket? Post 302469349 by DGPickett on Friday 5th of November 2010 03:05:29 PM
Old 11-05-2010
TCP is achieved with packets, the first is flagged syn, then syn-ack, the middle are probably ack, until fin and fin-ack are exchanged. The client sends syn to connect, and the server send syn-ack. The fd close sends fin and the other end sends fin-ack. If a packet is lost, the sending end might retry minutes later. So, like linger, there is a socket residue to answer any belated questions.

When you said accept, did you mean it is a server? Here is a generic server:
Code:
$ cat mysrc/u_inetd.c 
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#define P_EEXIT fprintf(stderr,"Abnormal exit at %s:%d\n",__FILE__,__LINE__),exit(1)
int main( int argc, char **argv ){
        int i, s, ns, loa, t_on = 1, ka = 10, only1 = 0, maxbuf=65535 ;
        union {
                struct sockaddr sa ;
                struct sockaddr_in sin ;
        } u ;
        struct sigaction siga ;
        u.sin.sin_port = 0 ;
        u.sin.sin_addr.s_addr = INADDR_ANY ;
        for ( i = 1 ; i < argc ; i++ ){
                if ( !strcmp( argv[i], "-l" )){
                        u.sin.sin_addr.s_addr = INADDR_LOOPBACK ;
                } else if ( !strcmp( argv[i], "-o" )){
                        only1 = 1 ;
                } else if ( u.sin.sin_port == 0 ){
                        if ( 1 > atoi( argv[i] )){
                                break ;
                        }
                        u.sin.sin_port = atoi( argv[i] );
                }
                else    /* must be command */
                        break ;
        }
        if ( u.sin.sin_port == 0
          || i == argc ){
                printf(
"Usage:  %s [ -l ] [ -o ] <port> <cmd> [<cmd args>]\n"
"\n"
"Listens on port for tcp connections, and for every connection,\n"
" runs <cmd> with any <cmd args>\n"
"  and with the socket as stdin and stdout (but stderr is preserved)\n"
"   under the user's id.  (Be careful!)\n"
"\n"
"The -l option restricts connections to clients on the same host\n"
" for greater security.\n"
"The -o option restricts the server to only one (1) service at a time\n"
" for commands that need exclusive control of a resource,\n"
" by waiting for child termination.  This can result in a hung server.\n"
"The <cmd> must be exec() compatible, so if it is not a sh shell script,\n"
" make sure to have '#!<full_path_of_interpreter> [ <one_arg> ]' as line 1.\n"
"\n",
                        argv[0] );
                exit( 1 );
        }
        /* ensure that no fd's are open that are not needed,
                as sh does not allow >&##       */
        for ( s = 0 ; s < 128 ; s++ ){
                if ( s != 2 )
                        close( s );
        }
        if ( 0 > ( s = socket( PF_INET, SOCK_STREAM, NULL ))){
                perror( "socket( PF_INET, SOCK_STREAM, NULL )" );
                P_EEXIT ;
        }
        u.sin.sin_family = AF_INET ;
        if ( setsockopt(  s, IPPROTO_TCP, TCP_NODELAY, (char *)&t_on, sizeof(int))){
                 perror( "setsockopt( s, IPPROTO_TCP, TCP_NODELAY, (char *)&t_on, sizeof(int))" );
                P_EEXIT ;
        }
        if ( setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&t_on,
                sizeof(int))){
                perror( "setsockopt( s, SOL_SOCKET, SO_REUSEADDR, (char*)&t_on, sizeof(int) )" );
                P_EEXIT ;
        }
        if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char*)&ka,
                sizeof(int))){
                perror( "setsockopt( s, SOL_SOCKET, SO_KEEPALIVE, (char*)&ka, sizeof(int) )" );
                P_EEXIT ;
        }
        if ( setsockopt(  s, SOL_SOCKET, SO_RCVBUF, (char *)&maxbuf, sizeof(int))){
                 perror( "setsockopt( s, IPPROTO_TCP, SO_RCVBUF, (char *)&maxbuf, sizeof(int))" );
                P_EEXIT ;
        }
        if ( setsockopt(  s, SOL_SOCKET, SO_SNDBUF, (char *)&maxbuf, sizeof(int))){
                 perror( "setsockopt( s, IPPROTO_TCP, SO_SNDBUF, (char *)&maxbuf, sizeof(int))" );
                P_EEXIT ;
        }
        if ( bind( s, &u.sa, sizeof(u.sa))){
                perror( "bind()" );
                P_EEXIT ;
        }
        if ( listen( s, 256 )){
                perror( "listen( s, 256 )" );
                P_EEXIT ;
        }
        siga.sa_handler = NULL ;
        sigemptyset( &siga.sa_mask );
        siga.sa_flags = SA_NOCLDWAIT ;
        sigaction( SIGCHLD, &siga, NULL );
next:
        loa = sizeof(u.sa);
        if ( 0 > ( ns = accept( s, &u.sa, &loa ))){
                if ( errno == EINTR
                  || errno == EAGAIN ){
                        goto next ;
                }
                perror( "accept( s, &u.sa, sizeof(u.sa) )" );
                P_EEXIT ;
        }
refork:
        switch ( fork()){
        case -1 :
                sleep( 1 );
                goto refork ;
        case 0 :
                break ;
        default:                /* Parent */
                close( ns );
                if ( only1 ){
                        do {
                                wait( NULL );
                        } while ( errno != ECHILD );
                }
                goto next ;
        }
        /* child */
        close(s);
        do {
                s = dup(ns);
        } while ( s < 1 && s >= 0 );
        if ( ns > 2 ){
                close(ns);
        }
        execvp( argv[i], argv + i );
        /* should never get here */
        perror( "execvp( argv[i], argv + i )" );
        P_EEXIT ;
}

 

10 More Discussions You Might Find Interesting

1. Programming

Socket Programming

Dear Reader, Is there any way to check up socket status other than 'netstatus ' Thanks in advance, (1 Reply)
Discussion started by: joseph_shibu
1 Replies

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

3. IP Networking

socket programming

Hello Everyone Iam working on tcp/ip programming.with some time interval server has to send data.client has to close the connection and to open the connection between the time interval.this is the scenario when iam closing the connection in client side the connection terminates.how to... (1 Reply)
Discussion started by: sureshvaikuntam
1 Replies

4. UNIX for Advanced & Expert Users

socket programming

Hi, I am trying to connect to more than 60 servers to perform some actions remotely. for this I am using ssh2. But its taking lot of time. Though i am running the command in background, still its taking long time to execute.. Any one let me know can we use sockets instead of ssh2 for... (3 Replies)
Discussion started by: pvamsikr
3 Replies

5. UNIX for Advanced & Expert Users

socket programming

can we send udp message to a destination ip address .. without having an ip address configured in our machine using recvfrom ? (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

6. Programming

Help with socket programming in C

hi guys i got this code trying to make connection between the server and multi clients but when i do ./server i got message server waiting then when i run ./client it says client 1 nosuch file i dont know whats that should i use any argument plz help how to compile and run and whats the expected... (1 Reply)
Discussion started by: kedah160
1 Replies

7. Programming

help with socket programming in c

i'm doing a simple program in socket programming on c i have server that can handle 2clients in a single machine i'm running ubuntu linux so i got it work but the probelm when clients send a message the server will echo it but i cant distinguish which client send the message client 1 or client... (7 Replies)
Discussion started by: kedah160
7 Replies

8. UNIX for Advanced & Expert Users

Socket programming

my socket program is not working with larger port numbers like more than 60000 , any reason why ? (4 Replies)
Discussion started by: Gopi Krishna P
4 Replies

9. Programming

Socket programming

Hi everyone, I'm new to this forum. I'm working on new project for last few days and this forum already helped me on couple of occasions. I don't have any prior experience with network programming so I'll appreciate any advise given. I'm trying to do the following: 1. open user... (2 Replies)
Discussion started by: _thomas
2 Replies

10. Ubuntu

Socket Programming

HI Can anyone provide me with codes for file transfer server to client or vice versa? Also please explain how to compile those programs in ubuntu terminal as i am totally new to socket programming. Thanks (1 Reply)
Discussion started by: mayhemtrigger
1 Replies
All times are GMT -4. The time now is 04:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy