Raised This Month: $ Target: $400
 0% 

Solved problem with reading from socket


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



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 17:29.


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