Thread: problem in this code?

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    problem in this code?

    What is wrong with this code? i get some errors like:
    50:warning passing argument of 'bind' from imcopatible pointer type.
    60:warning passing argument of 'accept' from imcopatible pointer type.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <unistd.h>
    /* Read text from the socket and print it out. Continue until the
    socket closes. Return nonzero if the client sent a “quit”
    message, zero otherwise. */
    int server (int client_socket)
    {
    while (1) {
    int length;
    char* text;
    /* First, read the length of the text message from the socket. If
    read returns zero, the client closed the connection. */
    if (read (client_socket, &length, sizeof (length)) == 0)
    return 0;
    /* Allocate a buffer to hold the text. */
    text = (char*) malloc (length);
    /* Read the text itself, and print it. */
    read (client_socket, text, length);
    printf ("%s\n", text);
    /* Free the buffer. */
    free (text);
    /* If the client sent the message “quit,” we’re all done. */
    if (!strcmp (text, "quit"))
    return 1;
    }
    }
    int main (int argc, char* const argv[])
    {
    const char* const socket_name = argv[1];
    	int socket_fd;
    struct sockaddr_un name;
    int client_sent_quit_message;
    /* Create the socket. */
    socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
    /* Indicate that this is a server. */
    name.sun_family = AF_LOCAL;
    strcpy (name.sun_path, socket_name);
    bind (socket_fd, &name, SUN_LEN (&name));
    /* Listen for connections. */
    listen (socket_fd, 5);
    /* Repeatedly accept connections, spinning off one server() to deal
    with each client. Continue until a client sends a “quit” message. */
    do {
    struct sockaddr_un client_name;
    socklen_t client_name_len;
    int client_socket_fd;
    /* Accept a connection. */
    client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
    /* Handle the connection. */
    client_sent_quit_message = server (client_socket_fd);
    /* Close our end of the connection. */
    close (client_socket_fd);
    }
    while (!client_sent_quit_message);
    /* Remove the socket file. */
    close (socket_fd);
    unlink (socket_name);
    return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Jebus format you code!

    Also, mark in like red where those warnings are poping up, I don't like to have to copy and paste code into one of my editors to get line numbers.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The 2nd parameter of bind is a pointer to sockaddr structure, but you give a pointer to a sockaddr_un structure. And the accept's 2nd parameter is a pointer to the addr structure inside the sockaddr structure not the sockaddr_un structure.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Agghhh...Do you know what a TAB key is? If so, try to use it when writing your code!

    You can read the following man pages for the usage of bind(2) and accept(2).

    Hint : have a close look at the types of parameters and descriptions of parameters.

    http://man.linuxquestions.org/?query...ction=2&type=2
    http://man.linuxquestions.org/?query...ction=0&type=2

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    http://cboard.cprogramming.com/showthread.php?t=83899
    I thought you were doing a simulation of a network program, not an actual network program.

    Of course, prior to your previous message, it was an actual network program as well.
    http://cboard.cprogramming.com/showthread.php?t=83877

    You really need to clarify what it is you're doing.
    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.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Salem
    I thought you were doing a simulation of a network program, not an actual network program.
    What's the difference?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One is a lot more work than the other?
    One doesn't require a network card?
    What else?
    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.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I just wanted to know what simulating a "client server" application means...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I just wanted to know what simulating a "client server" application means...
    An exercise in using queues, as per my reply to one of the other threads.
    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.

  10. #10
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Wink Hi everyone

    What iam doing is a simulating program. I found this example in a text book and i was trying it on running it but it didint work. But i dont really have to do an actual program that runs on a network. I got this example to see if it was helpfull. I need to do a simulatin program between clients and server this means is not going to be happening in a real network, but is just going to be a function for client and some code for server and i have to appy a queue as Salem mention in my previous thread. Iam in that process right now, thanks for your help.
    Wise_ron
    One man's constant is another man's variable

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM