solved:
1. timing the socket_change and socket_is_readable miss info from client, so needed to do task often.
2. reset the data array.
3. format the data
because its very often, the server got lags, conclusion its not good ay do its task.
just use write and wait for answer
Code:
#include <amxmodx>
#include <sockets>
#define ADDRESS "xx.xxx.xxx.xxx"
#define PORT xxxx
#define TASK_READ 1
new g_Socket
new g_Data[999]
public plugin_init() {
set_task(14.0, "open_socket")
}
public plugin_precache() {
socket_close(g_Socket)
}
public plugin_end() {
socket_close(g_Socket)
}
public plugin_cfg() {
socket_close(g_Socket)
}
public open_socket() {
new error
g_Socket = socket_open(ADDRESS, PORT, SOCKET_TCP, error)
if(error != 0) {
socket_close(g_Socket)
switch(error) {
case 1: client_print(0, print_chat, "[AMX SERVER] error while creating socket")
case 2: client_print(0, print_chat, " [AMX SERVER] couldn't resolve hostname")
case 3: client_print(0, print_chat, "[AMX SERVER] couldn't connect")
}
return PLUGIN_HANDLED
}
client_print(0, print_chat, "[AMX SERVER] socket opening was successful")
set_task(0.3, "read_socket", TASK_READ, _, _, "b")
return PLUGIN_HANDLED
}
public read_socket() {
g_Data[0] = '^0'
new countByte = 0
if(socket_is_readable(g_Socket)) {
countByte = socket_recv(g_Socket, g_Data, charsmax(g_Data))
if(countByte > 0) {
formatex(g_Data, charsmax(g_Data), "%s", g_Data)
client_print(0, print_chat, "[SERVER] %s", g_Data)
}
}
}