Hice un chat entre servidores pero no funciona.
Lo que falla es el envío de datos, al menos es el único registro de problema que aparece. Ven algo mal?. Tengo otros plugins con sockets pero nada entre servidores por ende no sé que puede ser.
PHP Code:
#include <amxmodx>
#include <engine>
#include <sockets_hz>
#define PLUGIN "Sockets: Multi Chat"
#define VERSION "1.0"
#define AUTHOR "gladius"
new g_hSocket;
new g_iPort;
new g_msgSayText;
new g_MaxPlayers;
new g_szIPs[2][40] =
{
"201.238.222.69:27018",
"201.238.222.69:27019"
}
new g_server_ip[40]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
get_user_ip(0, g_server_ip, sizeof g_server_ip - 1);
g_iPort = get_cvar_num("port"); // Algunos usan #define DEFAULTPORT 18107 //Game: find why the port number is 18107
new iError;
g_hSocket = socket_listen("127.0.0.1", g_iPort, SOCKET_TCP, iError);
if(g_hSocket <= 0 || iError)
{
server_print("Error listening; Socket=%d Error=%d", g_hSocket, iError);
log_to_file("socketsss.txt", "Error listening; Socket=%d Error=%d", g_hSocket, iError);
}
/*else
{
server_print("Listening on %d" , g_hSocket);
//log_to_file("socketsss.txt", "Listening on %d" , g_hSocket);
}*/
socket_unblock(g_hSocket);
g_MaxPlayers = get_maxplayers();
g_msgSayText = get_user_msgid("SayText");
register_message(g_msgSayText, "Message_SayText");
new iEntity = create_entity("info_target");
if(is_valid_ent(iEntity))
{
new const szClassName[] = "SocketReader";
entity_set_string(iEntity, EV_SZ_classname, szClassName);
entity_set_float(iEntity, EV_FL_nextthink, get_gametime() + 0.1);
register_think(szClassName, "FwdSocketEntityThink");
}
else
{
server_print("socketsss.txt", "Could not create socket reader entity.");
log_to_file("socketsss.txt", "Could not create socket reader entity.");
plugin_end();
}
}
public plugin_end()
{
if(g_hSocket > 0)
{
socket_close(g_hSocket);
g_hSocket = 0;
}
}
public Message_SayText()
{
new szChannel[60];
get_msg_arg_string(2, szChannel, charsmax(szChannel));
if(equal(szChannel, "#Cstrike_Chat", 13))
{
if(equal(szChannel, "#Cstrike_Chat_All"))
{
new iError, hSocket;
new i;
new IP[40], Port[8];
for(i = 0; i < sizeof g_szIPs; i++)
{
if(equal(g_server_ip, g_szIPs[i]))
continue;
strtok(g_szIPs[i], IP, sizeof IP - 1, Port, sizeof Port - 1, ':');
hSocket = socket_open(IP, str_to_num(Port), SOCKET_TCP, iError);
if(hSocket <= 0 || iError)
{
server_print("Error opening; Socket=%d Error=%d IP=%s Port=%d", hSocket, iError, IP, str_to_num(Port));
log_to_file("socketsss.txt", "Error opening; Socket=%d Error=%d IP=%s Port=%d", hSocket, iError, IP, str_to_num(Port));
continue;
}
//server_print("[%d] Socket Connected", hSocket);
//log_to_file("socketsss.txt", "[%d] Socket Connected", hSocket);
socket_send(hSocket, szChannel, sizeof szChannel - 1);
socket_close(hSocket);
}
}
}
}
public FwdSocketEntityThink( iEntity )
{
new hSocket = socket_accept(g_hSocket);
if(hSocket > 0 || socket_change(g_hSocket, 1))
{
//socket_unblock(hSocket);
//server_print("Connection successfully accepted on %d, new socket=%d", g_hSocket, hSocket);
//set_task(1.0, "TaskReceiveData", hSocket);
//log_to_file("socketsss.txt", "Connection successfully accepted on %d, new socket=%d", g_hSocket, hSocket);
server_print("Connection successfully accepted on %d", g_hSocket);
log_to_file("socketsss.txt", "Connection successfully accepted on %d", g_hSocket);
Task_ReceiveData(g_hSocket)
}
else
{
//server_print("Error accepting connection on %d" , g_hSocket);
//log_to_file("socketsss.txt", "Error accepting connection on %d" , g_hSocket);
}
entity_set_float(iEntity, EV_FL_nextthink, get_gametime() + 0.1);
}
public Task_ReceiveData(hSocket)
{
new szData[1024];
socket_recv(hSocket, szData, charsmax(szData));
server_print("Data: %s", szData);
log_to_file("socketsss.txt", "Data: %s", szData);
for(new i; i < g_MaxPlayers; i++)
{
message_begin(MSG_BROADCAST, g_msgSayText)
{
write_byte(i)
write_string(szData)
write_string("")
write_string("")
}
message_end()
}
}
Log
Code:
L 12/18/2012 - 02:00:24: Error opening; Socket=-1 Error=3 IP: 201.238.222.69 Port: 27019
* Socket=-1 - Error.
* Error = 3 - couldn't connect to given hostname:port .
Estaba tratando de conectarme desde el server con puerto 27018 al 27019.
Ya revisé el plugin de Exolent y el plugin de Hacknizer, mi plugin es bastante similar a ellos solo se podría decir que cambia en la forma que metemos varias ips, por ahora lo tengo así ya que busco hacerlo funcionar primero.
__________________