The Artima Developer Community
Sponsored Link

Java Answers Forum
Fun for nerds. (telnet)

2 replies on 1 page. Most recent reply: Apr 14, 2003 6:04 PM by Wayne Weeks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Wayne Weeks

Posts: 5
Nickname: wayneweeks
Registered: Apr, 2003

Fun for nerds. (telnet) Posted: Apr 12, 2003 12:31 AM
Reply to this message Reply
Advertisement
I will start this with a disclaimer. I know nothing about the area of interest. I am a starving college student working for a degree in software enginering. I do not know how many of you have ever played on a "MUD" but they are typicaly ran thru telnet. I would like to learn how to set up a server client relationship with multiple computers over the internet so that i can make my own. As you can see I am in dire need of a hoby. I do realize this is a vauge request; however, I do not expect an answer. If anyone can point me in the proper direction I would be very happy.

Wayne Weeks


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Fun for nerds. (telnet) Posted: Apr 12, 2003 7:17 PM
Reply to this message Reply
Setting up and running a multithreaded server is a piece of cake in java.

/*	AddressServer.java
*	@author: Charles Bell
*	@version: January 17, 2001
*	email: charles@quantumhyperspace.com
*
*	Problem Statement:
*	I've a problem!! Could You help me?
*	How can I retrieve Physical Address (Ethernet Address ) from Client
*	in Java Language or better from an HttpRequest?
*	Please, if You can send me any code.
*	 Thanks
*/
 
import java.io.*;
import java.net.*;
import java.util.*;
 
/**	When started allows multiple clients to connect. It listens on port 8189.
*	It returns whatever a connected client sends.
*/
public class AddressServer{
 
	/**	Instantiates an instance of the SimpleServer class and initilaizes it.
	*/
	public static void main(String[] args){
		AddressServer server = new AddressServer();
		server.init();
	}
 
	/**	Sets up a ServerSocket and listens on port 8189.
	*/
	public void init(){
		int connections = 1; // 
		ServerSocket serversocket = null;
		try{
			//establish a server socket monitoring port 8189 
			//port 8189 is not used by any services
			//but there are pleny to choose from
			serversocket = new ServerSocket(8189);
			System.out.println("Listening at IP Address: " + getMyIPAddress() + " on port 8189");
 
			for ( ; ; ){
				//wait indefinitely until a client connects to the socket
				Socket socket = serversocket.accept();
				System.out.println("MyIPAddress: " +  getMyIPAddress());
				System.out.println("Server: " +  socket.getLocalAddress().getHostAddress());
				System.out.println("Client: " + socket.getInetAddress().getHostAddress());
				System.out.println("Spawning connection number: " + connections);
				AnotherServerThread serverthread = new AnotherServerThread(socket,connections);
				serverthread.start();
				connections++;
			}
 
		}catch(UnknownHostException unhe){
			System.out.println("UnknownHostException: " + unhe.getMessage());
		}catch(InterruptedIOException intioe){
			System.out.println("Timeout while attempting to establish socket connection.");
		}catch(IOException ioe){
			System.out.println("IOException: " + ioe.getMessage());
		}finally{
			try{
				serversocket.close();
			}catch(IOException ioe){
				System.out.println("IOException: " + ioe.getMessage());
			}
		}
	}
 
	/**	Returns the string value of the user's IP Address.
	*/
	public String getMyIPAddress(){
		String myIpAddress = "";
		try{
			myIpAddress = InetAddress.getLocalHost().getHostAddress();	
		}catch(UnknownHostException unhe){
				System.err.println("Exception: " + unhe.getMessage());		
		}catch(Exception e){
				System.err.println("Exception: " + e.getMessage());		
		}
		return myIpAddress;
	}
		
	//inner class
	class AnotherServerThread extends Thread{
		private int counter;
		private Socket socket;
	
		AnotherServerThread(Socket s, int c){
			this.socket = s;
			this.counter = c;
		}
 
