Raised This Month: $ Target: $400
 0% 

Solved problem with reading from socket


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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
 



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:46.


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