├── .gitignore ├── README.md ├── Makefile ├── LICENSE ├── xsocket.c └── xsocket.h /.gitignore: -------------------------------------------------------------------------------- 1 | main.c 2 | test.c 3 | server.c 4 | client.c 5 | tcp.c 6 | udp.c 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XSocket 2 | 3 | XSocket is a cross-platform socket library written in C language. 4 | 5 | **Attention: Project is under development.** 6 | 7 | **Target platforms:** 8 | 9 | * POSIX compatible operating systems (BSD, Linux, OS X, Android). 10 | * Microsoft Windows XP / 7 / 8 / 10 using winsock library. 11 | 12 | Rule of XSocket is simple! everything starts with "**x**". 13 | 14 | Copyright (c) 2018, Saeid Bostandoust. 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # XSocket is a cross-platform socket library. 2 | # Copyright (c) 2018 Saeid Bostandoust 3 | 4 | LIBDIR=/usr/lib 5 | LIB64DIR=/usr/lib64 6 | INCLUDEDIR=/usr/include 7 | CC=gcc 8 | CFLAGS=-Wall 9 | CXXFLAGS=-fPIC -c 10 | LDFLAGS=-shared 11 | SONAME=libxsocket.so 12 | 13 | $(SONAME): xsocket.o 14 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $? 15 | 16 | xsocket.o: xsocket.c 17 | $(CC) $(CFLAGS) $(CXXFLAGS) -o $@ $? 18 | 19 | .PHONY: install 20 | install: 21 | cp $(SONAME) $(LIBDIR) 22 | [ -d $(LIB64DIR) ] && ln -s $(LIBDIR)/$(SONAME) $(LIB64DIR)/$(SONAME) 23 | cp xsocket.h $(INCLUDEDIR) 24 | 25 | .PHONY: uninstall 26 | uninstall: 27 | [ -d $(LIB64DIR) ] && rm -f $(LIB64DIR)/$(SONAME) 28 | rm -f $(LIBDIR)/$(SONAME) 29 | rm -f $(INCLUDEDIR)/xsocket.h 30 | 31 | .PHONY: clean 32 | clean: 33 | rm -f $(SONAME) xsocket.o 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, Saeid Bostandoust 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /xsocket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * XSocket is a cross-platform socket library. 3 | * 4 | * Copyright (c) 2018 Saeid Bostandoust 5 | * 6 | * BSD 3-Clause "New" or "Revised" License. 7 | * 8 | * @link http://bostandoust.ir/ 9 | * @link http://gitlab.com/ssbostan/XSocket/ 10 | * 11 | */ 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | #include "xsocket.h" 18 | 19 | int xstartup() { 20 | #ifdef XWIN32 21 | WSADATA wsadata; 22 | return WSAStartup(MAKEWORD(2, 2), &wsadata); // for using winsock version 2.2 on windows. 23 | #else 24 | return 0; 25 | #endif 26 | } 27 | 28 | int xcleanup() { 29 | #ifdef XWIN32 30 | return WSACleanup(); // terminates use of winsock library on windows. 31 | #else 32 | return 0; 33 | #endif 34 | } 35 | 36 | int xblocking(xsocket_t socket, int mode) { 37 | #ifdef XWIN32 38 | unsigned long blocking; 39 | blocking=mode ? 0 : 1; 40 | return ioctlsocket(socket, FIONBIO, &blocking); 41 | #else 42 | int flags; 43 | flags=fcntl(socket, F_GETFL); 44 | flags=mode ? flags & ~O_NONBLOCK : flags | O_NONBLOCK; 45 | return fcntl(socket, F_SETFL, flags); 46 | #endif 47 | } 48 | 49 | int xclose(xsocket_t socket) { 50 | #ifdef XWIN32 51 | return closesocket(socket); // close socket on windows. 52 | #else 53 | return close(socket); 54 | #endif 55 | } 56 | 57 | int xerror() { 58 | #ifdef XWIN32 59 | return WSAGetLastError(); // socket errors on windows. 60 | #else 61 | return errno; // socket errors on posix. 62 | #endif 63 | } 64 | 65 | xsocket_t xaccept(xsocket_t socket, struct sockaddr *addr, xsocklen_t *addr_len) { 66 | return accept(socket, addr, addr_len); 67 | } 68 | 69 | xsocket_t xsocket(int domain, int type, int protocol) { 70 | return socket(domain, type, protocol); 71 | } 72 | 73 | // implementations goes here. 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | -------------------------------------------------------------------------------- /xsocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * XSocket is a cross-platform socket library. 3 | * 4 | * Copyright (c) 2018 Saeid Bostandoust 5 | * 6 | * BSD 3-Clause "New" or "Revised" License. 7 | * 8 | * @link http://bostandoust.ir/ 9 | * @link http://gitlab.com/ssbostan/XSocket/ 10 | * 11 | */ 12 | 13 | #ifndef __XSOCKET_H__ 14 | #define __XSOCKET_H__ 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | #ifdef _WIN32 21 | #define XWIN32 // if operating system is windows. 22 | #endif 23 | 24 | #ifdef XWIN32 25 | #include 26 | #include 27 | #else 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #endif 36 | 37 | #ifdef XWIN32 38 | #pragma comment(lib, "Ws2_32.lib") // tell linker to link winsock library. 39 | #endif 40 | 41 | #ifdef XWIN32 42 | #define XERROR(E) WSA##E // for WSA socket errors on windows. 43 | #else 44 | #define XERROR(E) E // cross-platform method for handling socket errors. 45 | #endif 46 | 47 | #ifdef XWIN32 48 | #define XSHUT_RECV SD_RECEIVE 49 | #define XSHUT_SEND SD_SEND 50 | #define XSHUT_BOTH SD_BOTH 51 | #define XSOCKET_INVALID INVALID_SOCKET 52 | #define XSOCKET_ERROR SOCKET_ERROR 53 | typedef SOCKET xsocket_t; 54 | typedef int xsocklen_t; 55 | typedef char xsockopt_t; 56 | typedef char xsockdata_t; 57 | typedef int xsize_t; 58 | #else 59 | #define XSHUT_RECV SHUT_RD 60 | #define XSHUT_SEND SHUT_WR 61 | #define XSHUT_BOTH SHUT_RDWR 62 | #define XSOCKET_INVALID -1 63 | #define XSOCKET_ERROR -1 64 | typedef int xsocket_t; 65 | typedef socklen_t xsocklen_t; 66 | typedef void xsockopt_t; 67 | typedef void xsockdata_t; 68 | typedef size_t xsize_t; 69 | #endif 70 | 71 | int xstartup(); 72 | int xcleanup(); 73 | int xblocking(xsocket_t, int); 74 | int xclose(xsocket_t); 75 | int xerror(); 76 | 77 | xsocket_t xaccept(xsocket_t, struct sockaddr*, xsocklen_t*); 78 | xsocket_t xsocket(int, int, int); 79 | 80 | // implementations goes here. 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif 87 | --------------------------------------------------------------------------------