Raised This Month: $32 Target: $400
 8% 

Solved problem with reading from socket


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nacknic
Senior Member
Join Date: Mar 2019
Old 08-11-2020 , 07:27   problem with reading from socket
Reply With Quote #1

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)
			
	}
	
}

Last edited by nacknic; 08-13-2020 at 01:47.
nacknic is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-11-2020 , 10:22   Re: problem with reading from socket
Reply With Quote #2

Are you trying to create a listening socket? Or you just sent a request to the server and expect some answer?
__________________

Last edited by HamletEagle; 08-12-2020 at 02:51.
HamletEagle is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 08-11-2020 , 10:44   Re: problem with reading from socket
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
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

Last edited by nacknic; 08-11-2020 at 10:44.
nacknic is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 08-12-2020 , 15:35   Re: problem with reading from socket
Reply With Quote #4

some1 ?
nacknic is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 08-12-2020 , 17:27   Re: problem with reading from socket
Reply With Quote #5

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.
__________________

Last edited by Black Rose; 08-12-2020 at 17:28.
Black Rose is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 08-12-2020 , 18:44   Re: problem with reading from socket
Reply With Quote #6

Quote:
Originally Posted by Black Rose View Post
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

Last edited by nacknic; 08-12-2020 at 18:50.
nacknic is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 08-13-2020 , 01:44   Re: problem with reading from socket
Reply With Quote #7

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)
		}
	}
}

Last edited by nacknic; 08-13-2020 at 01:47.
nacknic is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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