newbie to network programming

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default newbie to network programming

    Hey there,

    I'm having trouble with this very basic network program. All it does is wait
    for a connection to my machine on port 4000. THen when it gets a connection
    it sends the string Hello World to the caller. I am having problems in that
    when the program gets to the send function I get the error socket operation
    on nonsocket and I cant work out why this is since newsocket is an integer,
    given a value by accept.

    Here is my source, all one main function. As I said I'm just starting out on
    network programming, but have a big background in C.

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    int mysocket, newsocket;
    struct sockaddr_in myaddr;
    struct sockaddr_in calleraddr;
    int sin_size;
    close(mysocket);

    myaddr.sin_family=AF_INET;
    myaddr.sin_port=htons(4000);
    myaddr.sin_addr.s_addr=inet_addr("192.168.1.3");
    memset(&(myaddr.sin_zero),'\0',8);

    mysocket=socket(AF_INET, SOCK_STREAM, 0);
    if (bind(mysocket, (struct sockaddr *)&myaddr, sizeof(struct sockaddr))==
    -1) {
    perror("bind");
    exit(1);
    }


    if (listen(mysocket, 5)==-1)
    {
    perror("listen");
    exit(1);
    };

    while(1)
    {
    sin_size = sizeof(struct sockaddr_in);
    if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
    &sin_size)==-1)
    {
    perror(accept);
    exit(1);
    };
    if (send(newsocket, "Hello, world!\n", 14, 0) == -1) perror("send");
    printf("server: got connection from
    %s\n",inet_ntoa(calleraddr.sin_addr));
    close(newsocket);

    };

    close(mysocket);



    return EXIT_SUCCESS;
    }
    Dave Hughes Guest

  2. Similar Questions and Discussions

    1. clueless newbie to web programming
      I have never coded in perl or mod_perl before and have a project that needs me to do so. It is a web data entry system, using mod_perl to generate...
    2. Book review: Network Programming With Perl by Lincoln Stein
      http://www.amazon.com/exec/obidos/tg/detail/-/0201615711/qid=1074699239/sr=1 -1/ref=sr_1_1/102-8963439-4561724?v=glance&s=books Drieux and others...
    3. network programming?
      On Dec 2, 2003, at 1:22 PM, McMahon, Chris wrote: First off RUN AWAY! JUST SAY NO! DO NOT DO THIS! right, so much for the obligatory...
    4. Need Perl teacher/school: Network programming
      For months (actually years) I have been trying to learn how to write a Perl proxy that can do the following: intercept *all* HTTP and HTTPS traffic...
    5. Problem with configure script in Unix network programming book by Stevens
      After downloading the source from the website, I tried running the configure script. All I got is: : command not found : command not found :...
  3. #2

    Default newbie to network programming

    Hey there,

    I'm having trouble with this very basic network program. All it does is wait
    for a connection to my machine on port 4000. THen when it gets a connection
    it sends the string Hello World to the caller. I am having problems in that
    when the program gets to the send function I get the error socket operation
    on nonsocket and I cant work out why this is since newsocket is an integer,
    given a value by accept.

    Here is my source, all one main function. As I said I'm just starting out on
    network programming, but have a big background in C.

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    int mysocket, newsocket;
    struct sockaddr_in myaddr;
    struct sockaddr_in calleraddr;
    int sin_size;
    close(mysocket);

    myaddr.sin_family=AF_INET;
    myaddr.sin_port=htons(4000);
    myaddr.sin_addr.s_addr=inet_addr("192.168.1.3");
    memset(&(myaddr.sin_zero),'\0',8);

    mysocket=socket(AF_INET, SOCK_STREAM, 0);
    if (bind(mysocket, (struct sockaddr *)&myaddr, sizeof(struct sockaddr))==
    -1) {
    perror("bind");
    exit(1);
    }


    if (listen(mysocket, 5)==-1)
    {
    perror("listen");
    exit(1);
    };

    while(1)
    {
    sin_size = sizeof(struct sockaddr_in);
    if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
    &sin_size)==-1)
    {
    perror(accept);
    exit(1);
    };
    if (send(newsocket, "Hello, world!\n", 14, 0) == -1) perror("send");
    printf("server: got connection from
    %s\n",inet_ntoa(calleraddr.sin_addr));
    close(newsocket);

    };

    close(mysocket);



    return EXIT_SUCCESS;
    }
    Dave Hughes Guest

  4. #3

    Default Re: newbie to network programming

    In article <xneWa.129$Am1.97@newsfep1-gui.server.ntli.net>,
    Dave Hughes <donotresus@yahoo.co.uk> wrote:
    >Hey there,
    >
    >I'm having trouble with this very basic network program. All it does is wait
    >for a connection to my machine on port 4000. THen when it gets a connection
    >it sends the string Hello World to the caller. I am having problems in that
    >when the program gets to the send function I get the error socket operation
    >on nonsocket and I cant work out why this is since newsocket is an integer,
    >given a value by accept.
    ....
    > if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
    >&sin_size)==-1)
    Classic error.

    You're setting newsocket to the result of the == comparison, not to the
    socket returned by accept(). You need another set of parentheses around
    the assignment.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  5. #4

    Default Re: newbie to network programming

    On Thu, 31 Jul 2003 20:41:28 +0100, Dave Hughes <donotresus@yahoo.co.uk>
    wrote:

    [snip]

    Taking a quick glance, I see this:

    sizeof(struct sockaddr)

    This is surely wrong... stuct sockaddr is really an abstrat type, with the
    wrong size. You need the size of the actual address structure you're using -
    - struct sockaddr_in.

    (There may be other things wrong.)

    Also, what is this line doing:

    memset(&(myaddr.sin_zero),'\0',8);

    --Marc
    Marc Rochkind Guest

  6. #5

    Default Re: newbie to network programming

    Barry Margolin wrote:
    > In article <xneWa.129$Am1.97@newsfep1-gui.server.ntli.net>,
    Thanks alot, my code now works

    Dave
    > Dave Hughes <donotresus@yahoo.co.uk> wrote:
    >>Hey there,
    >>
    >>I'm having trouble with this very basic network program. All it does is
    >>wait for a connection to my machine on port 4000. THen when it gets a
    >>connection it sends the string Hello World to the caller. I am having
    >>problems in that when the program gets to the send function I get the
    >>error socket operation on nonsocket and I cant work out why this is since
    >>newsocket is an integer, given a value by accept.
    >
    > ...
    >
    >> if (newsocket = accept(mysocket, (struct sockaddr *)&calleraddr,
    >>&sin_size)==-1)
    >
    > Classic error.
    >
    > You're setting newsocket to the result of the == comparison, not to the
    > socket returned by accept(). You need another set of parentheses around
    > the assignment.
    >
    Dave Hughes Guest

  7. #6

    Default Re: newbie to network programming


    "Dave Hughes" <donotresus@yahoo.co.uk> wrote in message
    news:jefWa.141$Am1.62@newsfep1-gui.server.ntli.net...
    > int main(int argc, char *argv[])
    > {
    > int mysocket, newsocket;
    > struct sockaddr_in myaddr;
    > struct sockaddr_in calleraddr;
    > int sin_size;
    > close(mysocket);
    In addition to the other things mentioned, this 'close' uses an
    initialized variable.

    DS


    David Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139