From 741d8b9dabfe9d332994c861fa118cb64f9a88c8 Mon Sep 17 00:00:00 2001 From: Matriller <1079561605@qq.com> Date: Thu, 11 Apr 2024 15:42:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86socket,=E5=B9=B6?= =?UTF-8?q?=E4=BD=BF=E5=85=B6=E6=94=AF=E6=8C=81new=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=88=9B=E5=BB=BASocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- socket/Ubuntu/linux_sock.c | 56 +++++++----- socket/Ubuntu/linux_sock.h | 18 ++-- socket/Windows/win_sock.c | 81 ++++++++--------- socket/Windows/win_sock.h | 20 ++--- socket/info | 2 +- socket/socket.c | 52 +++++++++-- socket/socket.els | 171 +++++++++++++++++++++-------------- socket/socket.h | 177 ++++++++++++++++++++++--------------- 8 files changed, 356 insertions(+), 221 deletions(-) diff --git a/socket/Ubuntu/linux_sock.c b/socket/Ubuntu/linux_sock.c index 8b7d7ecf..8ce07747 100755 --- a/socket/Ubuntu/linux_sock.c +++ b/socket/Ubuntu/linux_sock.c @@ -1,6 +1,6 @@ #include "linux_sock.h" -#ifdef LOSU_LINUX +#ifdef __linux__ //辅助函数 gethostbyname_t *LosuSock_gethostbyname(const char *name) @@ -18,7 +18,9 @@ bool LosuSock_sendtimeout(socket_t *sock, double sec) timeout.tv_sec = _sec; timeout.tv_usec = usec; - return setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout))==0 ? true : false; + if (setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout)) == 0) + return true; + return false; } bool LosuSock_recvtimeout(socket_t *sock, double sec) @@ -31,7 +33,9 @@ bool LosuSock_recvtimeout(socket_t *sock, double sec) timeout.tv_sec = _sec; timeout.tv_usec = usec; - return setsockopt(sock->sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout))==0 ? true : false; + if (setsockopt(sock->sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout)) == 0) + return true; + return false; } @@ -47,7 +51,7 @@ socket_t *LosuSock_socket(int family, int type, int protocol) return sock; } -int LosuSock_bind(socket_t *sock, const char *ip, int port) +bool LosuSock_bind(socket_t *sock, const char *ip, int port) { sock->addr.sin_port = htons(port); @@ -55,13 +59,17 @@ int LosuSock_bind(socket_t *sock, const char *ip, int port) sock->addr.sin_addr.s_addr = INADDR_ANY; else sock->addr.sin_addr.s_addr = inet_addr(ip); - - return bind(sock->sockfd, (void*)&sock->addr, sizeof(sock->addr)); + + if (bind(sock->sockfd, (void*)&sock->addr, sizeof(sock->addr)) == 0) + return true; + return false; }; -int LosuSock_listen(socket_t *sock, int amount) +bool LosuSock_listen(socket_t *sock, int amount) { - return listen(sock->sockfd, amount); + if (listen(sock->sockfd, amount) == 0) + return true; + return false; } socket_t *LosuSock_accept(socket_t *sock) @@ -78,33 +86,41 @@ socket_t *LosuSock_accept(socket_t *sock) return client; } -int LosuSock_connect(socket_t *sock, const char *ip, int port) +bool LosuSock_connect(socket_t *sock, const char *ip, int port) { sock->addr.sin_port = htons(port); sock->addr.sin_addr.s_addr = inet_addr(ip); - connect(sock->sockfd, (struct sockaddr*)&sock->addr, sizeof(sock->addr)); - - return 1; + if (connect(sock->sockfd, (struct sockaddr*)&sock->addr, sizeof(sock->addr)) == 0) + return true; + return false; } -int LosuSock_send(socket_t *sock, const char *data, int length, int x) +bool LosuSock_send(socket_t *sock, const char *data, int length, int x) { - send(sock->sockfd, data, length, x); - return 1; + if (send(sock->sockfd, data, length, x) != -1); + return true; + return false; } -int LosuSock_recv(socket_t *sock, char *buffer, int length, int x) +bool LosuSock_recv(socket_t *sock, char *buffer, int length, int x) { - return recv(sock->sockfd, buffer, length, x); + if (recv(sock->sockfd, buffer, length, x) >= 0) + return true; + return false; } -int LosuSock_close(socket_t *sock) +bool LosuSock_close(socket_t *sock) { - close(sock->sockfd); + if (close(sock->sockfd) != -1) + { + free(sock); + return true; + } free(sock); - return 1; + return false; } + #endif \ No newline at end of file diff --git a/socket/Ubuntu/linux_sock.h b/socket/Ubuntu/linux_sock.h index c3d2b71d..13625686 100755 --- a/socket/Ubuntu/linux_sock.h +++ b/socket/Ubuntu/linux_sock.h @@ -1,11 +1,11 @@ #ifndef LOSU_LINUX_SOCK_H__ #define LOSU_LINUX_SOCK_H__ -#ifndef LOSU_LINUX -#warning "该文件用于Linux系统,如要使用文件,请包含LOSU_LINUX宏" +#ifndef __linux__ +#warning "该文件用于Linux系统" #endif -#ifdef LOSU_LINUX +#ifdef __linux__ #include #include //struct timeval @@ -40,13 +40,13 @@ bool LosuSock_recvtimeout(socket_t *sock, double sec); //socket操作函数 socket_t *LosuSock_socket(int family, int type, int protocol); -int LosuSock_bind(socket_t *sock, const char *ip, int port); -int LosuSock_listen(socket_t *sock, int amount); +bool LosuSock_bind(socket_t *sock, const char *ip, int port); +bool LosuSock_listen(socket_t *sock, int amount); socket_t *LosuSock_accept(socket_t *sock); -int LosuSock_connect(socket_t *sock, const char *ip, int port); -int LosuSock_send(socket_t *sock, const char *data, int length, int x); -int LosuSock_recv(socket_t *sock, char *buffer, int length, int x); -int LosuSock_close(socket_t *sock); +bool LosuSock_connect(socket_t *sock, const char *ip, int port); +bool LosuSock_send(socket_t *sock, const char *data, int length, int x); +bool LosuSock_recv(socket_t *sock, char *buffer, int length, int x); +bool LosuSock_close(socket_t *sock); #endif diff --git a/socket/Windows/win_sock.c b/socket/Windows/win_sock.c index 4742fdc4..19883874 100755 --- a/socket/Windows/win_sock.c +++ b/socket/Windows/win_sock.c @@ -1,11 +1,11 @@ #include "win_sock.h" -#ifdef LOSU_WINDOWS +#ifdef _WIN32 int WinSockInited = 0; //保存初始化信息,已初始化则为1,否则为0 //辅助函数 -int LosuSock_init() +bool LosuSock_init() { /* winsock库需要特殊的初始化 @@ -13,9 +13,9 @@ int LosuSock_init() WORD sockVersion = MAKEWORD(2, 2); WSADATA wsdata; - if (WSAStartup(sockVersion, &wsdata)!=0) - return 0; - return 1; + if (WSAStartup(sockVersion, &wsdata) == 0) + return true; + return false; } gethostbyname_t *LosuSock_gethostbyname(const char *name) @@ -23,9 +23,7 @@ gethostbyname_t *LosuSock_gethostbyname(const char *name) if (!WinSockInited) WinSockInited = LosuSock_init(); gethostbyname_t *output; - char *buffer = vm_win_togbk(name); output = gethostbyname(name); - free(buffer); return output; } @@ -39,7 +37,9 @@ bool LosuSock_sendtimeout(socket_t *sock, double sec) timeout.tv_sec = _sec; timeout.tv_usec = usec; - return setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout))==0 ? true : false; + if (setsockopt(sock->sockfd, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout)) == 0) + return true; + return false; } bool LosuSock_recvtimeout(socket_t *sock, double sec) @@ -52,7 +52,9 @@ bool LosuSock_recvtimeout(socket_t *sock, double sec) timeout.tv_sec = _sec; timeout.tv_usec = usec; - return setsockopt(sock->sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout))==0 ? true : false; + if (setsockopt(sock->sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout)) == 0) + return true; + return false; } //socket操作 @@ -75,35 +77,35 @@ socket_t *LosuSock_socket(int family, int type, int protocol) return sock; } -int LosuSock_bind(socket_t *sock, const char *ip, int port) +bool LosuSock_bind(socket_t *sock, const char *ip, int port) { /* 将套接字和对应的ip和端口相绑定 */ sock->addr.sin_port = htons(port); - if (ip==NULL) + if (ip == NULL) //ip缺省时的情况 sock->addr.sin_addr.S_un.S_addr = INADDR_ANY; else sock->addr.sin_addr.S_un.S_addr = inet_addr(ip); - if (bind(sock->sockfd, (void*)&sock->addr, sizeof(sock->addr))==SOCKET_ERROR) - return 0; - return 1; + if (bind(sock->sockfd, (void*)&sock->addr, sizeof(sock->addr)) != SOCKET_ERROR) + return true; + return false; } -int LosuSock_listen(socket_t *sock, int amount) +bool LosuSock_listen(socket_t *sock, int amount) { /* 监听 */ - if (listen(sock->sockfd, amount)==SOCKET_ERROR) - return 0; - return 1; + if (listen(sock->sockfd, amount) != SOCKET_ERROR) + return true; + return false; } -int LosuSock_connect(socket_t *sock, const char *ip, int port) +bool LosuSock_connect(socket_t *sock, const char *ip, int port) { /* 连接 @@ -112,9 +114,9 @@ int LosuSock_connect(socket_t *sock, const char *ip, int port) sock->addr.sin_addr.S_un.S_addr = inet_addr(ip); int length = sizeof(sock->addr); - if (connect(sock->sockfd, (void*)&sock->addr, length)==SOCKET_ERROR) - return 0; - return 1; + if (connect(sock->sockfd, (void*)&sock->addr, length) == SOCKET_ERROR) + return false; + return true; } socket_t *LosuSock_accept(socket_t * sock) @@ -128,34 +130,33 @@ socket_t *LosuSock_accept(socket_t * sock) return client; } -int LosuSock_send(socket_t *sock, const char *msg, int length, int x) +bool LosuSock_send(socket_t *sock, const char *msg, int length, int x) { - char *buffer = vm_win_togbk(msg); - send(sock->sockfd,buffer, strlen(buffer), x); - free(buffer); - return 1; + if (send(sock->sockfd, msg, length, x) != SOCKET_ERROR) + { + return true; + } + return false; } -int LosuSock_recv(socket_t *sock, char *buffer, int length, int x) +bool LosuSock_recv(socket_t *sock, char *buffer, int length, int x) { - char *tmp = malloc(sizeof(char)*length+1); - char *_t; - recv(sock->sockfd, tmp, length, x); - _t = vm_win_toutf8(tmp); - strcpy(buffer, _t); - free(tmp); - free(_t); - return 1; + recv(sock->sockfd, buffer, length, x); + return true; } -int LosuSock_close(socket_t *sock) +bool LosuSock_close(socket_t *sock) { /* 关闭 */ - closesocket(sock->sockfd); + if (closesocket(sock->sockfd) == 0) + { + free(sock); + return true; + } free(sock); - return 1; + return false; } -#endif \ No newline at end of file +#endif diff --git a/socket/Windows/win_sock.h b/socket/Windows/win_sock.h index 7c751042..e6fd6992 100755 --- a/socket/Windows/win_sock.h +++ b/socket/Windows/win_sock.h @@ -1,12 +1,12 @@ #ifndef LOSU_WIN_SOCK_H__ #define LOSU_WIN_SOCK_H__ -#ifndef LOSU_WINDOWS -#warning "该文件用于Windows系统,如需使用,请包含LOSU_WINDOWS宏" +#ifndef _WIN32 + #warning "该文件用于Windows系统" #endif -#ifdef LOSU_WINDOWS +#ifdef _WIN32 #include "els.h" @@ -33,20 +33,20 @@ typedef struct typedef struct hostent gethostbyname_t; //辅助函数 -int LosuSock_init(); +bool LosuSock_init(); gethostbyname_t *LosuSock_gethostbyname(const char *name); bool LosuSock_sendtimeout(socket_t *sock, double sec); bool LosuSock_recvtimeout(socket_t *sock, double sec); //socket操作 socket_t *LosuSock_socket(int family, int type, int prototol); -int LosuSock_bind(socket_t *sock, const char *ip, int port); -int LosuSock_listen(socket_t *sock, int amount); +bool LosuSock_bind(socket_t *sock, const char *ip, int port); +bool LosuSock_listen(socket_t *sock, int amount); socket_t *LosuSock_accept(socket_t *sock); -int LosuSock_connect(socket_t *sock, const char *ip, int port); -int LosuSock_send(socket_t *sock, const char *msg, int length, int x); -int LosuSock_recv(socket_t *sock, char *buffer, int length, int x); -int LosuSock_close(socket_t *sock); +bool LosuSock_connect(socket_t *sock, const char *ip, int port); +bool LosuSock_send(socket_t *sock, const char *msg, int length, int x); +bool LosuSock_recv(socket_t *sock, char *buffer, int length, int x); +bool LosuSock_close(socket_t *sock); #endif #endif \ No newline at end of file diff --git a/socket/info b/socket/info index bf3a968f..c6ee955f 100755 --- a/socket/info +++ b/socket/info @@ -2,7 +2,7 @@ info = { name = "socket/套接字模块", text = "网络套接字模块", - version = "23.10.15" + version = "24.4.11" }, source = { all = { diff --git a/socket/socket.c b/socket/socket.c index 93a3d1b7..7a2e6f61 100755 --- a/socket/socket.c +++ b/socket/socket.c @@ -1,11 +1,13 @@ #include "socket.h" -#ifdef LOSU_WINDOWS -#include "win_sock.h" +#ifdef _WIN32 + #include "win_sock.h" + #include + #include #endif -#ifdef LOSU_LINUX -#include "linux_sock.h" +#ifdef __linux__ + #include "linux_sock.h" #endif /* @@ -58,6 +60,41 @@ int ELSAPI_socket_c_SOCK_STREAM(els_VmObj* vm) return 1; } +#define PUTN(s, n) vm_setval(vm, s, obj_newnum(vm, n)) + +int ELSAPI_socket_c_constinit(els_VmObj* vm) // c_constinit() +{ + /* + 用于将一些常量,如AF_INET放入全局作用域中 + */ + //地址系列常量 + PUTN("AF_UNSPEC", AF_UNSPEC); + PUTN("AF_INET", AF_INET); + PUTN("AF_IPX", AF_IPX); + PUTN("AF_APPLETALK", AF_APPLETALK); + PUTN("AF_NETBIOS", AF_NETBIOS); + PUTN("AF_INET6", AF_INET6); + PUTN("AF_IRDA", AF_IRDA); + PUTN("AF_BTH", AF_BTH); + + //套接字类型系列常量 + PUTN("SOCK_STREAM", SOCK_STREAM); + PUTN("SOCK_DGRAM", SOCK_DGRAM); + PUTN("SOCK_RAW", SOCK_RAW); + PUTN("SOCK_RDM", SOCK_RDM); + PUTN("SOCK_SEQPACKET", SOCK_SEQPACKET); + + //套接字协议系列常量 + PUTN("IPPROTO_ICMP", IPPROTO_ICMP); + PUTN("IPPROTO_IGMP", IPPROTO_IGMP); + PUTN("IPPROTO_TCP", IPPROTO_TCP); + PUTN("IPPROTO_UDP", IPPROTO_UDP); + PUTN("IPPROTO_ICMPV6", IPPROTO_ICMPV6); + PUTN("IPPROTO_RM", IPPROTO_RM); + + return 0; +} + int ELSAPI_socket_c_gethostbyname(els_VmObj *vm) { /* @@ -306,7 +343,10 @@ int ELSAPI_socket_c_send(els_VmObj* vm) 2.data */ socket_t *sock = (void*)arg_getptr(vm, 1); - const char *data = arg_getstr(vm, 2); + const char *data = ""; + + if (arg_gettype(vm, 2) == ELS_API_TYPE_STRING) + data = arg_getstr(vm, 2); LosuSock_send(sock, data, strlen(data), 0); return 1; @@ -321,7 +361,7 @@ int ELSAPI_socket_c_recv(els_VmObj* vm) */ socket_t *sock = (void*)arg_getptr(vm, 1); int amount = arg_getnum(vm, 2); - char *tmp = malloc(sizeof(char)*amount+1); + char *tmp = calloc(sizeof(char), amount + 1); LosuSock_recv(sock, tmp, amount, 0); arg_returnstr(vm, tmp); diff --git a/socket/socket.els b/socket/socket.els index 827f60e1..ab31eabc 100755 --- a/socket/socket.els +++ b/socket/socket.els @@ -1,4 +1,6 @@ #!declare +c_constinit() + c_AF_INET() c_SOCK_STREAM() @@ -17,108 +19,145 @@ c_close() #!end #!script -import("stdlib") + +c_constinit() #加载常量到全局作用域 socket = { - AF_INET = c_AF_INET(), - SOCK_STREAM = c_SOCK_STREAM() + #地址系列常量 + AF_UNSPEC = AF_UNSPEC, + AF_INET = AF_INET, + AF_IPX = AF_IPX, + AF_APPLETALK = AF_APPLETALK, + AF_NETBIOS = AF_NETBIOS, + AF_INET6 = AF_INET6, + AF_IRDA = AF_IRDA, + AF_BTH = AF_BTH, + + #套接字类型系列常量 + SOCK_STREAM = SOCK_STREAM, + SOCK_DGRAM = SOCK_DGRAM, + SOCK_RAW = SOCK_RAW, + SOCK_RDM = SOCK_RDM, + SOCK_SEQPACKET = SOCK_SEQPACKET, + + #套接字协议系列常量 + IPPROTO_ICMP = IPPROTO_ICMP, + IPPROTO_IGMP = IPPROTO_IGMP, + IPPROTO_TCP = IPPROTO_TCP, + IPPROTO_UDP = IPPROTO_UDP, + IPPROTO_ICMPV6 = IPPROTO_ICMPV6, + IPPROTO_RM = IPPROTO_RM, + + #类 + Socket = {} } def socket.gethostbyname(name): rt c_gethostbyname(name) ; -def socket.socket(family, type, protocol): +def socket.Socket.class(family, type, protocol): var this = { } - family = family or socket.AF_INET - type = type or socket.SOCK_STREAM - protocol = 0 - #成员变量 - this.sock = c_socket(family, type, protocol) + this.family = family or socket.AF_INET + this.type = type or socket.SOCK_STREAM + this.protocol = protocol or 0 + this.sock = c_socket(this.family, this.type, this.protocol) + + #成员函数 + this.sendtimeout = socket.sendtimeout + this.recvtimeout = sock.recvtimeout + this.bind = socket.bind + this.listen = socket.listen + this.accept = socket.accept + this.connect = socket.connect + this.send = socket.send + this.recv = socket.recv + this.close = socket.close - def this.sendtimeout(sec): - if not sec or not this.sock: - rt null - ; - rt c_sendtimeout(this.sock, sec) - ; + rt this +; - def this.recvtimeout(sec): - if not sec or not this.sock: - rt null - ; - rt c_recvtimeout(this.sock, sec) +def socket.sendtimeout(sec): + if not sec or not this.sock: + rt null ; + rt c_sendtimeout(this.sock, sec) +; - def this.bind(ip, port): - if not this.sock: - rt null - ; - - ip = ip or 0 - port = port or 80 - rt c_bind(this.sock, ip, port) +def socket.recvtimeout(sec): + if not sec or not this.sock: + rt null ; + rt c_recvtimeout(this.sock, sec) +; - def this.listen(amount): - if not this.sock: - rt null - ; - - amount = amount or 5 - rt c_listen(this.sock, amount) +def socket.bind(ip, port): + if not this.sock: + rt null ; - def this.accept(): - if not this.sock: - rt null - ; + this.ip = ip or 0 + this.port = port or 80 + rt c_bind(this.sock, this.ip, this.port) +; - var client = socket.socket(family, type, protocol) #闭包 - client.sock = c_accept(this.sock) - rt client +def socket.listen(amount): + if not this.sock: + rt null ; - def this.connect(ip, port): - if not ip or not this.sock: - rt null - ; - port = port or 80 - c_connect(this.sock, ip, port) + this.amount = amount or 5 + rt c_listen(this.sock, this.amount) +; + +def socket.accept(): + if not this.sock: + rt null ; - def this.send(data): - if not data or not this.sock: - rt null - ; + var client = new(socket.Socket, this.family, this.type, this.protocol) + client.sock = c_accept(this.sock) + rt client +; - c_send(this.sock, data) +def socket.connect(ip, port): + if not ip or not this.sock: + rt null ; + this.ip = ip + this.port = port or 80 + c_connect(this.sock, this.ip, this.port) +; - def this.recv(amount): - if not this.sock: - rt null - ; +def socket.send(data): + if not data or not this.sock: + rt null + ; - amount = amount or 1024 + c_send(this.sock, data) +; - rt c_recv(this.sock, amount) +def socket.recv(amount): + if not this.sock: + rt null ; - def this.close(): - if not this.sock: - rt null - ; + amount = amount or 1024 - c_close(this.sock) - this.sock = null + rt c_recv(this.sock, amount) +; + +def socket.close(): + if not this.sock: + rt null ; - rt this + c_close(this.sock) + this.sock = null ; #!end diff --git a/socket/socket.h b/socket/socket.h index 33e706ac..b32e3843 100755 --- a/socket/socket.h +++ b/socket/socket.h @@ -1,4 +1,5 @@ #include "els.h" +int ELSAPI_socket_c_constinit(els_VmObj* vm); // c_constinit() int ELSAPI_socket_c_AF_INET(els_VmObj* vm); // c_AF_INET() int ELSAPI_socket_c_SOCK_STREAM(els_VmObj* vm); // c_SOCK_STREAM() int ELSAPI_socket_c_gethostbyname(els_VmObj* vm); // c_gethostbyname() @@ -14,108 +15,145 @@ int ELSAPI_socket_c_recv(els_VmObj* vm); // c_recv() int ELSAPI_socket_c_close(els_VmObj* vm); // c_close() #ifdef ELS_CONF_TOKEN_EN static const char LibScript[]={ -105,109,112,111,114,116,40,34,115,116,100,108,105,98,34,41,10, +10, +99,95,99,111,110,115,116,105,110,105,116,40,41,32,35,-27,-118,-96,-24,-67,-67,-27,-72,-72,-23,-121,-113,-27,-120,-80,-27,-123,-88,-27,-79,-128,-28,-67,-100,-25,-108,-88,-27,-97,-97,10, 10, 115,111,99,107,101,116,32,61,32,123,10, -32,32,32,32,65,70,95,73,78,69,84,32,61,32,99,95,65,70,95,73,78,69,84,40,41,44,10, -32,32,32,32,83,79,67,75,95,83,84,82,69,65,77,32,61,32,99,95,83,79,67,75,95,83,84,82,69,65,77,40,41,10, +32,32,32,32,35,-27,-100,-80,-27,-99,-128,-25,-77,-69,-27,-120,-105,-27,-72,-72,-23,-121,-113,10, +32,32,32,32,65,70,95,85,78,83,80,69,67,32,61,32,65,70,95,85,78,83,80,69,67,44,10, +32,32,32,32,65,70,95,73,78,69,84,32,61,32,65,70,95,73,78,69,84,44,10, +32,32,32,32,65,70,95,73,80,88,32,61,32,65,70,95,73,80,88,44,10, +32,32,32,32,65,70,95,65,80,80,76,69,84,65,76,75,32,61,32,65,70,95,65,80,80,76,69,84,65,76,75,44,10, +32,32,32,32,65,70,95,78,69,84,66,73,79,83,32,61,32,65,70,95,78,69,84,66,73,79,83,44,10, +32,32,32,32,65,70,95,73,78,69,84,54,32,61,32,65,70,95,73,78,69,84,54,44,10, +32,32,32,32,65,70,95,73,82,68,65,32,61,32,65,70,95,73,82,68,65,44,10, +32,32,32,32,65,70,95,66,84,72,32,61,32,65,70,95,66,84,72,44,10, +10, +32,32,32,32,35,-27,-91,-105,-26,-114,-91,-27,-83,-105,-25,-79,-69,-27,-98,-117,-25,-77,-69,-27,-120,-105,-27,-72,-72,-23,-121,-113,10, +32,32,32,32,83,79,67,75,95,83,84,82,69,65,77,32,61,32,83,79,67,75,95,83,84,82,69,65,77,44,10, +32,32,32,32,83,79,67,75,95,68,71,82,65,77,32,61,32,83,79,67,75,95,68,71,82,65,77,44,10, +32,32,32,32,83,79,67,75,95,82,65,87,32,61,32,83,79,67,75,95,82,65,87,44,10, +32,32,32,32,83,79,67,75,95,82,68,77,32,61,32,83,79,67,75,95,82,68,77,44,10, +32,32,32,32,83,79,67,75,95,83,69,81,80,65,67,75,69,84,32,61,32,83,79,67,75,95,83,69,81,80,65,67,75,69,84,44,10, +10, +32,32,32,32,35,-27,-91,-105,-26,-114,-91,-27,-83,-105,-27,-115,-113,-24,-82,-82,-25,-77,-69,-27,-120,-105,-27,-72,-72,-23,-121,-113,10, +32,32,32,32,73,80,80,82,79,84,79,95,73,67,77,80,32,61,32,73,80,80,82,79,84,79,95,73,67,77,80,44,10, +32,32,32,32,73,80,80,82,79,84,79,95,73,71,77,80,32,61,32,73,80,80,82,79,84,79,95,73,71,77,80,44,10, +32,32,32,32,73,80,80,82,79,84,79,95,84,67,80,32,61,32,73,80,80,82,79,84,79,95,84,67,80,44,10, +32,32,32,32,73,80,80,82,79,84,79,95,85,68,80,32,61,32,73,80,80,82,79,84,79,95,85,68,80,44,10, +32,32,32,32,73,80,80,82,79,84,79,95,73,67,77,80,86,54,32,61,32,73,80,80,82,79,84,79,95,73,67,77,80,86,54,44,10, +32,32,32,32,73,80,80,82,79,84,79,95,82,77,32,61,32,73,80,80,82,79,84,79,95,82,77,44,10, +10, +32,32,32,32,35,-25,-79,-69,10, +32,32,32,32,83,111,99,107,101,116,32,61,32,123,125,10, 125,10, 10, 100,101,102,32,115,111,99,107,101,116,46,103,101,116,104,111,115,116,98,121,110,97,109,101,40,110,97,109,101,41,58,10, 32,32,32,32,114,116,32,99,95,103,101,116,104,111,115,116,98,121,110,97,109,101,40,110,97,109,101,41,10, 59,10, 10, -100,101,102,32,115,111,99,107,101,116,46,115,111,99,107,101,116,40,102,97,109,105,108,121,44,32,116,121,112,101,44,32,112,114,111,116,111,99,111,108,41,58,10, +100,101,102,32,115,111,99,107,101,116,46,83,111,99,107,101,116,46,99,108,97,115,115,40,102,97,109,105,108,121,44,32,116,121,112,101,44,32,112,114,111,116,111,99,111,108,41,58,10, 32,32,32,32,118,97,114,32,116,104,105,115,32,61,32,123,10, 10, 32,32,32,32,125,10, 10, -32,32,32,32,102,97,109,105,108,121,32,61,32,102,97,109,105,108,121,32,111,114,32,115,111,99,107,101,116,46,65,70,95,73,78,69,84,10, -32,32,32,32,116,121,112,101,32,61,32,116,121,112,101,32,111,114,32,115,111,99,107,101,116,46,83,79,67,75,95,83,84,82,69,65,77,10, -32,32,32,32,112,114,111,116,111,99,111,108,32,61,32,48,10, -10, 32,32,32,32,35,-26,-120,-112,-27,-111,-104,-27,-113,-104,-23,-121,-113,10, -32,32,32,32,116,104,105,115,46,115,111,99,107,32,61,32,99,95,115,111,99,107,101,116,40,102,97,109,105,108,121,44,32,116,121,112,101,44,32,112,114,111,116,111,99,111,108,41,10, +32,32,32,32,116,104,105,115,46,102,97,109,105,108,121,32,61,32,102,97,109,105,108,121,32,111,114,32,115,111,99,107,101,116,46,65,70,95,73,78,69,84,10, +32,32,32,32,116,104,105,115,46,116,121,112,101,32,61,32,116,121,112,101,32,111,114,32,115,111,99,107,101,116,46,83,79,67,75,95,83,84,82,69,65,77,10, +32,32,32,32,116,104,105,115,46,112,114,111,116,111,99,111,108,32,61,32,112,114,111,116,111,99,111,108,32,111,114,32,48,10, +32,32,32,32,116,104,105,115,46,115,111,99,107,32,61,32,99,95,115,111,99,107,101,116,40,116,104,105,115,46,102,97,109,105,108,121,44,32,116,104,105,115,46,116,121,112,101,44,32,116,104,105,115,46,112,114,111,116,111,99,111,108,41,10, +10, +32,32,32,32,35,-26,-120,-112,-27,-111,-104,-27,-121,-67,-26,-107,-80,10, +32,32,32,32,116,104,105,115,46,115,101,110,100,116,105,109,101,111,117,116,32,61,32,115,111,99,107,101,116,46,115,101,110,100,116,105,109,101,111,117,116,10, +32,32,32,32,116,104,105,115,46,114,101,99,118,116,105,109,101,111,117,116,32,61,32,115,111,99,107,46,114,101,99,118,116,105,109,101,111,117,116,10, +32,32,32,32,116,104,105,115,46,98,105,110,100,32,61,32,115,111,99,107,101,116,46,98,105,110,100,10, +32,32,32,32,116,104,105,115,46,108,105,115,116,101,110,32,61,32,115,111,99,107,101,116,46,108,105,115,116,101,110,10, +32,32,32,32,116,104,105,115,46,97,99,99,101,112,116,32,61,32,115,111,99,107,101,116,46,97,99,99,101,112,116,10, +32,32,32,32,116,104,105,115,46,99,111,110,110,101,99,116,32,61,32,115,111,99,107,101,116,46,99,111,110,110,101,99,116,10, +32,32,32,32,116,104,105,115,46,115,101,110,100,32,61,32,115,111,99,107,101,116,46,115,101,110,100,10, +32,32,32,32,116,104,105,115,46,114,101,99,118,32,61,32,115,111,99,107,101,116,46,114,101,99,118,10, +32,32,32,32,116,104,105,115,46,99,108,111,115,101,32,61,32,115,111,99,107,101,116,46,99,108,111,115,101,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,115,101,110,100,116,105,109,101,111,117,116,40,115,101,99,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,115,101,99,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, -32,32,32,32,32,32,32,32,114,116,32,99,95,115,101,110,100,116,105,109,101,111,117,116,40,116,104,105,115,46,115,111,99,107,44,32,115,101,99,41,10, -32,32,32,32,59,10, +32,32,32,32,114,116,32,116,104,105,115,10, +59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,114,101,99,118,116,105,109,101,111,117,116,40,115,101,99,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,115,101,99,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, -32,32,32,32,32,32,32,32,114,116,32,99,95,114,101,99,118,116,105,109,101,111,117,116,40,116,104,105,115,46,115,111,99,107,44,32,115,101,99,41,10, +100,101,102,32,115,111,99,107,101,116,46,115,101,110,100,116,105,109,101,111,117,116,40,115,101,99,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,115,101,99,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, +32,32,32,32,114,116,32,99,95,115,101,110,100,116,105,109,101,111,117,116,40,116,104,105,115,46,115,111,99,107,44,32,115,101,99,41,10, +59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,98,105,110,100,40,105,112,44,32,112,111,114,116,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, -10, -32,32,32,32,32,32,32,32,105,112,32,61,32,105,112,32,111,114,32,48,10, -32,32,32,32,32,32,32,32,112,111,114,116,32,61,32,112,111,114,116,32,111,114,32,56,48,10, -32,32,32,32,32,32,32,32,114,116,32,99,95,98,105,110,100,40,116,104,105,115,46,115,111,99,107,44,32,105,112,44,32,112,111,114,116,41,10, +100,101,102,32,115,111,99,107,101,116,46,114,101,99,118,116,105,109,101,111,117,116,40,115,101,99,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,115,101,99,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, +32,32,32,32,114,116,32,99,95,114,101,99,118,116,105,109,101,111,117,116,40,116,104,105,115,46,115,111,99,107,44,32,115,101,99,41,10, +59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,108,105,115,116,101,110,40,97,109,111,117,110,116,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, -10, -32,32,32,32,32,32,32,32,97,109,111,117,110,116,32,61,32,97,109,111,117,110,116,32,111,114,32,53,10, -32,32,32,32,32,32,32,32,114,116,32,99,95,108,105,115,116,101,110,40,116,104,105,115,46,115,111,99,107,44,32,97,109,111,117,110,116,41,10, +100,101,102,32,115,111,99,107,101,116,46,98,105,110,100,40,105,112,44,32,112,111,114,116,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,97,99,99,101,112,116,40,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, +32,32,32,32,116,104,105,115,46,105,112,32,61,32,105,112,32,111,114,32,48,10, +32,32,32,32,116,104,105,115,46,112,111,114,116,32,61,32,112,111,114,116,32,111,114,32,56,48,10, +32,32,32,32,114,116,32,99,95,98,105,110,100,40,116,104,105,115,46,115,111,99,107,44,32,116,104,105,115,46,105,112,44,32,116,104,105,115,46,112,111,114,116,41,10, +59,10, 10, -32,32,32,32,32,32,32,32,118,97,114,32,99,108,105,101,110,116,32,61,32,115,111,99,107,101,116,46,115,111,99,107,101,116,40,102,97,109,105,108,121,44,32,116,121,112,101,44,32,112,114,111,116,111,99,111,108,41,32,35,-23,-105,-83,-27,-116,-123,10, -32,32,32,32,32,32,32,32,99,108,105,101,110,116,46,115,111,99,107,32,61,32,99,95,97,99,99,101,112,116,40,116,104,105,115,46,115,111,99,107,41,10, -32,32,32,32,32,32,32,32,114,116,32,99,108,105,101,110,116,10, +100,101,102,32,115,111,99,107,101,116,46,108,105,115,116,101,110,40,97,109,111,117,110,116,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,99,111,110,110,101,99,116,40,105,112,44,32,112,111,114,116,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,105,112,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, -32,32,32,32,32,32,32,32,112,111,114,116,32,61,32,112,111,114,116,32,111,114,32,56,48,10, -32,32,32,32,32,32,32,32,99,95,99,111,110,110,101,99,116,40,116,104,105,115,46,115,111,99,107,44,32,105,112,44,32,112,111,114,116,41,10, +32,32,32,32,116,104,105,115,46,97,109,111,117,110,116,32,61,32,97,109,111,117,110,116,32,111,114,32,53,10, +32,32,32,32,114,116,32,99,95,108,105,115,116,101,110,40,116,104,105,115,46,115,111,99,107,44,32,116,104,105,115,46,97,109,111,117,110,116,41,10, +59,10, +10, +100,101,102,32,115,111,99,107,101,116,46,97,99,99,101,112,116,40,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,115,101,110,100,40,100,97,116,97,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,100,97,116,97,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, +32,32,32,32,118,97,114,32,99,108,105,101,110,116,32,61,32,110,101,119,40,115,111,99,107,101,116,46,83,111,99,107,101,116,44,32,116,104,105,115,46,102,97,109,105,108,121,44,32,116,104,105,115,46,116,121,112,101,44,32,116,104,105,115,46,112,114,111,116,111,99,111,108,41,10, +32,32,32,32,99,108,105,101,110,116,46,115,111,99,107,32,61,32,99,95,97,99,99,101,112,116,40,116,104,105,115,46,115,111,99,107,41,10, +32,32,32,32,114,116,32,99,108,105,101,110,116,10, +59,10, 10, -32,32,32,32,32,32,32,32,99,95,115,101,110,100,40,116,104,105,115,46,115,111,99,107,44,32,100,97,116,97,41,10, +100,101,102,32,115,111,99,107,101,116,46,99,111,110,110,101,99,116,40,105,112,44,32,112,111,114,116,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,105,112,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, +32,32,32,32,116,104,105,115,46,105,112,32,61,32,105,112,10, +32,32,32,32,116,104,105,115,46,112,111,114,116,32,61,32,112,111,114,116,32,111,114,32,56,48,10, +32,32,32,32,99,95,99,111,110,110,101,99,116,40,116,104,105,115,46,115,111,99,107,44,32,116,104,105,115,46,105,112,44,32,116,104,105,115,46,112,111,114,116,41,10, +59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,114,101,99,118,40,97,109,111,117,110,116,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, +100,101,102,32,115,111,99,107,101,116,46,115,101,110,100,40,100,97,116,97,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,100,97,116,97,32,111,114,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, +32,32,32,32,59,10, 10, -32,32,32,32,32,32,32,32,97,109,111,117,110,116,32,61,32,97,109,111,117,110,116,32,111,114,32,49,48,50,52,10, +32,32,32,32,99,95,115,101,110,100,40,116,104,105,115,46,115,111,99,107,44,32,100,97,116,97,41,10, +59,10, 10, -32,32,32,32,32,32,32,32,114,116,32,99,95,114,101,99,118,40,116,104,105,115,46,115,111,99,107,44,32,97,109,111,117,110,116,41,10, +100,101,102,32,115,111,99,107,101,116,46,114,101,99,118,40,97,109,111,117,110,116,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, 10, -32,32,32,32,100,101,102,32,116,104,105,115,46,99,108,111,115,101,40,41,58,10, -32,32,32,32,32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, -32,32,32,32,32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, -32,32,32,32,32,32,32,32,59,10, +32,32,32,32,97,109,111,117,110,116,32,61,32,97,109,111,117,110,116,32,111,114,32,49,48,50,52,10, 10, -32,32,32,32,32,32,32,32,99,95,99,108,111,115,101,40,116,104,105,115,46,115,111,99,107,41,10, -32,32,32,32,32,32,32,32,116,104,105,115,46,115,111,99,107,32,61,32,110,117,108,108,10, +32,32,32,32,114,116,32,99,95,114,101,99,118,40,116,104,105,115,46,115,111,99,107,44,32,97,109,111,117,110,116,41,10, +59,10, +10, +100,101,102,32,115,111,99,107,101,116,46,99,108,111,115,101,40,41,58,10, +32,32,32,32,105,102,32,110,111,116,32,116,104,105,115,46,115,111,99,107,58,10, +32,32,32,32,32,32,32,32,114,116,32,110,117,108,108,10, 32,32,32,32,59,10, 10, -32,32,32,32,114,116,32,116,104,105,115,10, +32,32,32,32,99,95,99,108,111,115,101,40,116,104,105,115,46,115,111,99,107,41,10, +32,32,32,32,116,104,105,115,46,115,111,99,107,32,61,32,110,117,108,108,10, 59,10, 10, 0}; @@ -124,16 +162,17 @@ void ElsLib_socket_libinit(els_VmObj *vm){ vm_register(vm,"c_gethostbyname",ELSAPI_socket_c_gethostbyname); vm_register(vm,"c_socket",ELSAPI_socket_c_socket); vm_register(vm,"c_recvtimeout",ELSAPI_socket_c_recvtimeout); + vm_register(vm,"c_close",ELSAPI_socket_c_close); vm_register(vm,"c_SOCK_STREAM",ELSAPI_socket_c_SOCK_STREAM); vm_register(vm,"c_bind",ELSAPI_socket_c_bind); - vm_register(vm,"c_close",ELSAPI_socket_c_close); + vm_register(vm,"c_recv",ELSAPI_socket_c_recv); vm_register(vm,"c_sendtimeout",ELSAPI_socket_c_sendtimeout); vm_register(vm,"c_send",ELSAPI_socket_c_send); vm_register(vm,"c_accept",ELSAPI_socket_c_accept); vm_register(vm,"c_connect",ELSAPI_socket_c_connect); - vm_register(vm,"c_recv",ELSAPI_socket_c_recv); - vm_register(vm,"c_listen",ELSAPI_socket_c_listen); vm_register(vm,"c_AF_INET",ELSAPI_socket_c_AF_INET); + vm_register(vm,"c_listen",ELSAPI_socket_c_listen); + vm_register(vm,"c_constinit",ELSAPI_socket_c_constinit); #ifdef ELS_CONF_TOKEN_EN vm_dostring(vm,LibScript); #endif -- Gitee From cf3402440ee0c209ee158cf25ea6b7ec09f73501 Mon Sep 17 00:00:00 2001 From: Matriller <1079561605@qq.com> Date: Thu, 11 Apr 2024 07:44:40 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0socket=E7=9A=84=E6=96=87?= =?UTF-8?q?=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matriller <1079561605@qq.com> --- socket/readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/socket/readme.md b/socket/readme.md index 8c2b5007..b0788413 100755 --- a/socket/readme.md +++ b/socket/readme.md @@ -4,7 +4,7 @@ # 简介 socket是一个跨平台的网络编程模块,用于向洛书提供套接字服务。 -# 变量 +# 常量 ## socket.AF_INET AF_INET指的是IPv4地址族 @@ -29,7 +29,7 @@ SOCK_STREAM指的是流式套接字 ### 注意事项 1. 该函数在错误输入除字符串外的一切数据时不会出错,但在输入某些错误字符串时会造成未知原因的程序无响应,请确保输入的字符串大致符合主机名的格式 -## socket.socket(family, type, protocol) +## socket.class(family, type, protocol) ### 参数 1. family指要创建的套接字的地址族,目前可选的有socket.AF_INET 2. type指要创建的套接字的类型,目前可选的有socket.SOCK_STREAM @@ -40,7 +40,7 @@ SOCK_STREAM指的是流式套接字 # 类 ## socket ### 创建方式 -+ 使用socket.socket函数创建 ++ 使用new(socket.Socket, ...)创建 ### 成员变量 #### sockfd + 保存一个指向socket_t的指针 -- Gitee