This C++ Socket Wrapper Library provides a simple and efficient interface for creating and managing sockets in C++. It encapsulates core socket operations, such as creation, binding, listening, connecting, sending, and receiving, into a Socket class. Additionally, it introduces two derived classes, ServerSocket and ClientSocket, to simplify server and client implementations.
- Socket Management: Abstracts low-level socket creation and configuration.
- Server-Specific Operations: Simplified setup for servers, including binding, listening, and accepting connections.
- Client-Specific Operations: Streamlined connection setup for clients.
- Advanced Socket Options: Support for non-blocking mode, buffer sizing, and advanced options like keep-alive and linger.
-
Prerequisites
- C++ 11 or later
-
Clone the Repository
git clone https://github.com/jiafie7/socket.git
cd socket
- Example Code
- server_1.cpp
#include "socket/server_socket.h"
using namespace melon::socket;
int main()
{
Singleton<LogSystem>::getInstance()->open("./../server_1.log");
Singleton<LogSystem>::getInstance()->setConsole(false);
ServerSocket server("127.0.0.1", 7777);
while (true)
{
int conn_fd = server.accept();
Socket client(conn_fd);
char buf[1024] = {0};
client.recv(buf, sizeof(buf));
printf("recv: %s\n", buf);
std::string msg = "hi, I am server!";
client.send(msg.c_str(), msg.size());
}
return 0;
}
- client_1.cpp
#include "socket/client_socket.h"
using namespace melon::socket;
int main()
{
Singleton<LogSystem>::getInstance()->open("./../client_1.log");
Singleton<LogSystem>::getInstance()->setConsole(false);
ClientSocket client("127.0.0.1", 7777);
std::string msg = "hi, I am client!";
client.send(msg.c_str(), msg.size());
char buf[1024] = {0};
client.recv(buf, sizeof(buf));
printf("recv: %s", buf);
return 0;
}
- Build the Project
mkdir build
cd build
cmake ..
make
./server_1
./client_1
- Output
- server_1
recv: hi, I am client!
- client_1
recv: hi, I am server!
Socket()
: Constructor that initializes a socket.Socket(int socket_fd)
: Constructor that wraps an existing socket descriptor.~Socket()
: Destructor that closes the socket.bool bind(const std::string& ip, int port)
: Binds the socket to a specific IP and port.bool listen(int backlog)
: Listens for incoming connections with a backlog limit.bool connect(const std::string& ip, int port)
: Connects the socket to a server.int accept()
: Accepts a connection from a client.int send(const char* buf, int len)
: Sends data over the socket.int recv(char* buf, int len)
: Receives data from the socket.void close()
: Closes the socket.bool setNonBlocking()
: Sets the socket to non-blocking mode.bool setSendBuffer(int size)
: Configures the send buffer size.bool setRecvBuffer(int size)
: Configures the receive buffer size.bool setLinger(bool active, int seconds)
: Configures linger options for the socket.bool setKeepAlive()
: Enables the keep-alive option.bool setReuseAddr()
: Enables the reuse address option.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions, bug reports, and feature requests are welcome. Feel free to fork the repository, open issues, or submit pull requests.