Last week, during the prac 2 of CS IB, we were asked to program in Java to build a simple client/server system in which the server takes “commands” from the client and executes them. Due to the time restriction, we were only asked to implement the server, and use telnet to act as client to transact with server. You can have a look here for details.
ServerSocket server = new ServerSocket(6666);
Socket s= server.accept();
// We now have a client on the end of the socket s
BufferedReader in = new BufferedReader( new InputStreamReader( s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
It’s no more than the knowledge above. Maybe the simplest nextwork program in Java world.
The telnet on lab’s Mac just work well, but after I brought my program to home and run on my own computer, I met a really troublesome problem with the Microsoft’s telnet in Windows XP.
1. First, I started my server.
2. Then, I opened the console in XP. Typed in the telnet command: telnet localhost 6666 (6666 is my server port number).
3. Server side msg indicated that I successfully connected.
4. How about the client side?
The nice bug come out! When you are typing, you can’t see what you have typed. Why is that? The Google says: When you type your password, local echo is switched off so others cannot see what you are typing. Unfortunately, Telnet fails to switch it back on again, and fails to accept any further entries!
5. solution: press “Ctrl + ]”, then input “set localecho”, and hit return twice. You should now be able to see what you type.
6. The whole transaction of my Online Shop Server looks like this (You can check the detailed requirements):
- Create account
- List the products
- Add the corresponding products to shopping basket
- Commit the transaction
- Terminate the session
7. When use “QUIT” command, the connection is closed. But my server keep running.
8. Really solved the problem? Unfortunately, you will need to repeat step 5 each time when you enter console. That’s really noising. But I found a really sweet alternative program, called PuTTY.
blah, blah, blah just four steps
trying to use PuTTY as Telnet to connect my server…
9. Connected again.
10. Very pleasant interface.
If you have interests, test my code yourself.