Author Topic: Server TCP Objects not functioning properly, client are fine - Solved  (Read 1716 times)

Simply put, whenever I attempt to make a server with a TCP object it fails.
Client ones, which connect to servers are fine.

One can plainly see the stuffload of echoes I have everywhere, but they rarely give me any useful information.

Code: [Select]
new TCPObject(lugsTCPServ);

function lugsTCPServ::onConnectRequest(%this, %ip, %socket)
{
   if(isObject(%this.connection[%ip]))
   {
      echo(%this.getName() @ ": Got duplicate connection from" SPC %ip);
      %this.connection[%ip].disconnect();
      %this.connection[%ip].delete();
   }
   echo(%this.getName() @ ": Creating connection to" SPC %ip);
   %this.connection[%ip] = new TCPobject("Client", %socket) { class = lugsTCPServ; parent = %this;};
}
 
function lugsTCPServ::onConnected(%this)
{
echo("SRVR:Connected");
}
 
function lugsTCPServ::onConnectFailed(%this)
{
echo("SRVR:Connection Failed");
}
 
function lugsTCPServ::onDisconnect(%this)
{
echo("SRVR:Disconnected");
}

function lugsTCPServ::onLine(%this, %line)
{
echo(%this);
echo(%line);
%mode = getword(%line, 0);
echo(%mode);
%msg = strReplace(%line, getword(%line, 0), "");
echo(%msg);
switch$(%mode)
{
case "CHAT:":
echo("SRVR:CHAT:" SPC %msg);
case "WARNMSG":
warn("SRVR:WARN:" SPC %msg);
default:
echo("SRVR:Message received, unknown mode:" SPC %line);
}
return;
}
lugsTCPServ.listen(8675);


solved: i needed to get onLine from "client" in the code.

see the part where there's "lugTCPServ::onLine"?

That needs to be Client::onLine, using the ambiguous (therefore poorly chosen) names above.
« Last Edit: May 07, 2012, 04:08:05 PM by Lugnut1206 »

Exactly how isn't it working?
Also, I recommend representing elements in packets using a single byte instead of a long string.

In a initialization function, you could define the protocol, i.e.:
%this.callback[ "\x01" ] = "kill_everyone";
%this.callback[ "\x02" ] = "delete";

Then, when receiving a packet, do a variable lookup on the first character.
« Last Edit: May 07, 2012, 03:46:44 PM by Port »

probably should've mentioned that
:c

as you can see by my lovely echoes, on my end it does this (i'm using putty in raw mode to connect to localhost:8675)

Blockland:
Code: [Select]
==>exec("base/tcpserver.cs");
Executing base/tcpserver.cs.
Binding server port to default IP
lugsTCPServ: Creating connection to IP:127.0.0.1:26054
Add-Ons/System_ReturnToBlockland/support/networking.cs (222): Unknown command onLine.
Add-Ons/System_ReturnToBlockland/support/networking.cs (222): Unknown command onLine.Add-Ons/System_ReturnToBlockland/support/networking.cs (319): Unknown command onDisconnect. //After closing PuTTY
PuTTY:
Code: [Select]
asdf
asdf

As you can see, it doesn't even echo the line, or %this, as specified in lugsTCPServer::onLine

well the problem is fairly obvious

what do you want the server to receive lines from?
  • the server
  • the client

Quote
%this.connection[%ip] = new TCPobject("Client", %socket) { class = lugsTCPServ; parent = %this;};
Quote
function lugsTCPServ::onLine(%this, %line)

before you go saying "there's the class thing"; class inheritance is broken for TCPObjects

function Client::onLine(%this, %line)

also name the client type more privately
don't give it such an ambiguous name that other mods might use

i don't understand, I need to do
Client::onLine?

i don't understand, I need to do
Client::onLine?

yes

you will never receive lines from yourself (the server)
you receive lines from clients

isn't this obvious

no
i figured that lugsTCPServ::onLine was called when lugsTCPServ received a line

not like
where it came from or something


does the class = lugsTCPServ thing need to exist
can i remove it, if I'm understanding you right you're saying it has no functionality
« Last Edit: May 07, 2012, 04:04:13 PM by Lugnut1206 »

no
i figured that lugsTCPServ::onLine was called when lugsTCPServ received a line

not like
where it came from or something

well that's clearly not what you were thinking
you used these arguments: %this, %line

how were you planning to figure out who sent it

well that's clearly not what you were thinking
you used these arguments: %this, %line

how were you planning to figure out who sent it
i figured it was more straight forward
i have no idea


yeah, now it's working exactly as intended. thanks port!
« Last Edit: May 07, 2012, 04:09:36 PM by Lugnut1206 »

and please do this

also name the client type more privately
don't give it such an ambiguous name that other mods might use

if you're going to be sending a lot of stuff you should do this because of the TCPObject implementation's poor quality

I recommend representing elements in packets using a single byte instead of a long string.

In a initialization function, you could define the protocol, i.e.:
%this.callback[ "\x01" ] = "kill_everyone";
%this.callback[ "\x02" ] = "delete";

Then, when receiving a packet, do a variable lookup on the first character.

and please do this
already done- lugsTCPServClient, it'll probably be more adeptly named based on any add-ons i make but whatever
if you're going to be sending a lot of stuff you should do this because of the TCPObject implementation's poor quality
i'm not clear, \xnumbers where numbers is what? ascii characters or something? what if i unintentionally use actual alphanumeric characters? can i echo those for more information? as in, will they print stuff?


\x01 is character 1, it's ASCII

I can get those in normal text if I do ALT+NumpadKeys, right?

I can get those in normal text if I do ALT+NumpadKeys, right?

Yes, but not all can be printed.