It is the 1727th of March 2020 (aka the 21st of November 2024)
You are 3.138.69.101,
pleased to meet you!
mailto:blog-at-heyrick-dot-eu
DumbServer - writing a simple server for RISC OS
Following a discussion on the RISC OS Open forums, I thought I'd have a crack at writing a complete simple server for RISC OS.
Presented below is a program that opens port 123. If you connect to it, you will see a menu giving various options, and upon pressing a key, will give humorous responses. Only one connection can be established at a time, other connections will be told the server is busy.
This program compiles with the ROOL DDE. It makes use of the TCPIP libraries, and the standard C libraries. It is a proper multitasking program, though for simplicity it does not place an icon on the iconbar or anything like that. You can quit it from the TaskManager.
The code ought to be fairly self-documenting with the comments. If you think anything needs to be explained better, leave a comment. The exception is the FD_SET/FD_ISSET nonsense in check_listener(). This is because the socket code has its origins in Unix, and Unix is good at doing complicated things, but less good at simple things. You can easily check dozens of sockets at the same time, but you need to do all of this junk to check just one socket. Ho hum. Just copy it into your code, it works... ☺
There is no verification of WHO is connecting. You might want to ask for a username/password or somesuch.
It is fairly easy to extend this to cater for multiple concurrent connections. Turn mysock into an array, then you can support up to n connections.
If you want a proper telnet server, change the port to 23 and you may like to do some telnet/terminal negotiation. I believe Nettle (maybe others) will local echo at the moment. This can be disabled by the server by proper negotiation.
Alternatively, you can make a cheap'n'nasty web server by hanging on to port 80 (or 8080, usually) and waiting quietly until you see GET /filename (followed by some other rubbish, followed by a blank line). There is actually a whole load of things a browser will send you, and a bunch of responses it expects in return. Google will educate you.
Likewise, hang on to port 110 and you can fake being a POP3 server. Asides from encrypted connections, the underpinnings of many Internet protocols is to open a connection and push data back and forth in this sort of manner. Until recently when practically all decent mail servers expect SSL, it was possible to log in and check your mail using a telnet program and typing the commands in manually.
Rip out the assembler stuff and drop in some proper library code. I recommend DeskLib (well, I would...search this blog for why!). You can then add an iconbar icon, a menu, all the usual stuff.
The icon has been ripped from OmniClient and had "Duh" superimposed. This is because my ability to draw utterly crap icons is legendary. Well, maybe not legendary, but it should be. My drawing sucks. A blind three year old girl could make more convincing drawings of things she's never seen...using crayons...those really big ones.
And now, the bit you were waiting for. Click the picture for it...
Your comments:
Please note that while I check this page every so often, I am not able to control what users write; therefore I disclaim all liability for unpleasant and/or infringing and/or defamatory material. Undesired content will be removed as soon as it is noticed. By leaving a comment, you agree not to post material that is illegal or in bad taste, and you should be aware that the time and your IP address are both recorded, should it be necessary to find out who you are. Oh, and don't bother trying to inline HTML. I'm not that stupid! ☺ ADDING COMMENTS DOES NOT WORK IF READING TRANSLATED VERSIONS.
You can now follow comment additions with the comment RSS feed. This is distinct from the b.log RSS feed, so you can subscribe to one or both as you wish.
Colin, 5th January 2016, 16:32
A recv return value of 0 means the the remote end of the socket wont send any more data ie that it has shutdown the 'write' side of its socket - it isn't an error condition. The socket may still be open for you to write to it. It will return zero whether the socket is nonblocking or not.
Love the lengths you go to to avoid using _swix
Could simplify checklistener to just accept instead of select and accept. nonblocking accept returns ewouldblock if there are no queued connections
This web page is licenced for your personal, private, non-commercial use only. No automated processing by advertising systems is permitted.
RIPA notice: No consent is given for interception of page transmission.