AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved problem with reading from socket (https://forums.alliedmods.net/showthread.php?t=326670)

nacknic 08-11-2020 07:27

problem with reading from socket
 
the problem:
socket_is_readable and socket_change not notice input from the socket.

Code:

#include <amxmodx>
#include <sockets>

#define ADDRESS "xx.xxx.xxx.xxx" // i hide my ip
#define PORT 1000 // not the real port

new g_Socket
new g_Data[1000]

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(1.0, "read_socket", TASK_READ,  _,  _, "b")
        return PLUGIN_HANDLED
       
}

public read_socket() {
       
        if(socket_is_readable(g_Socket)) { //i tried even with socket_change
               
                client_print(0, print_chat, "[AMX SERVER] socket change")
                new count_byte = socket_recv(g_Socket, g_Data, charsmax(g_Data))
               
                if(count_byte  == 0) {
                        client_print(0, print_chat, "[AMX SERVER]the socket is closed")
                } else if(count_byte  == -1) {
                        client_print(0, print_chat, "failed to receive message")
                } else if(count_byte  > 0) client_print(0, print_chat, "The Server: %s", g_Data)
                       
        }
       
}


HamletEagle 08-11-2020 10:22

Re: problem with reading from socket
 
Are you trying to create a listening socket? Or you just sent a request to the server and expect some answer?

nacknic 08-11-2020 10:44

Re: problem with reading from socket
 
Quote:

Originally Posted by HamletEagle (Post 2713838)
Are you trying to create a listening socket? Or you just sent a request to the server and except some answer?

listening socket, read data

nacknic 08-12-2020 15:35

Re: problem with reading from socket
 
some1 ?

Black Rose 08-12-2020 17:27

Re: problem with reading from socket
 
I'm having a hard time understanding what you wrote in your first post.
Try getting someone to translate it for you because Google Translate is not doing you any favors.

Also, don't double post.

nacknic 08-12-2020 18:44

Re: problem with reading from socket
 
Quote:

Originally Posted by Black Rose (Post 2714003)
I'm having a hard time understanding what you wrote in your first post.
Try getting someone to translate it for you because Google Translate is not doing you any favors.

Also, don't double post.

in short: i wrote server.
amx connect to the server.
amx listening with socket_change or socket_is_readable in task, but those functions not notice the input from the server, almost all the time.

im not using translate...
i edit the first message to one sentence

nacknic 08-13-2020 01:44

Re: problem with reading from socket
 
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)
                }
        }
}



All times are GMT -4. The time now is 13:46.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.