This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tommyroyall's profile picture

Client-Server Connection Based Websites

Started by tommyroyall, 23 June 2012 - 11:57 PM
tommyroyall #1
Posted 24 June 2012 - 01:57 AM
Hey guys, I've made a client-server connection that can represent web-sites (text files that are sent over networks).

To use this, create a file called "server_data" without the quotes, and then fill it with whatever you want on the site (I think that so far, only letters, numbers and maybe punctuation is allowed. If you get an error, that probably means that punctuation isn't allowed) and then after the server_data file is created, launch the server program.

Then, to access that from a client, go on another computer and launch the client program, and then put in the ID of the host, and it'll connect you. Pinging is just a small feature that I made for testing the host.

Server Code: http://pastebin.com/rP5a169D
Client Code: http://pastebin.com/9sHZiVzU
MysticT #2
Posted 24 June 2012 - 02:26 AM
Just some things I'd like to point out:
Server:
1)

tostring(serverContent)
This is pretty pointless, since you'r not assigning it to a variable. It should be:

serverContent = tostring(serverContent)

2) You never change the state variable, so the loop will never end.

Client:

os.exit()
There's no os.exit function, it's os.shutdown or shell.exit.

And in both server and client, you use a recursive function to create a loop, that will crash the computer after some time. You should use an actual loop, a while true do loop should work:

-- define variables here
while true do
  -- code here
  -- use break to stop the loop
end
-- exit code here (if any)

It looks like you didn't test the program, cause they would crash in some cases, and for sure after some time (because of the recursive functions).

Also, you should try using else and elseif:

if <condition> then
  -- do something if the condition is true
else
  -- do something if the condition is false
end

if <condition> then
  -- do something if the condition is true
elseif <condition2> then
  -- do something if the second condition is true
else
  -- do something if both conditions are false
end
You can add as many elseif as you need.

Lastly, try to use local, since you have a lot of global variables, and it could mess your program and others.