Advertisement

Help with C++ sockets and networking

Started by March 03, 2004 06:21 PM
1 comment, last by Promit 20 years, 11 months ago
I have some C++ socket classes (not written by me) and I want to learn how to use them. I know the guy who wrote these things pretty well, but he''s very busy and it''ll be a miracle if he ever gets time to explain it all to me In any case, I need to get my bearings here... So I have a couple classes that abstract raw sockets a little bit, they''re cross-platform and all that jazz. I''m gonna start by just posting the class interfaces for the sockets: Socket.h

class Socket {
public:
  Socket( );
  Socket( ps_socket_t );
  virtual ~Socket( );

  bool create( );

  class ByteArray receive( ssize_t );
  bool send( const class ByteArray& );
  bool send( const std::string& );
  // XXX does not act like other send methods

  void send( const char c) {
    ::send(this->_socket, &c, 1, 0);
  };
  
  bool close( );

  ps_socket_t fd( ) const;

protected:

  ps_socket_t _socket;
};
ClientSocket.h

class ClientSocket : public Socket {
public:
  ClientSocket( );
  virtual ~ClientSocket( );

  bool open( string , unsigned int );
};
ServerSocket.h

class ServerSocket {
public:
  ServerSocket( );
  virtual ~ServerSocket( );

  bool bind( unsigned int );
  unsigned int acceptPending( );

  class Socket* client( unsigned int );
  unsigned int numClients( ) const;

protected:
  class Socket _listen_socket;
  Array< class Socket* > _client_sockets;

};
Things of note: Array, ByteArray, etc. are data structures in use. They abstract fairly fundamental data structures and are of little importance here. ps_socket_t is an abstraction of the raw socket. On Windows this is a typedef for SOCKET, on Unix it is a typedef for int. The basic problem is this: I have no idea how to use these classes. I don''t understand networking too well in itself. I do know that these classes function as they''re supposed to, since they''ve been tested. What I want to is write maybe a simple chat program with these things, just so I can figure out how to use them before I start working with them in our actual project. This would be a client/server architecture, not p2p. I really don''t know if I''ve posted enough details or not; I can post the implementations if you need them, as this is a GPL project. Basic info that I can provide: Socket::create uses the socket() function Socket::receive uses the recv() function Socket::send uses the send() function Socket::close uses the close() function ClientSocket::open uses the connect() function ServerSocket::bind uses the bind() function (no idea what that does) ServerSocket::acceptPending uses select() and accept() (no idea what they do either) Again, I can provide more info if you need it, but I just don''t know how this stuff works or exactly what it is I''m supposed to do to start a server, take client connections, etc. Any help would be greatly appreciated.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
In either case you need to call create() to allocate a socket, and close() (or closesocket(), not sure which it is offhand) to free it.

For a client socket, you want to connect to an address. Thats what the connect() function does.

For a server socket, its slightly more complex. First of all, you bind() to an address (port really), to tell the OS that anything comming in on this port, send to me (connect() does this automatically). Then you call listen(). listen() tells the OS that other people can connect to your computer on the port it was bind()ed to.
Then, you call accept() to accept a new connection. The accept() function blocks until a connection arrives, so you need to use it in a thread, use WSAAccept() (evil evil evil), or call select() to see if theres connections waiting (select() just checks the status of a socket and returns immediately).
After you''ve accept()ed, you get a new socket. This socket is the connection to the other computer. You use it to send or recieve data.

So, the client creates the socket and then connect()s, and the server creates a listening socket and then accept()s another new socket.

So, a client program will usually have 1 socket - the one that connects to the server, whereas a server program will have 1 socket listening for connections, and then additional 1 socket for every connected user.

If you want yet another socket class to look at, you can get my one from Here. It uses select(), and compiles and runs fine under windows and linux (red hat 8 and 9).
Advertisement
Thanks.

The above classes do all that stuff already, but now I understand what is going on, at any rate. I''ve got a shoddy sort of chat thing going to, so I can use these things.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement