lua socketクライアントlua string.pack文字列パッケージ

18644 ワード

https://www.runoob.com/manual/lua53doc/manual.html#6.4.2
<:       
>:       
=:          
![n]:          n (          )
b:         (char)
B:         (char)
h:       short (    )
H:       short (    )
l:       long (    )
L:       long (    )
j:    lua_Integer
J:    lua_Unsigned
T:    size_t (    )
i[n]:    n    (       )     int
I[n]:    n    (       )     int
f:    float (    )
d:    double (    )
n:    lua_Number
cn: n          
z:        
s[n]:          ,         n   (     size_t)        。
x:        
Xop:     op      (        )      
' ': (  )  

たとえば
local text2 = "123456789012"
--  " > "       s2    2           unsigned short
local text = string.pack(">s2", text2)
string.pack (fmt, v1, v2, ···)
       (          ) v1, v2          。     fmt      (   §6.4.2)。

string.unpack (fmt, s [, pos])
      fmt (   §6.4.2)        s (   string.pack)    。    pos(    1 )     s        。        ,     s            。

luaクライアント
local socket = require("socket")

local host = "127.0.0.1"
local port = 9999

local sock = assert(socket.connect(host, port)) 
print("connect ok")


function m_send(buf)
    local text = string.pack(">s2", buf)--  2     
    sock:send(text)
end

function m_recv1(buf)
    local text = string.unpack(">s2", buf, 1);
    return text
end

function m_recv2(buf)
    --    2     ,         
    local s = buf:byte(1) * 256 + buf:byte(2)

    --      
    if #buf < (s+2) then
        return nil
    end

    --   
    return buf:sub(3, 2+s)
end

local msg = "hello world"
m_send(msg)

local t ,status , buf = sock:receive()
--print(sock:receive())
local data = m_recv2(buf)
print(data)

data = m_recv1(buf)
print(data)


Cサービス側
#include
#include
#include
#include
#include
#include

#define SIZE 300

int main(int argc,char *argv[])
{
	int server_sock,client_sock,r;
	struct sockaddr_in server_addr,client_addr;
	socklen_t len;

	server_sock = socket(AF_INET,SOCK_STREAM,0);

    // bind ,            	bind error: : Address already in use
	const int on=1;
	if (setsockopt(server_sock,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on))
	{
		printf("setsockopt
"
); exit(1); } memset(&server_addr,0,sizeof server_addr); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(9999); if(-1 == bind(server_sock, (struct sockaddr*)&server_addr,sizeof(server_addr))) { printf(" bind error
"
); return -1; } if(-1 == listen(server_sock, 10)) { printf(" listen error
"
); return -1; } char *buf = (char*)malloc(200); if(!buf){ fprintf(stderr,"malloc error"); return 1; } memset(&client_addr,0,sizeof client_addr); len = sizeof client_addr; client_sock=accept(server_sock,(struct sockaddr*)&client_addr,&len); printf("new client ip: %s ,port: %d
"
, inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port)); memset(buf,0,SIZE); r = read(client_sock,buf,SIZE-1); buf[r]='\0'; write(client_sock,buf,r); // unsigned short t = *(unsigned short *)buf; t = ntohs(t); printf("head: %u
"
,t); printf("data: %s
"
,&buf[2]); close(client_sock); close(server_sock); return 0; }

結果:
new client ip: 127.0.0.1 ,port: 41394
head: 11
data: hello world
ubuntu@VM-0-14-ubuntu:~/skynet/test$ lua lua_test.lua 
connect ok
hello world
hello world