This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: AF_UNIX Socket Programming


Thank you very much.  That worked quite well.  I'm kicking myself for not 
seeing that.

Again, thanks for your time and assistance.

Andy


>From: "Addison, Darrick" <DARRICK.ADDISON@saic.com>
>To: "'A. Dalton'" <garibaldi_cpp@hotmail.com>
>Subject: RE: AF_UNIX Socket Programming
>Date: Mon, 21 Aug 2000 11:17:00 -0400
>
>Try this modified version of Server.cpp.
>
>Darrick
>
>//**********************************************************************
>
>//
>// Server.cpp
>//
>#include <sys/types.h>
>#include <sys/socket.h>
>#include <sys/un.h>
>#include <unistd.h>
>#include <stdio.h>
>
>int main()
>{
>     int         server_sockfd;
>     int         client_sockfd;
>     int         server_len;
>     int         client_len;
>     sockaddr_un server_address;
>     sockaddr_un client_address;
>
>     unlink("server_socket");
>     server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
>
>     server_address.sun_family = AF_UNIX;
>     strcpy(server_address.sun_path, "server_socket");
>     server_len = sizeof(server_address);
>     client_len = sizeof(client_address); //get byte length of client 
>address
>     bind(server_sockfd, (sockaddr*)&server_address, server_len);
>
>     listen(server_sockfd, 5);
>     for(;;)  // Loop forever
>     {
>         char ch;
>
>         // printf("server waiting\n");
>
>         client_sockfd = accept(server_sockfd,
>                         (sockaddr*)&client_address,
>                         &client_len);
>
>         read(client_sockfd, &ch, 1);
>         ++ch;
>         write(client_sockfd, &ch, 1);
>         close(client_sockfd);
>     }
>}
>
>
> > -----Original Message-----
> > From:	A. Dalton [SMTP:garibaldi_cpp@hotmail.com]
> > Sent:	Monday, August 21, 2000 10:39 AM
> > To:	cygwin@sources.redhat.com
> > Subject:	AF_UNIX Socket Programming
> >
> > Greetings,
> >
> > I have been able to install Cygwin (thanks to the nice little setup
> > program).  Now I am trying to do some basic AF_UNIX socket programming.  
>I
> >
> > have copied an example from the following URL:
> >
> > 
>http://www.informatik.hu-berlin.de/~mueller/dsm/www.wrox.com/books/680/680
> > .html
> >
> > In this example, a server and a client that communicate via a socket.
> > Everything compiles correctly but when I run it, nothing happens.  It
> > appears that the accept() in the server fails.  Whenever I include the
> > printf() for "server waiting," I see that I'm in a hard loop.  The
> > accept()
> > doesn't seem to block.  The exact same code works in Linux.  Here's the
> > code
> > I'm using:
> >
> > //**********************************************************************
> >
> > //
> > // Server.cpp
> > //
> > #include <sys/types.h>
> > #include <sys/socket.h>
> > #include <sys/un.h>
> > #include <unistd.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> >     int         server_sockfd;
> >     int         client_sockfd;
> >     int         server_len;
> >     int         client_len;
> >     sockaddr_un server_address;
> >     sockaddr_un client_address;
> >
> >     unlink("server_socket");
> >     server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
> >
> >     server_address.sun_family = AF_UNIX;
> >     strcpy(server_address.sun_path, "server_socket");
> >     server_len = sizeof(server_address);
> >     client_len = sizeof(client_address); //get byte length of client
> > address
> >     bind(server_sockfd, (sockaddr*)&server_address, server_len);
> >
> >     listen(server_sockfd, 5);
> >     for(;;)  // Loop forever
> >     {
> >         char ch;
> >
> >         // printf("server waiting\n");
> >
> >         client_sockfd = accept(server_sockfd,
> >                         (sockaddr*)&client_address,
> >                         &client_len);
> >
> >         read(client_sockfd, &ch, 1);
> >         ++ch;
> >         write(client_sockfd, &ch, 1);
> >         close(client_sockfd);
> >     }
> > }
> >
> > //**********************************************************************
> >
> > //
> > // Client.cpp
> > //
> > #include <sys/types.h>
> > #include <sys/socket.h>
> > #include <sys/un.h>
> > #include <unistd.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> >     int         sockfd;
> >     int         len;
> >     sockaddr_un address;
> >     int         result;
> >     char        ch       = 'A';
> >
> >     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
> >
> >     address.sun_family = AF_UNIX;
> >     strcpy(address.sun_path, "server_socket");
> >     len = sizeof(address);
> >
> >     result = connect(sockfd, (sockaddr*)&address, len);
> >
> >     if(result == -1)
> >     {
> >         perror("oops:  client1");
> >         exit(1);
> >     }
> >
> >     write(sockfd, &ch, 1);
> >     read(sockfd, &ch, 1);
> >     printf("char from server = %c\n", ch);
> >     close(sockfd);
> >     exit(0);
> > }
> >
> > //**********************************************************************
> >
> > ad24482@GARIBALDI ~
> > $ g++ -Wall -o server server.cpp
> >
> > ad24482@GARIBALDI ~
> > $ g++ -Wall -o client client.cpp
> >
> > ad24482@GARIBALDI ~
> > $ server&
> > [1] 1015
> >
> > ad24482@GARIBALDI ~
> > $ client
> >
> > // In theory, I should see "char from server = B" here
> >
> > The server does seem to create the socket file:
> >
> > ad24482@GARIBALDI ~
> > $ ls -lap server_socket
> > srw-r--r--   1 ad24482  unknown        15 Aug 21 10:30 server_socket=
> >
> > ad24482@GARIBALDI ~
> > $ g++ --version
> > 2.95.2
> >
> > ad24482@GARIBALDI ~
> > $
> >
> >
> > If anyone has any idea what is wrong, I could really use some help.
> > Thanks
> > in advance for your time.
> >
> > Andy
> > ________________________________________________________________________
> > Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
> >
> >
> > --
> > Want to unsubscribe from this list?
> > Send a message to cygwin-unsubscribe@sourceware.cygnus.com
> >
>

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]