local socket programming problem

local socket programming problem

Post by Mark Cha » Wed, 07 Apr 2004 06:27:50



I have a very simple program to create a local socket shared between 2
processes.

This is the client side, that will open and write the data...
void write_text (int socket_fd, const char* text)
{
  int length = strlen (text) + 1;
  write (socket_fd, &length, sizeof (length));
  write (socket_fd, text, length);

Quote:}

int main (int argc, char* const argv[])
{
  const char* const socket_name = argv[1];
  const char* const message = argv[2];
  int socket_fd;
  struct sockaddr_un name;

  socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  name.sun_family = AF_LOCAL;
  strcpy (name.sun_path, socket_name);
  connect (socket_fd, (struct sockaddr *) &name, SUN_LEN (&name));
  write_text (socket_fd, message);
  close (socket_fd);
  return 0;

Quote:}

This is the server side.

int server (int client_socket)
{
  while (1) {
    int length;
    char* text;

    if (read (client_socket, &length, sizeof (length)) == 0)
      return 0;

    printf("length %d\n",length);
    length = 15; /* prevent code dump */
    text = (char*) malloc (length);
    read (client_socket, text, length);
    printf ("%s\n", text);  
  }

Quote:}

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;

  socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
  name.sun_family = AF_LOCAL;
  strcpy (name.sun_path, socket_name);
  bind (socket_fd, (struct sockaddr *) &name, SUN_LEN (&name));
  listen (socket_fd, 5);

  do {
    struct sockaddr_un client_name;
    socklen_t client_name_len;
    int client_socket_fd;
    client_socket_fd = accept (socket_fd, (struct sockaddr *)
&client_name,  &client_name_len);
    client_sent_quit_message = server (client_socket_fd);
    close (client_socket_fd);
  }
  while (!client_sent_quit_message);
  close (socket_fd);
  unlink (socket_name);
  return 0;

Quote:}

On the server side, the length is ALWAYS retured as a negative number
when the client sends the length.
Even if I force the length, nothing is read for the second read.
Really strange.  Do you think it is my linux setup?
I use /tmp/socket as the device as in

socket-server /tmp/socket and

socket-client /tmp/socket "this is a test"

TIA

 
 
 

local socket programming problem

Post by Jeff Breitne » Wed, 07 Apr 2004 09:31:25



> I have a very simple program to create a local socket shared between 2
> processes.

> This is the server side.

