convert a single to multiuser.. in sock.c program

Discussion in 'C Programming' started by OZ, Feb 8, 2004.

  1. OZ

    OZ Guest

    the serproxy claim itself a multi-thread proxy thing.

    I have sent email to write the original writer and there is no replay after
    3 weeks. my configuration and setting are good.

    http://www.lspace.nildram.co.uk/freeware.html

    I installed it in rh 9.0

    I found it is single user only.
    do any one know how to change the single program in c program into a multi
    /*
    * sock.c
    *
    * socket routines
    *
    * (C)1999 Stefano Busti
    *
    */


    #include <stdlib.h>

    #include "sock.h"

    #if defined(SOCK_BSD)
    # include <unistd.h>
    # include <sys/types.h>
    # include <sys/socket.h>
    # include <netdb.h>
    # include <netinet/in.h>
    # include <arpa/inet.h>
    # include <fcntl.h>
    # include <sys/ioctl.h>
    #elif defined(SOCK_WIN)
    # define ioctl ioctlsocket
    #endif

    int sock_start(void)
    {
    #if defined(SOCK_BSD)
    return 0;
    #elif defined (SOCK_WIN)
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD(1, 1);
    return WSAStartup(wVersionRequested, &wsaData);
    #endif
    }


    void sock_finish(void)
    {
    #if defined(SOCK_WIN)
    WSACleanup();
    #endif
    }

    int tcp_init(tcpsock_s *sock, int port)
    {
    int optval = 1;

    //struct protoent *ent;
    struct sockaddr_in name;

    //ent = getprotobyname("TCP");

    //if (!ent)
    // return -1;

    sock->fd = socket(AF_INET, SOCK_STREAM, 0/*ent->p_proto*/);

    if (sock->fd < 0)
    return -1;

    /* Allow reuse of existing TIME_WAIT sockets */
    if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR,
    (void *)&optval, sizeof(optval)))
    {
    tcp_cleanup(sock);
    return -1;
    }

    if (ioctl(sock->fd, FIONBIO, &optval))
    {
    tcp_cleanup(sock);
    return -1;
    }

    name.sin_family = AF_INET;
    name.sin_port = htons(port);
    name.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sock->fd, (struct sockaddr *)&name, sizeof(name)) < 0)
    {
    tcp_cleanup(sock);
    return -1;
    }

    sock->port = port;

    return 0;
    }

    void tcp_cleanup(tcpsock_s *sock)
    {
    #if defined(SOCK_BSD)
    close(sock->fd);
    #elif defined(SOCK_WIN)
    closesocket(sock->fd);
    #endif
    }

    int tcp_listen(tcpsock_s *sock)
    {
    return listen(sock->fd, 0);
    }

    tcpsock_s *tcp_accept(tcpsock_s *sock)
    {
    struct sockaddr addr;
    int addrlen;
    int fd;
    tcpsock_s *newsock;

    addrlen = sizeof(addr);

    fd = accept(sock->fd, &addr, &addrlen);

    if (fd < 0)
    return NULL;

    newsock = malloc(sizeof(tcpsock_s));

    if (newsock)
    newsock->fd = fd;

    return newsock;
    }

    void tcp_refuse(tcpsock_s *sock)
    {
    tcpsock_s *temp = tcp_accept(sock);

    if (temp)
    {
    /* Disconnect immediately */
    tcp_disconnect(temp);

    /* Clean up */
    tcp_cleanup(temp);
    free(temp);
    }
    }

    int tcp_connect(tcpsock_s *sock, char *host, int port)
    {
    struct sockaddr_in name;
    struct hostent *hostinfo;
    u_long addr;

    name.sin_family = AF_INET;
    name.sin_port = htons(port);

    addr = inet_addr(host);
    hostinfo = gethostbyaddr((char *)&addr, 4, PF_INET);
    /*hostinfo = gethostbyname(hostname);*/

    if (hostinfo == NULL)
    return -1;

    name.sin_addr = *(struct in_addr *) hostinfo->h_addr;

    return connect(sock->fd, (struct sockaddr *)&name, sizeof(name));
    }

    void tcp_disconnect(tcpsock_s *sock)
    {
    shutdown(sock->fd, 2);
    }

    #ifdef SOCK_BSD
    __inline
    #endif
    int tcp_read(tcpsock_s *sock, void *buf, size_t count)
    {
    #if defined(SOCK_BSD)
    return read(sock->fd, buf, count);
    #elif defined(SOCK_WIN)
    return recv(sock->fd, buf, count, 0);
    #endif
    }

    #ifdef SOCK_BSD
    __inline
    #endif
    int tcp_write(tcpsock_s *sock, void *buf, size_t count)
    {
    #if defined(SOCK_BSD)
    return write(sock->fd, buf, count);
    #elif defined(SOCK_WIN)
    return send(sock->fd, buf, count, 0);
    #endif
    }

    int tcp_getport(tcpsock_s *sock)
    {
    return sock->port;
    }

    void tcp_debug(tcpsock_s *sock, FILE *f)
    {
    fprintf(f, "tcp_socket {\n");
    fprintf(f, "\tfd = %d\n", sock->fd);
    fprintf(f, "\tport = %d\n", sock->port);
    fprintf(f, "}\n\n");
    }

    ---

    mcu development team
    http://arm.web7days.com
    OZ, Feb 8, 2004
    #1
    1. Advertising

  2. OZ

    Mike Wahler Guest

    Re: [OT, welcome msg] convert a single to multiuser.. in sock.c program

    "OZ" <> wrote in message
    news:c04l9j$...
    > the serproxy claim itself a multi-thread proxy thing.


    Your query and the code you posted are not topical for
    comp.lang.c

    Before posting here again, please read:
    http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

    Thank you.

    -Mike
    Mike Wahler, Feb 8, 2004
    #2
    1. Advertising

  3. OZ

    OZ Guest

    Re: [OT, welcome msg] convert a single to multiuser.. in sock.c program

    Mike Wahler <> wrote in message
    news:6UkVb.15770$...
    > "OZ" <> wrote in message
    > news:c04l9j$...
    > > the serproxy claim itself a multi-thread proxy thing.

    >
    > Your query and the code you posted are not topical for
    > comp.lang.c
    >
    > Before posting here again, please read:
    > http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    >
    > Thank you.



    this is a "C" program program, a single user program has be changed to
    mutliple user.





    >
    > -Mike
    >
    >
    OZ, Feb 8, 2004
    #3
  4. OZ

    Mike Wahler Guest

    Re: [OT, welcome msg] convert a single to multiuser.. in sock.c program

    "OZ" <> wrote in message
    news:c04n4l$...
    >
    > Mike Wahler <> wrote in message
    > news:6UkVb.15770$...
    > > "OZ" <> wrote in message
    > > news:c04l9j$...
    > > > the serproxy claim itself a multi-thread proxy thing.

    > >
    > > Your query and the code you posted are not topical for
    > > comp.lang.c
    > >
    > > Before posting here again, please read:
    > > http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    > >
    > > Thank you.

    >
    >
    > this is a "C" program program,


    Simply saying that doesn't make it so.

    Your program contains *many* nonstandard extensions (for example,
    not one of the #included headers is standard), it's not ISO standard
    C, which is the topic here. Did you not read the document whose
    link I cited above?

    >a single user program has be changed to
    > mutliple user.


    The C langauge has no notion of 'single user' or 'multi user'.
    Those are operating system concepts. Judging from what I saw
    in the code, you'll probably get the best help from Unix group.

    -Mike
    Mike Wahler, Feb 8, 2004
    #4
  5. OZ wrote:

    > the serproxy claim itself a multi-thread proxy thing.
    > I have sent email to write the original writer
    > and there is no reply after 3 weeks.
    > my configuration and setting are good.
    >
    > http://www.lspace.nildram.co.uk/freeware.html
    >
    > I installed it in rh 9.0
    >
    > I found it is single user only.
    > Does any one know how to change the single program in c program into a multi


    No.

    This is not the right place to get the answers you want.
    Try contacting the author again.
    If you can't get the author to help you,
    you will probably need to find a different package
    that does what you want to do. Good luck.
    E. Robert Tisdale, Feb 8, 2004
    #5
  6. OZ

    Mac Guest

    On Sat, 07 Feb 2004 23:59:25 +0000, E. Robert Tisdale wrote:

    > OZ wrote:
    >
    >> the serproxy claim itself a multi-thread proxy thing.
    >> I have sent email to write the original writer
    >> and there is no reply after 3 weeks.
    >> my configuration and setting are good.
    >>
    >> http://www.lspace.nildram.co.uk/freeware.html
    >>
    >> I installed it in rh 9.0
    >>
    >> I found it is single user only.
    >> Does any one know how to change the single program in c program into a multi

    >
    > No.
    >
    > This is not the right place to get the answers you want.
    > Try contacting the author again.
    > If you can't get the author to help you,
    > you will probably need to find a different package
    > that does what you want to do. Good luck.


    The OZ could also try another newsgroup, such as comp.unix.programmer, but
    should read the FAQ for that group prior to posting there.

    Mac
    Mac, Feb 9, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. OZ
    Replies:
    1
    Views:
    701
    Aggro
    Feb 8, 2004
  2. Jason

    sock.recv() in Windows

    Jason, Aug 22, 2004, in forum: Python
    Replies:
    1
    Views:
    621
    Peter Hansen
    Aug 22, 2004
  3. Nikolaus Rath
    Replies:
    1
    Views:
    75
    Roy Smith
    Feb 17, 2014
  4. Chris Angelico
    Replies:
    0
    Views:
    74
    Chris Angelico
    Feb 17, 2014
  5. Nikolaus Rath
    Replies:
    0
    Views:
    82
    Nikolaus Rath
    Feb 18, 2014
Loading...

Share This Page