LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2003, 09:23 AM   #1
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Rep: Reputation: 15
Question Sockets - The Error That Shouldn't Be There


Hey, this is my first post... yay for me. I've been programming c++ for a couple years now and programming for a little bit longer. Recently, I've been learning out of this book, "Advanced Linux Programming," created by Code Sourcery. Now, it gives an example of creating, using and destroying sockets. I copied the source code completely, I even tried downloading the source code off of their website. But, I get these errors:



Code:
socket-server.cpp: In function `int main(int, char* const*)':

socket-server.cpp:75: cannot convert `sockaddr_un*' to `const sockaddr*' for
   argument `2' to `int bind(int, const sockaddr*, unsigned int)'

socket-server.cpp:97: cannot convert `sockaddr_un' to `sockaddr*' for argument
   `2' to `int accept(int, sockaddr*, socklen_t*)'


And here's the source code:


Code:
//socket-server.cpp
//Server of socket test

#include <iostream.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.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);
      cout << text << endl;


      // 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;
}
 
Old 10-18-2003, 11:34 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Casting to struct sockaddr* is needed for the calls to bind() and accept().
Need to change those lines to :
Code:
bind(socket_fd, (struct sockaddr *) &name, SUN_LEN(&name));

      [..snip..]

client_socket_fd=accept(socket_fd, (struct sockaddr *) &client_name, &client_name_len);
I also downloaded the source from the book. Indeed it has the same problem.

<edit>
I bought the book, and I've sent a error-report about this to the publisher.
</edit>

Last edited by Hko; 10-18-2003 at 12:08 PM.
 
Old 10-18-2003, 12:47 PM   #3
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
Cool, thanks.
 
Old 10-19-2003, 09:05 PM   #4
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
...

I get a segmenation fault when I attempt to compile it.

Oh, and sorry it took so long to reply, I ended up ripping up the carpet in my room t'day. Turns out theres some pretty good wood floor down there. Anyhoo, I actually get segmentation faults for a couple programs I attempt to run. Might this problem be in how I execute it, how I compile it?

I generally compile using: c++ x.cpp -o x. and then i run it by doing ./x in terminal. That right?
 
Old 10-20-2003, 02:22 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Code:
text=(char*)malloc(length);

[..snip..]

read(client_socket, text, length);
When these lines execute, the length variable isn't assigned a value yet! So length will contain a random value. That probably the cause of segfaulting. I suppose it is not like this in the original code from the book.
 
Old 10-20-2003, 01:29 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I got a reply from one of the authors (Jeffrey D. Oldham). He has put it on the errata page on
http://www.advancedlinuxprogramming.com/errata.html
(page 121, 122 and 125)
 
Old 10-20-2003, 03:32 PM   #7
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
does yours compile even when you give it a length? do you get any other problems?
 
Old 10-20-2003, 03:38 PM   #8
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
sry...

I'm really stupid, sorry. I forgot to use arguments when executing the program... It works now. Cool, so thanks.
 
Old 10-20-2003, 06:55 PM   #9
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
nvm, it runs, but the code just loops forever, I've been trying to debug it all afternoon. Is there something that I should change?
 
Old 10-20-2003, 07:21 PM   #10
drdroid
Member
 
Registered: Oct 2003
Location: Here
Distribution: Redhat 9.0
Posts: 124

Original Poster
Rep: Reputation: 15
omg, nevermind yet again i solved it... anyhoo, thanks for putting up with this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C(sockets): 'Invalid argument' error when using MSG_OOB elluva Programming 6 06-08-2005 09:16 AM
c and sockets Matir Programming 4 03-09-2005 04:15 PM
Sockets on RH 9.0 rjs2006 Linux - Newbie 1 01-17-2005 10:16 PM
PCMCIA - No sockets error chazco Linux - Laptop and Netbook 1 03-22-2004 06:39 PM
MySQL sockets error Amerist Linux - Software 3 09-14-2002 05:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration