AF_UNIX Socket Programming

A. Dalton garibaldi_cpp@hotmail.com
Mon Aug 21 07:39:00 GMT 2000


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


More information about the Cygwin mailing list

AltStyle によって変換されたページ (->オリジナル) /