ecsnet
Loading...
Searching...
No Matches
net_socket.h
1#pragma once
2#include <stdint.h>
3#include <stdbool.h>
4
5#include "config.h"
6#ifdef _WIN32
7#include <winsock2.h>
8#include <ws2tcpip.h>
9#else
10#include <sys/socket.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#endif
14#ifdef __cplusplus
15extern "C" {
16#endif
20typedef enum
21{
22 SOCKET_TYPE_TCP,
23 SOCKET_TYPE_UDP,
24 PROTOCOL_COUNT
25} socket_type_t;
26
32typedef struct
33{
34 struct sockaddr_in addr;
35 socket_type_t type;
36 int fd;
38
44net_socket_t net_socket_create(socket_type_t type);
45
51int net_socket_set_non_blocking(net_socket_t* socket);
52
60int net_socket_connect(net_socket_t* socket, char* ip, uint16_t port);
61
69int net_socket_send(net_socket_t* socket, const void* data, int len);
70
79int net_socket_sendto(net_socket_t* socket, const void* data, int len, const struct sockaddr_in* addr);
80
88int net_socket_receive(net_socket_t* socket, void* buffer, int max_len);
89
98int net_socket_receive_from(net_socket_t* socket, void* buffer, int max_len, struct sockaddr_in* sender_addr);
99
107int net_socket_bind(net_socket_t* socket, const char* ip, uint16_t port);
108
119uint16_t net_socket_get_local_port(const net_socket_t* s);
126int net_socket_listen(net_socket_t* socket, int backlog);
127
133int net_socket_close(net_socket_t* socket);
134
141ECSNET_API void net_socket_init(void);
142
149ECSNET_API void net_socket_cleanup(void);
150
151#ifdef __cplusplus
152}
153#endif
154
Represents a network socket. This struct encapsulates the socket's file descriptor,...
Definition net_socket.h:33
struct sockaddr_in addr
Definition net_socket.h:34
int fd
Definition net_socket.h:36
socket_type_t type
Definition net_socket.h:35