libassa  3.5.1
UDPSocket.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // UDPSocket.C
4 //------------------------------------------------------------------------------
5 // Copyright (c) 2000 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 //------------------------------------------------------------------------------
13 // Created: 03/22/99
14 //------------------------------------------------------------------------------
15 
16 #include "assa/UDPSocket.h"
17 
18 using namespace ASSA;
19 
20 bool
22 open (const int domain_)
23 {
24  trace("UDPSocket::open");
25 
26  m_type = domain_;
27  m_fd = ::socket (m_type, SOCK_DGRAM, 0);
28 
29  if (m_fd < 0) {
31  return false;
32  }
33  clear ();
34  return true;
35 }
36 
37 bool
39 close ()
40 {
41  trace("UDPSocket::close()");
42  if ( m_fd >= 0 ) {
43  ::close(m_fd);
45  m_fd = -1;
46  }
47  return true;
48 }
49 
50 bool
52 bind (const Address& my_address_)
53 {
54  trace("UDPSocket::bind");
55 
56  int ret = ::bind (m_fd, (SA*) my_address_.getAddress(),
57  my_address_.getLength());
58  if (ret < 0) {
60  return false;
61  }
62  return true;
63 }
64 
65 
66 
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
Class UDPSocket is an implementation of UNIX domain socket that is the base class for more specialize...
virtual SA * getAddress() const =0
Retrieve pointer to the address structure.
virtual const int getLength() const =0
Return length of the underlying address structure.
int m_type
Socket domain type.
Definition: Socket.h:488
void setstate(iostate flag_)
Set socket state to flag_ by adding flag_ to the existing state.
Definition: Socket.h:577
handler_t m_fd
File descriptor.
Definition: Socket.h:485
void clear(iostate state_=Socket::goodbit)
Clear the socket state. Closed socket remains in bad state.
Definition: Socket.h:571
@ failbit
indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: Socket.h:84
bool open(const int domain_)
Create socket.
Definition: UDPSocket.cpp:22
bool bind(const Address &my_address_)
Server in UDP client-server scenario has to bind socket to its local well-known port.
Definition: UDPSocket.cpp:52
bool close()
Close socket connection.
Definition: UDPSocket.cpp:39
Definition: Acceptor.h:40
struct sockaddr SA
Definition: Address.h:33