		public void run(){
			try{
				//set up communications for sending and receiving lines of text data
				//establish a bufferedreaderr from the input stream provided by the socket object
				InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
				BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
 
				//establish an printwriter using the output streamof the socket object
				//and set auto flush on		
				PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
 
				//for binary data use
				// DataInputStream and DataOutputStream
 
				//for serialized objects use
				// ObjectInputStream and ObjectOutputStream
 
				String datetimestring = (Calendar.getInstance()).getTime().toString();
				printwriter.println("You connected to the Address Server at " + datetimestring);
				printwriter.println("You connected to the Server at IP Address: " + getMyIPAddress());
				// a second version of the same thing as on above line
				printwriter.println("You connected to the Server at IP Address: " + socket.getLocalAddress().getHostAddress());
				printwriter.println("You are Client at IP Address: " +  socket.getInetAddress().getHostAddress());
				printwriter.println("Send Bye to disconnect.");
 
				String lineread = "";
				boolean done = false;
				while (((lineread = bufferedreader.readLine()) != null) && (!done)){
					System.out.println("Received from Client: " + lineread);
					printwriter.println("You sent: " + lineread);
					if (lineread.compareToIgnoreCase("Bye") == 0) done = true;
				}
				System.out.println("Closing connection.");
				bufferedreader.close();
				inputstreamreader.close();
				printwriter.close();
				socket.close();
			}catch(UnknownHostException unhe){
				System.out.println("UnknownHostException: " + unhe.getMessage());
			}catch(InterruptedIOException intioe){
				System.out.println("Timeout while attempting to establish socket connection.");
			}catch(IOException ioe){
				System.out.println("IOException: " + ioe.getMessage());
			}finally{
				try{
			  		this.socket.close();
				}catch(IOException ioe){
					System.out.println("IOException: " + ioe.getMessage());
				}
			}
		}	
	}
}
 
 
class AddressClient{
 
	//this will have to be changed if the server has a different address
	//String serverurl = "127.0.0.1"; //use this for local
	String serverurl = "148.193.14.109";
 
	int serverport = 8189;
 
	/**	Instantiates an instance of the AddressClientclass and initiliazes it.
	*/
	public static void main(String[] args){
		AddressClient client = new AddressClient();
		client.init();
	}
 
	/**	Connects to the AddressServer on port 8189 and sends a few demo lines
	*	to the server, and reads, displays the server reply,
	*	then issues a Bye command signaling the server to quit.
	*/
	public void init(){
		Socket socket = null;		
		try{
			System.out.println("Connecting to " + serverurl + " on port " + serverport);
			socket = new Socket(serverurl,serverport);
			//Set socket timeout for 10000 milliseconds or 10 seconds just 
			//in case the host can't be reached
			socket.setSoTimeout(10000);
			System.out.println("Connected.");
			
			InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
			BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
			//establish an printwriter using the output streamof the socket object
			//and set auto flush on		
			PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
			printwriter.println("Hey howze it going today!");
			printwriter.println("This is some anonymous user on the client side.");
			printwriter.println("Let's see what happens when I type this.");
			printwriter.println("Oh well, I've had enough.");
			printwriter.println("Bye");
			System.out.println("The server should now close its connection with you as client");
			String lineread = "";
			while ((lineread = bufferedreader.readLine()) != null){
				System.out.println("Received from Server: " + lineread);
			}
			System.out.println("Closing connection.");
			bufferedreader.close();
			inputstreamreader.close();
			printwriter.close();
			socket.close();
			System.exit(0);
 
		}catch(UnknownHostException unhe){
			System.out.println("UnknownHostException: " + unhe.getMessage());
		}catch(InterruptedIOException intioe){
			System.out.println("Timeout while attempting to establish socket connection.");
		}catch(IOException ioe){
			System.out.println("IOException: " + ioe.getMessage());
		}finally{
			try{
				socket.close();
			}catch(IOException ioe){
				System.out.println("IOException: " + ioe.getMessage());
			}
		}
	}
}
 
 

Wayne Weeks

Posts: 5
Nickname: wayneweeks
Registered: Apr, 2003

Re: Fun for nerds. (telnet) Posted: Apr 14, 2003 6:04 PM
Reply to this message Reply
THANKS

I think that has to be the most in depth answer i have ever recieved on a message board.

Flat View: This topic has 2 replies on 1 page
Topic: determining mulitples Previous Topic   Next Topic Topic: 2 really easy questions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use