Results 1 to 2 of 2

Thread: sockets problem

  1. #1

    Default sockets problem

    I don't know it is a correct subgroup. If no could I ask admin to move to correct.
    I have two programs:
    server.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <unistd.h>

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

    if(read(client,&length,sizeof(length))==0)
    return 0;
    text=(char*)malloc(length);

    read(client,text,length);

    printf("%s\n",text);

    free(text);

    if(!strcmp(text,"quit"))
    return 1;
    }
    }

    int main(int argc, char **argv)
    {
    const char* const sname=argv[1];
    int socketid;
    struct sockaddr_un name;
    int quit_sent;

    socketid=socket(PF_LOCAL,SOCK_STREAM,0);
    name.sun_family=AF_LOCAL;
    strcpy(name.sun_path,sname);
    bind(socketid,(struct sockaddr*)&name,SUN_LEN(&name));
    listen(socketid,5);
    do
    {
    struct sockaddr_un cname;
    socklen_t cnamel;
    int csocketid;

    csocketid=accept(socketid,(struct sockaddr*)&cname,&cnamel);
    quit_sent=server(csocketid);
    close(csocketid);
    } while(!quit_sent);

    close(socketid);
    unlink(sname);

    return 0;
    }

    and client.c
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/un.h>
    #include <unistd.h>

    void write_text(int sid, const char *napis)
    {
    int length=strlen(napis)+1;
    write(sid,&length,sizeof(length));
    write(sid,napis,length);
    }

    int main(int argc, char **argv)
    {
    const char* const sn=argv[1];
    const char* const msg=argv[2];
    int sid;
    struct sockaddr_un name;

    sid=socket(PF_LOCAL,SOCK_STREAM,0);
    name.sun_family=AF_LOCAL;
    strcpy(name.sun_path,sn);
    connect(sid,(struct sockaddr*) &name,SUN_LEN(&name));
    write_text(sid,msg);
    close(sid);

    return 0;
    }

    We can run server and via client sent text to server (yes it's trivial but I'm learning it).
    We can run on slackware, redhat, debian. But not on OpenSuse. Why? What have I to change to run it on SUSE?

  2. #2
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: sockets problem

    On 2011-03-08 21:06, mironoza wrote:
    >
    > I don't know it is a correct subgroup. If no could I ask admin to move
    > to correct.


    No, as there is a programming subforum.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.2 x86_64 "Emerald" at Telcontar)

Posting Permissions

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