45 lines
1.2 KiB
C
45 lines
1.2 KiB
C
#ifndef SOCKET_SAMPLE_H
|
|
#define SOCKET_SAMPLE_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
typedef SOCKET Socket_t;
|
|
#define INVALID_SOCKET_HANDLE INVALID_SOCKET
|
|
#define SOCKET_ERROR_HANDLE SOCKET_ERROR
|
|
#else
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
typedef int Socket_t;
|
|
#define INVALID_SOCKET_HANDLE -1
|
|
#define SOCKET_ERROR_HANDLE -1
|
|
#endif
|
|
|
|
#define SOCKET_SUCCESS 0
|
|
#define SOCKET_FAIL -1
|
|
|
|
// 环境初始化(Win必须,Linux为空操作)
|
|
int xSocketInitEnvironment(void);
|
|
|
|
// 创建 UDP Socket
|
|
Socket_t xSocketCreateUDP(void);
|
|
|
|
// 绑定端口(接收端必备)
|
|
int xSocketBind(Socket_t xSocket, uint16_t usPort);
|
|
|
|
// 关闭 Socket
|
|
void vSocketClose(Socket_t xSocket);
|
|
|
|
// UDP 发送数据
|
|
int32_t xSocketSendTo(Socket_t xSocket, const void* pvBuffer, uint32_t ulLength,
|
|
const char* pcIPAddress, uint16_t usPort);
|
|
|
|
// UDP 接收数据
|
|
int32_t xSocketReceiveFrom(Socket_t xSocket, void* pvBuffer, uint32_t ulLength,
|
|
char* pcIPAddress, uint16_t* pusPort);
|
|
|
|
#endif |