* RE: AF_UNIX Socket Programming
@ 2000-08-21 14:51 A. Dalton
0 siblings, 0 replies; 2+ messages in thread
From: A. Dalton @ 2000-08-21 14:51 UTC (permalink / raw)
To: cygwin
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* AF_UNIX Socket Programming
@ 2000-08-21 7:39 A. Dalton
0 siblings, 0 replies; 2+ messages in thread
From: A. Dalton @ 2000-08-21 7:39 UTC (permalink / raw)
To: cygwin
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);
bind(server_sockfd, (sockaddr*)&server_address, server_len);
listen(server_sockfd, 5);
while(true)
{
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-08-21 14:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-21 14:51 AF_UNIX Socket Programming A. Dalton
-- strict thread matches above, loose matches on Subject: below --
2000-08-21 7:39 A. Dalton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).