jbanes I read ur reply and understood I need a network soket and a output screen library, but after that I got lost. :-/ Where did you start talking about a terminal and since its too complex to explain could you give me a site on where to read about that paragraph? I really have no clue on how to act on your post. Sry.
Sorry, I thought I was being clear. Let me try to simplify it:
Telnet is a protocol designed for users to connect to remote terminals such as Unix or IBM Mainframes. When the Internet came around (actually it started with BBSes, but I won't go there), gamers came up with an idea for a new sort of dungeon game. The concept was that instead of presenting telnet users with a command line interface, they'd provide them with a game screen. Thus MUDs were born.
Now if you look at the Telnet spec (you don't actually have to do that, I'm speaking figuratively) it basically says that you can consider it nothing more than sending ASCII characters back and forth. i.e. The server can read text from the socket and write text back. There's other features in the Telnet protocol, but those are largely ignored. It's really just an open socket with text going back and forth.
This is extremely good news, because in Java it's very simple to write ASCII strings to an output stream, and read them from an InputStream. i.e.:
1 2 3 4 5 6 7 8
| Socket socket = new Socket("mymud.com", 23); OutputStream out = socket.getOutputStream(); String message = new String("Hello World!\n");
for(int i=0; i<message.length; i++) { out.write(message.charAt(i); } |
That bit of code should send "Hello World!" plus a newline to the server. The server might respond like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ServerSocket server = new ServerSocket(23); Socket socket = server.accept(); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream();
String message = "Hello to you too!\n"; int c;
while((c = in.read()) >= 0 && c != '\n') { System.out.print((char)c); }
for(int i=0; i<message.length; i++) { out.write(message.charAt(i); } |
So the above code would call the server, send "Hello World!" and get back the response "Hello to you too!". Pretty cool, huh? The only problem is that the communication is going to be a bit dull. You see, if you were to use a telnet client to contact the server piece, the client would simply show the text scroll one line at a time. That's no fun if we want to have text animations!
The solution is a set of codes called "ANSI Escape Characters". These characters were originally IBM's attempt at making their terminals smarter with better looking programs. I don't think IBM ever realized how useful those codes would be for games! Here's a tested and debugged class for sending the cursor to the home position (upper left), setting a color, and printing out "Hello World!".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import java.io.*; import java.net.*;
public class HelloServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(23); Socket socket = server.accept(); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream();
String message = "Hello World!";
int color = 31; int c;
while((c = in.read()) >= 0 && c != 'q') { out.write(0x1B); writeMessage(out, "[0;0H");
out.write(0x1B); writeMessage(out, "["+color+"m");
writeMessage(out, message);
out.flush();
color++;
if(color > 49) color = 31; }
socket.close(); }
private static void writeMessage(OutputStream out, String message) throws IOException { for(int i=0; i<message.length(); i++) { out.write(message.charAt(i)); } } } |
Instructions:
1. Compile the class above
2. Run it by typing "java HelloServer" from the command line, or executing it in your IDE. It is now waiting for a connection.
3. Open your favorite telnet or MUD client. Windows Telnet won't work right, so if you don't have a good client, go grab
PuTTY4. Connect to "localhost" on port 23. (23 should be the default.)
5. Hit enter to see new colors. Hit 'q' and then enter to quit.
Any questions?