C-chat
Classes | Typedefs | Functions
sockets.h File Reference

API for sockets. More...

Go to the source code of this file.

Classes

struct  Socket
 Information of a socket. More...
 

Typedefs

typedef struct Socket Socket
 

Functions

Socket createClientSocket (const char *ipAddress, const char *port)
 Creates a socket meant to be used to connect to a server socket and initialize a connection with the server. More...
 
Socket createServerSocket (const char *port)
 Creates a socket meant to receive connection from clients sockets. More...
 
Socket acceptClient (Socket serverSocket)
 Waits for a client to connect to the given server socket. Returns the socket for the connected client. More...
 
int receiveFrom (Socket clientSocket, char *buffer, unsigned int bufferSize)
 Receives through from the given socket. More...
 
int sendTo (Socket clientSocket, const char *buffer, unsigned int bufferSize)
 Sends data through the given socket. More...
 
void closeSocket (Socket *socket)
 Closes the given socket. More...
 
void cleanUp ()
 Performs any required resources cleanup. Must be called once all sockets usage is finished.
 

Detailed Description

API for sockets.

A general API to manipulate sockets.

Function Documentation

◆ acceptClient()

Socket acceptClient ( Socket  serverSocket)

Waits for a client to connect to the given server socket. Returns the socket for the connected client.

Parameters
serverSocketThe server socket
Returns
a socket to the accepted client

◆ closeSocket()

void closeSocket ( Socket socket)

Closes the given socket.

Parameters
socketThe a pointer to the socket to close

◆ createClientSocket()

Socket createClientSocket ( const char *  ipAddress,
const char *  port 
)

Creates a socket meant to be used to connect to a server socket and initialize a connection with the server.

Parameters
ipAddressThe IP address of the server to connect to
portThe port to connect on
Returns
a ready-to-use client socket

◆ createServerSocket()

Socket createServerSocket ( const char *  port)

Creates a socket meant to receive connection from clients sockets.

Parameters
portThe port the server must listen on
Returns
a ready-to-use server socket

◆ receiveFrom()

int receiveFrom ( Socket  clientSocket,
char *  buffer,
unsigned int  bufferSize 
)

Receives through from the given socket.

Parameters
clientSocketThe socket to receive data from
bufferA buffer to store received data in
bufferSizeThe maximum size of data to receive
Returns
the number of bytes received or -1 if an error occurred

◆ sendTo()

int sendTo ( Socket  clientSocket,
const char *  buffer,
unsigned int  bufferSize 
)

Sends data through the given socket.

Parameters
clientSocketThe socket to send a message through
bufferA buffer containing to data to send
bufferSizeThe size of the data to send
Returns
the number of bytes sent or -1 if an error occurred