Networking Forums

Networking Forums > Computer Networking > Linux Networking > calling connect on UNIXDomain dgram socket failes

Reply
Thread Tools Display Modes

calling connect on UNIXDomain dgram socket failes

 
 
oliver.kowalke@gmx.de
Guest
Posts: n/a

 
      11-25-2005, 07:38 AM
Hello,
I want to connect the UNIXDomain datagram socket (client -> address
should be bound to dgram socket) - the server doesn't get a correct
peer address (sun_family and sun_path have no valid value after calling
recvfrom).
so sending a message back to the peer will fail.

For UDP sockets it works - is it not supported by UNIXDomain sockets?

with best regards,
Oliver

#include <iostream>
#include <cstdlib>
#include <stdexcept>

#include <sys/un.h>
#include <sys/socket.h>

int main( int argc, char *argv[])
{
try
{
if ( argc > 1)
{
sockaddr_un un;

un.sun_family = PF_LOCAL;
::strncpy(
un.sun_path,
"/tmp/ud_server",
sizeof(un.sun_path) - 1);

int hndl = -1;

if ( ( hndl = ::socket( PF_LOCAL, SOCK_DGRAM, 0) ) < 0)
throw std::runtime_error( ::strerror( errno) );

if ( ::bind(
hndl,
reinterpret_cast< sockaddr * >( & un),
sizeof un) < 0)
throw std::runtime_error( ::strerror( errno) );

while ( true)
{
sockaddr_un peer;
char buf[1024];
socklen_t len;
int n = 0;
if ( ( n = ::recvfrom(
hndl,
buf,
sizeof buf,
0,
reinterpret_cast< sockaddr * >(
& peer),
& len) ) < 0)
throw std::runtime_error( ::strerror( errno) );

std::cout << "PF_LOCAL = " << peer.sun_family << std::endl; // is 0
std::cout << "path = " << peer.sun_path << std::endl; // zero

if ( ::sendto(
hndl,
buf,
n,
0,
reinterpret_cast< sockaddr * >(
& peer),
sizeof peer) < 0)
throw std::runtime_error( ::strerror( errno) );
}

}
else
{

sockaddr_un un;

un.sun_family = PF_LOCAL;
::strncpy(
un.sun_path,
"/tmp/ud_server",
sizeof(un.sun_path) - 1);

int hndl = -1;

if ( ( hndl = ::socket( PF_LOCAL, SOCK_DGRAM, 0) ) < 0)
throw std::runtime_error( ::strerror( errno) );

if ( ::connect( hndl, reinterpret_cast< sockaddr * >( & un), sizeof
un) < 0)
throw std::runtime_error( ::strerror( errno) );

if ( ::write( hndl, "ABC", 3) < 0)
throw std::runtime_error( ::strerror( errno) );

char buf[1024];
int n = 0;
if ( ( n = ::read( hndl, buf, 3) ) < 0)
throw std::runtime_error( ::strerror( errno) );

std::cout << std::string( buf, 0, n) << std::endl;
}

return EXIT_SUCCESS;
}
catch ( std::exception const& e)
{ std::cerr << e.what() << std::endl; }
catch (...)
{ std::cerr << "unhandled exception" << std::endl; }
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raid Failes?? Drive works fine? paulb787 Wireless Internet 1 01-09-2008 12:43 PM
Calling Listen on UDP Socket siva Linux Networking 1 06-21-2007 04:45 PM
Newbie:Calling Socket.BeginSend inside SendCallback Navin Mishra Windows Networking 0 04-25-2007 05:37 PM
Service name WSCSVC failes to start part 2 Dag N Wireless Networks 1 03-19-2007 01:08 PM
Service name WSCSVC failes to start Dag N Wireless Networks 2 03-16-2007 12:53 PM



1 2 3 4 5 6 7 8 9 10 11