> int server (int client_socket)
> {
>   while (1) {
>     int length;
>     char* text;

>     if (read (client_socket, &length, sizeof (length)) == 0)
>       return 0;

I think you read is bad -- 'man 2 read'

Try something like...

char buffer[BUFFERSIZE]

read(client_socket,buffer,BUFFERSIZE) or you can alloc some memory of
whatever size and sizeof that memory allocation as the third variable to
the read.

Quote:

>     printf("length %d\n",length);

If you want to know the number of bytes read, then do

int bytesread;

if((bytesread=read(foo_a,foo_b,foo_c)) == 0){

Quote:}

Then bytesread will contain an integer of the number of bytes read.  Or,
you could get it from strlen(buffer) provided you null the buffer before
each read.

There are, many ways of doing this.  They released a second edition of
Richard Steven's Unix Sockets programming book not too long ago.  You
might want to pick it up because it's worth every penny.

Quote:>     length = 15; /* prevent code dump */

Just FYI, and I'd take a lot of grief from people who are far far far
better C programmers than I if I didn't point this out...

Hard-sized buffers are ok for experimentation, but never, ever in a
million years use them in production code where this will actually be
exposed to the world.  You're probably aware of this, but it doesn't
hurt to say it.

As well as when you do client-server programming, what you receive from
the client is to be assumed hostile until proven otherwise.

Have fun learning and don't give up.

--

WWJD?  JWRTFM


 
 
 

local socket programming problem

Post by Mark Cha » Thu, 08 Apr 2004 02:06:22




> > I have a very simple program to create a local socket shared between 2
> > processes.

> > This is the server side.

> > int server (int client_socket)
> > {
> >   while (1) {
> >     int length;
> >     char* text;

> >     if (read (client_socket, &length, sizeof (length)) == 0)
> >       return 0;

> I think you read is bad -- 'man 2 read'

> Try something like...

> char buffer[BUFFERSIZE]

> read(client_socket,buffer,BUFFERSIZE) or you can alloc some memory of
> whatever size and sizeof that memory allocation as the third variable to
> the read.

> >     printf("length %d\n",length);

> If you want to know the number of bytes read, then do

> int bytesread;

> if((bytesread=read(foo_a,foo_b,foo_c)) == 0){

> }

> Then bytesread will contain an integer of the number of bytes read.  Or,
> you could get it from strlen(buffer) provided you null the buffer before
> each read.

> There are, many ways of doing this.  They released a second edition of
> Richard Steven's Unix Sockets programming book not too long ago.  You
> might want to pick it up because it's worth every penny.

> >     length = 15; /* prevent code dump */

> Just FYI, and I'd take a lot of grief from people who are far far far
> better C programmers than I if I didn't point this out...

> Hard-sized buffers are ok for experimentation, but never, ever in a
> million years use them in production code where this will actually be
> exposed to the world.  You're probably aware of this, but it doesn't
> hurt to say it.

> As well as when you do client-server programming, what you receive from
> the client is to be assumed hostile until proven otherwise.

> Have fun learning and don't give up.

Thanks for your input.
However, it is not the code.
I ran this on 2 other Linux boxes (one with the same install RH9) and
the programs work fine.  So now I have to figure out what is wrong
with this machine setup.
I'm wondering if because I use a file /tmp/socket that the correct
file permissions have anything to do with it.
But /tmp is r/w is that shouldn't be a problem.  And I'm running this
under root (yeah, yeah I know) but this is an embedded Linux device
only those with physical access can get to it.
However, since the target device runs OK (but not my dev sys) I will
just have to ignore it for now.
Thanks-
 
 
 

local socket programming problem

Post by Jeff Breitne » Thu, 08 Apr 2004 07:46:04



> Thanks for your input.
> However, it is not the code.

This is x86?

Quote:> I ran this on 2 other Linux boxes (one with the same install RH9) and
> the programs work fine.  So now I have to figure out what is wrong
> with this machine setup.
> I'm wondering if because I use a file /tmp/socket that the correct
> file permissions have anything to do with it.

Regardless of the what the file descriptor really is, if you don't have
permissions to it, it's not going to work.

Quote:> But /tmp is r/w is that shouldn't be a problem.  And I'm running this
> under root (yeah, yeah I know) but this is an embedded Linux device
> only those with physical access can get to it.

I wouldn't worry too much about running it as root.  If all you're
interested in is interprocess communication, have you considered a
socket of type AF_UNIX?

Quote:> However, since the target device runs OK (but not my dev sys) I will
> just have to ignore it for now.
> Thanks-

What kind of embedded device is it?

--

WWJD?  JWRTFM

 
 
 

local socket programming problem

Post by Mark Cha » Sun, 11 Apr 2004 05:00:20




> > Thanks for your input.
> > However, it is not the code.

> This is x86?

> > I ran this on 2 other Linux boxes (one with the same install RH9) and
> > the programs work fine.  So now I have to figure out what is wrong
> > with this machine setup.
> > I'm wondering if because I use a file /tmp/socket that the correct
> > file permissions have anything to do with it.

> Regardless of the what the file descriptor really is, if you don't have
> permissions to it, it's not going to work.

> > But /tmp is r/w is that shouldn't be a problem.  And I'm running this
> > under root (yeah, yeah I know) but this is an embedded Linux device
> > only those with physical access can get to it.

> I wouldn't worry too much about running it as root.  If all you're
> interested in is interprocess communication, have you considered a
> socket of type AF_UNIX?

> > However, since the target device runs OK (but not my dev sys) I will
> > just have to ignore it for now.
> > Thanks-

> What kind of embedded device is it?

I thought PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL were all the same.
Yes, this is x86 - the embedded device is for avionics flight computers.