Network Programming C / C++ question..

This is a discussion on Network Programming C / C++ question.. within the Networking/Device Communication forums, part of the General Programming Boards category; Hey guys, I'm really new to network programming and cant seem to get this program to work. #include <string.h> #include ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Network Programming C / C++ question..

    Hey guys, I'm really new to network programming and cant seem to get this program to work.

    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    #define DEST_IP "10.12.110.57"
    #define DEST_PORT 23

    main()
    {
    int sockfd;
    struct sockaddr_in dest_addr; // will hold the destination addr

    sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!

    dest_addr.sin_family = AF_INET; // host byte order
    dest_addr.sin_port = htons(DEST_PORT); // short, network byte order
    dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
    memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct

    // don't forget to error check the connect()!
    connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
    }
    I tried writing this into pico, on a UNIX server at school and used a gcc compiler. I get the following error:


    Undefined First referenced
    symbol in file
    __xnet_connect /var/tmp//ccC0mTbw.o
    __xnet_socket /var/tmp//ccC0mTbw.o
    __gxxpersonality_v0 /var/tmp//ccC0mTbw.o
    inet_addr /var/tmp//ccC0mTbw.o
    id: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status



    I'm really not sure what I'm doing wrong here. btw, this is directly writen from http://beej.us/guide/bgnet/output/html/syscalls.html ... I'm not taking credit for this persons work, just my way of learning is retyping it myself while explaining it to myself. Any help on why this isn't working would be great.

    Love forums!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'll need to link with the required libraries: http://beej.us/guide/bgnet/output/html/intro.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    am I not already linked to the correct header files?

    ... could you please elaborate? I'm still confused.

    thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > gxxpersonality_v0
    Everytime I've seen this, it meant I used a C compiler to compile a C++ program.

    For C programs
    gcc prog.c

    For C++ programs
    g++ prog.cpp

    > on a UNIX server at school
    Which sort of UNIX - Solaris, Linux, one of several BSD variants?
    http://beej.us/guide/bgnet/output/ht....html#platform
    You need to figure out what the network libraries on your system are called, so you can have a command line like
    cc -o server server.c -lnsl -lsocket -lresolv

    > C / C++ question..
    I can see why - you seem to think the languages are roughly the same, or at least highly interchangeable.
    They are not. C/C++ is just jaywalking down the middle of road between traffic moving in opposite directions. Sooner or later, it's going to get ugly.
    http://david.tribble.com/text/cdiffs.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    thanks salem,

    I compiled it through
    g++ -o server ntwk.cpp -lnsl -lsocket -lresolv
    and it seems to have worked.

    But, being a rather large newbie to this I am confused on if it really worked or not. There is a new file in my directory called "server" which I assume is the program in an executable form of some type?

    If I type "server" and press enter, nothing happens. I added a simple printf command to let me know the program is running, and even still, when I type in server.. nothing happens. I must have done something wrong?

    btw.. I think the school server is linux ;\

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    > -o server
    Yes, this is just the name of the program - you can pick any name you like.

    Typically, you run programs by typing in say
    ./server

    If you miss the ./ bit, then the shell searches the PATH environment variable for where the program is stored, and executes some system program with the same name, or reports that it cannot be found.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    ahh Salem, thankyou very much. I have gotten the program to work and although it doesn't connect to my computer I know its compiled! You are indeed God of the C :P

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    Path Issue

    Hi.

    It sounds like you ran into a path issue. Under Linux (and other UN*X's) the current directory (named '.') is not explicitly in your path unless you set your $PATH environment variable to include '.'.

    As a result, when you ran 'server' it was loading and running another application that happened to also be called 'server'. By using './server' you are specifically loading the app in your current directory.

    Steve

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Just so you all know, having '.' in your PATH is a big security problem waiting to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    Hi.

    I'm curious (though not challenging) why that in itself would be a security risk. Is the concern that you could 'replace' a system command that gets called through a script somewhere with a malicios one?

    Steve

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's one issue. Another is that you might simply execute an unexpected program from the command line. Imagine this scenario. A careless admin has PATH set to ".:/sbin: ...". i.e. local programs take preference over system programs.

    holeserver is a daemon that provides some network services and operates as a non-privileged user. Through a security hole, an attacker manages to gain write and chmod access to an unimportant directory. In there, he plants an executable called "ls" and makes it executable.

    The admin, logged in as root and browsing the logs, finds some strange entries from holeserver. He decides to check it out and does a cd /var/lib/holeserver/unimportant, the directory the attacker gained access to.
    Then he types "ls".


    He gets a proper ls output, except that it does not contain the ls file. But the evil executable also just installed a root kit - run by root, it has unlimited access to do what it wants.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. windows network programming noob question
    By mramazing in forum Networking/Device Communication
    Replies: 6
    Last Post: 01-27-2009, 03:20 PM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21