Can Java do quick input Forms?

Hi, for a Java test I'm going to have my mom run on her Macbook for me, I'll ask her to hit run-run in Eclipse, but then I'd like a dialog to come up for her to enter the number of clients to spawn, and also a radio button of possible IP addresses my server will be at, since it sometimes changes spontaneously in my router.

Must I do this in an awt frame with standard awt components and listeners, or is there a simple way to ask client setup questions? I don't want her to have to edit code!


Thanks
LVL 1
beavoidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
and also a radio button of possible IP addresses my server will be at, since it sometimes changes spontaneously in my router.

why go to all that bother when you can find out yourself what IP address you have at any given time from sites like whatsmyip?
beavoidAuthor Commented:
It's not about finding out what ip address it is, I need the client to be told what the server address is
CEHJCommented:
I need the client to be told what the server address is
Is the client on the same LAN?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

krakatoaCommented:
It's not about finding out what ip address it is, I need the client to be told what the server address i

That's precisely what I am saying.
beavoidAuthor Commented:
My server is being coded on my LAN
My mother will be running the test on her Macbook at her house.

So, to the question being about quick input, what class / window system should I use for my mother to enter the numbers, select items?
krakatoaCommented:
Assuming your code works on a LAN, once you are out on the 'net with it, you'll have to face the fact that the IP address is liable to change between sessions, so if your show isn't website-based, then you'll need something like Homingbeacon to allow clients to find your server. The alternative - which you seem to be suggesting - it to trawl through possible IP addresses that your ISP might allocate to you, which sounds like a real bind.
CEHJCommented:
My server is being coded on my LAN
My mother will be running the test on her Macbook at her house.
In that case, the following doesn't make sense
a radio button of possible IP addresses my server will be at, since it sometimes changes spontaneously in my router.
Unless you're your own ISP, you can't know what other publicly routeable addresses your router might get. You should probably use a dynamic DNS service like DynDNS (.com)
beavoidAuthor Commented:
Just a quick comment to keep this question alive before I try mores solutions, thanks
beavoidAuthor Commented:
The IP address at whatsmyip.com is different than my actual IP address
krakatoaCommented:
The IP reflected from that source will be the public-facing IP of your router. Any machine attached to that router will have, in all likelihood, an individual "NAT" IP address, something like 192.xxx.xxx.xxx, which is probably what you are referring to. You can look in your router's tables to see if the public IP is the same to satisfy your doubts.
beavoidAuthor Commented:
Thanks
My machines do all start with 192.168

I understand why TCP can't be expected to be guaranteed delivery, to anywhere, even though we were taught that (told that by my Novell supervisor). Security. I guess he just meant in our floor of building C.
But, I am concerned that all the web-start client players of my game will have this problem.
In the DMZ setup page, no selections seem to open the specified address to the Internet, even though it claims it will. - The DMZ "enable" and "disable" drop-down , -  frustrating
The UNCW firewall will probably never allow my dad to be a client?
Same with the computer science department?
I will definitely  use a dynamic DNS system, as my router address changes like the wind.
Thx
CEHJCommented:
I understand why TCP can't be expected to be guaranteed delivery, to anywhere, even though we were taught that (told that by my Novell supervisor).
You misunderstood. The guarantee applies AFTER routing has been OKd and just means no packets will be dropped.

But, I am concerned that all the web-start client players of my game will have this problem.
We've been through this. The solution is to only have one server for n games, so that the problem will only have to be solved once.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:)
krakatoaCommented:
I was toying around with some still unfinished networking code, intended to eventually assist with reachability analysis, but it is still incomplete. Is there any point my posting it for you here?
beavoidAuthor Commented:
Please do. I would really appreciate that
Thank you
krakatoaCommented:
It might not get you much further forward, but it could be deepened.
krakatoaCommented:
import java.io.*;
import java.util.*;
import java.net.*;
import java.nio.charset.*;
import javax.swing.*;


class CSPackage implements Serializable {

		static int datagram_incoming_tally;
		DatagramSocket dgs;
		static int nummes=0;
		String protocol;
		CSPackage csp;
		String networkEntity;
		public final static Hashtable<Integer, ServerThread> servers = new Hashtable<Integer, ServerThread>();
		public final static Hashtable<Integer, IDObject> ids = new Hashtable<Integer, IDObject>();
		ServerSocket mainServerSocket;
		ServerSocket idServerSock;
		InetAddress mainServer_PublicIPAddress;

	public static void main(String[] args){

		CSPackage csp = new CSPackage();

	}

	public CSPackage(){
	
				/*try{
			
					URL url = new URL("http://www.whatsmyip.org/");
					HttpURLConnection httpURLconnection = new myHttpURLConnection(url);
					try{
					  url.openConnection();
					  httpURLconnection.connect();
					}catch(IOException ix){ix.printStackTrace();}
				
				}catch (MalformedURLException mfux){}*/
				
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter \"Server\" or \"Client\" to launch either a server or a client");
		networkEntity = new String(scanner.next());
		if(!(networkEntity.equalsIgnoreCase("CLIENT")||networkEntity.equalsIgnoreCase("SERVER"))){System.out.println("Kindly try again.");System.gc();csp=new CSPackage();}
		System.out.println("Enter \"TCP\" or \"UDP\" or \"BOTH\" for protocol");
		protocol = new String(scanner.next());
		if(!(protocol.equalsIgnoreCase("TCP")||protocol.equalsIgnoreCase("UDP")||protocol.equalsIgnoreCase("BOTH"))){System.out.println("Kindly try again.");System.gc();csp=new CSPackage();}
		
		
		switch (networkEntity.toUpperCase()){
		
			case "SERVER": networkEntity = "SERVER";
					break;
			case "CLIENT": networkEntity = "CLIENT";
					break;	
		}
		
		switch (protocol.toUpperCase()){
		
			case "TCP": protocol = "TCP";
					break;
			case "UDP": protocol = "UDP";
					break;
			case "BOTH": protocol = "BOTH";
					break;					
		}

		switch (networkEntity){
		
			case "SERVER":
			
					new Server(30000,31000);
					
				break;
				
			case "CLIENT":
			
					new Client(protocol);
					
				break;
		}
		
	}
	
	
	class myHttpURLConnection extends HttpURLConnection {
	
			public myHttpURLConnection(URL url)throws MalformedURLException{
			
			
					super(url);
					try{
					JEditorPane website = new JEditorPane(url);
					website.setEditable(false);
					
					JFrame frame = new JFrame("IP Scraper");
					frame.add(new JScrollPane(website));
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setSize(800, 600);
					frame.setVisible(true);
					}catch(Exception webx){}
				
			///////////////////////////////////////////////////////
			try{
			Enumeration e = NetworkInterface.getNetworkInterfaces();
			while(e.hasMoreElements())
			{
				NetworkInterface n = (NetworkInterface) e.nextElement();
				Enumeration ee = n.getInetAddresses();
				while (ee.hasMoreElements())
				{
					InetAddress i = (InetAddress) ee.nextElement();
					System.out.println(i.getHostAddress());
				}
			}
			}catch(Exception exception){}
			///////////////////////////////////////////////////////
				
				
			}
			
			public boolean usingProxy(){
				return false;
			}
			
			public void disconnect(){
			
			}
			
			public void connect() throws IOException{
			
			
				System.out.println(this.getRequestMethod());
				
				
			}
	
	}
	
	
	class PeerClient implements Runnable{
	
	byte[] ba;
	DatagramSocket dtagrsock;
	DatagramPacket dgp;
	String msg;
	
		public PeerClient(){
			try{
			dtagrsock = new DatagramSocket(33000);
			}catch(SocketException sox){sox.printStackTrace();}
		
		}
	
		
		public void run(){
		
			while(CSPackage.this.hashCode()>0){
		
				dgp = new DatagramPacket(ba,65000);
				try{
				dtagrsock.receive(dgp);
				msg = new String(dgp.getData());
				}catch(IOException ioxs){ioxs.printStackTrace();}	
		
			}
		
		
		}
	
	}
	
	public class IDReceiver implements Runnable{
	
	CSPackage.Client cli;
	
	public IDReceiver(CSPackage.Client cl){this.cli = cl;}
	
	public void run(){
	
		while(true){
								try{
								cli.idlist = (Hashtable)cli.ips.readObject();System.out.println("Size of received list : "+cli.idlist.size());
								cli.idlist.remove(cli.ido.hashco);
								System.out.println("Size of amended list : "+cli.idlist.size());	
								}catch(Exception clix){}								
								Enumeration iter = cli.idlist.keys();
								while(iter.hasMoreElements()){System.out.println(iter.nextElement().hashCode());}
		}
	
	}
	
	}
	
	public class Client implements Runnable{
	
		private int datagram_output_tally;
		private BufferedReader br;
		private PrintWriter pw;
		private DatagramPacket dgp;
		private DatagramSocket dgs;
		private InetAddress clientInetAddress;
		private Socket clientSocket_TCP;
		private Socket clientIDsock;
		private int clientPort_TCP;
		private int clientPort_UDP;
		private Hashtable<String,Client> peerHashtable;
		private String protocol;
		private byte[] bA ;
		private byte[] announcer;
		private String message;
		private String lentosend;
		private ObjectOutputStream ops;
		public ObjectInputStream ips;
		private boolean idsubmitted;
		private String ip;
		private String msgBack;
		public IDObject ido;
		public Hashtable idlist;
		private CSPackage.IDReceiver idreceiver;
		
				
		public Client(String proto){
		
			
			System.out.println("Input the public IP address of client's host");
			Scanner sc = new Scanner(System.in);
			ip = sc.next();
			
			try{
				String clientNATIP = InetAddress.getLocalHost().getHostAddress();
				System.out.println(clientNATIP);
			}catch(Exception xx){}
		
			try{try{
			clientSocket_TCP = new Socket("localhost",30000);
			clientIDsock = new Socket("localhost",31000);
			
			dgs = new DatagramSocket();
			}catch(UnknownHostException unhe){}
			}catch(IOException unhe){}
			this.run();
		
		} 
		
		public void run(){
		
			Scanner sc = new Scanner(System.in);
			message=null;
			try{
			pw = new PrintWriter(new OutputStreamWriter(clientSocket_TCP.getOutputStream(), "UTF-8"),true);
			br = new BufferedReader(new InputStreamReader(clientSocket_TCP.getInputStream()));
			
			ops = new ObjectOutputStream(clientIDsock.getOutputStream());
			ips = new ObjectInputStream(clientIDsock.getInputStream());
			
			/////////************************************************
			ido = new IDObject(33000,ip,InetAddress.getLocalHost().getHostAddress());
			/////////************************************************
			
			idreceiver = new CSPackage.IDReceiver(this);
			Thread clth = new Thread(idreceiver);
			clth.start();
			
			if(!idsubmitted){
				pw.println("/ID");
				try{
				  ops.writeObject(ido);
				  ops.flush();
				  idsubmitted=true;
			
				}catch(Exception ixc){}
						
			}
			
			}catch(IOException pwx){pwx.printStackTrace();}
			
			while(true){
				message = "";
				System.out.println("Type a message to the server . . .");
				message = new String(sc.nextLine());
				if(message.trim().equals("/ID")){continue;}
				pw.println(message.trim());
				
				try{
									
					msgBack = br.readLine().trim();
					System.out.println(msgBack);
					
				}catch(Exception io){io.printStackTrace();}
				
				if(!(message.trim().equals("/burst"))){
				
				  //if(protocol.equals("UDP")||protocol.equals("BOTH")){
				
					
					announcer = new byte[(Integer.toString(message.length())).length()];
					bA = new byte[message.length()];
					lentosend = String.valueOf(bA.length);
					
					
					dgp = new DatagramPacket(announcer,announcer.length);
					dgp.setData(lentosend.getBytes());
					dgp.setAddress(clientSocket_TCP.getInetAddress());
					dgp.setPort(clientSocket_TCP.getPort());//remote port on server
					try{
						dgs.send(dgp);
					}catch(Exception iox){iox.printStackTrace();}
					
				
					dgp = new DatagramPacket(bA,message.length());
					dgp.setData(message.getBytes());
					dgp.setAddress(clientSocket_TCP.getInetAddress());
					dgp.setPort(clientSocket_TCP.getPort());//remote port on server
					datagram_output_tally++;
					
				try{
					dgs.send(dgp);
				}catch(Exception iox){iox.printStackTrace();}
				
				
				  System.out.println("DatagramPackets sent so far : "+datagram_output_tally);
				  //}
				}
				else{for(int g=0;g<4999;g++){ //send a burst of 5000 datagram packets
				
						announcer = new byte["/burst".length()];
						bA = new byte["/burst".length()];
						lentosend = String.valueOf(bA.length);
					
						dgp = new DatagramPacket(announcer,Integer.parseInt(lentosend));
						dgp.setData(lentosend.getBytes());
						dgp.setAddress(clientSocket_TCP.getInetAddress());
						dgp.setPort(clientSocket_TCP.getPort());//remote port on server
						try{
							dgs.send(dgp);
						}catch(Exception iox){iox.printStackTrace();}
					
				
						dgp = new DatagramPacket(bA,"/burst".length());
						dgp.setData("/burst".getBytes());
						dgp.setAddress(clientSocket_TCP.getInetAddress());
						dgp.setPort(clientSocket_TCP.getPort());
						datagram_output_tally++;
					try{
						dgs.send(dgp);
					}catch(Exception iox){iox.printStackTrace();}
				
				
					System.out.println("DatagramPackets sent so far : "+datagram_output_tally);
				
				}}
			}
		
		}
		
		
	}
	
	public class ServerThread implements Runnable, Serializable{
	
		private String messageIn;
		private Scanner messageReader;
		private InetAddress serverInstanceInetAddress;
		private Socket serverInstanceSocket;
		private Socket idSocket;
		private int port;
		private BufferedReader br;
		public PrintWriter pw;
		private UDPThread udpthread;
		private Thread udpHandler;
		private ObjectInputStream idInputStream;
		public ObjectOutputStream oouts;
		private boolean idreceived;
		
		
		
		public ServerThread(int port, Socket handedoffSocket, Socket idsock) throws IOException{
		
			try{
			
				this.port = port;
				this.serverInstanceSocket = handedoffSocket;
				this.idSocket = idsock;
				
				pw = new PrintWriter(new OutputStreamWriter(serverInstanceSocket.getOutputStream(), "UTF-8"),true);
				br = new BufferedReader(new InputStreamReader(serverInstanceSocket.getInputStream()));
				
				oouts = new ObjectOutputStream(idSocket.getOutputStream());
				idInputStream = new ObjectInputStream(idSocket.getInputStream());
				
			}catch(Exception gaff){gaff.printStackTrace();}
			
			if(protocol.equals("BOTH")||protocol.equals("UDP")){
			
				udpthread = new UDPThread();
				udpHandler = new Thread(udpthread);
				udpHandler.start();
			
			}
			
			System.out.println(++nummes); //number of clients
			
		}
		
		public void run(){
		
					servers.put(this.hashCode(),this);
					
					System.out.println("Serverthread running. . .");
				
					try{
						
						while(!((messageIn = br.readLine().trim())==null)){
						
							if(messageIn.equals("/p2pjoin")){if(idreceived){p2pjoin();}} //todo : this should be called on a separate thread
						
							else if(messageIn.equals("/ID")&&!idreceived){
							
								handleID(); //todo : this should be called on a separate thread
							}
							
							else{
					
								System.out.println("TCP-handled msg reads . . . "+messageIn.trim());
								pw.println("/cont");
							
							}
							messageIn = "";
						}
						
					}catch(Exception exxc){exxc.printStackTrace();}
		}
	
				private void p2pjoin(){
				
					Enumeration enu = servers.elements();
					System.out.println("servers size is "+servers.size());
					while(enu.hasMoreElements()){
					
						
						ServerThread sth = (ServerThread)enu.nextElement();
												
						try{
						sth.pw.println("/peergroupupdate");
						sth.oouts.writeObject(ids);
						sth.oouts.reset();
						}catch(Exception t){}
					
					}
					
				
				}
	
	
		class UDPThread implements Runnable{
		
			private byte[] bar;
			private byte[] announcedlength;
			private byte[] incomingBytes;
			private DatagramPacket dgp;
			
			
			public UDPThread(){
			
			}
		
			public void run(){
			
				System.out.println("UDP Handler running");
								
						while(true){
						
						try{
						
						synchronized(dgs){
							announcedlength = new byte[65507];
							dgp = new DatagramPacket(announcedlength,announcedlength.length);
							dgs.receive(dgp);
							
							bar = dgp.getData();
							String barstring =  new String(bar).trim();
							bar = barstring.getBytes();
							
							System.out.println("Number of bytes expected : . . . "+barstring);
							
							incomingBytes = new byte[Integer.parseInt(barstring)];
							
							dgp = new DatagramPacket(incomingBytes,incomingBytes.length); 
							
							dgs.receive(dgp);
							
							bar = dgp.getData();
														
								System.out.println("UDP-handled msg reads . . . "+new String(bar));
								datagram_incoming_tally++;
								System.out.println("DatagramPackets received so far : "+datagram_incoming_tally);
						}		
						}catch(Exception exxc){exxc.printStackTrace();}	
						}
						
			}
		
		}
		
		
		private void handleID(){
		
				System.out.println("Handling the client's ID object");
				try{
					Object o = idInputStream.readObject();
					ids.put(((IDObject)o).hashco,(IDObject)o);
					idreceived=true;
				}catch(Exception oix){oix.printStackTrace();}
				
				Enumeration en = ids.elements();
				while(en.hasMoreElements()){System.out.println(((IDObject)en.nextElement()).hashco);}
				
		}
		
	}

	
	
	public class Server implements Runnable {
	
	boolean keeprunning = true;
	int port;
	int idport;
	ServerSocket ss;
	ServerSocket sid;
	Socket handoffSock;
	Socket sidsock;
	
	
	public Server(int port, int idport){
	
		try{
			dgs = new DatagramSocket(30000);
		}catch(SocketException socxx){socxx.printStackTrace();System.exit(1);}		
		this.port = port;	
		this.idport = idport;
		this.run();
	}
	
	public void run(){
	
		try{
			ss = new ServerSocket(port);
			sid = new ServerSocket(idport);
		}catch(IOException ioex){}
	
		while(keeprunning){
			
			try{
				handoffSock = ss.accept();
				
				sidsock = sid.accept();
							
				Thread st = new Thread( new ServerThread(this.port,handoffSock,sidsock));
						
				st.start();
				
			}catch(Exception isx){}		
		}
	
	}
	
	}
	
	
	static class PortScanner {
		
		
		public void scan(){
			Socket socket;
			
				for (int port = 1; port <= 65535; port++) {
				try {
					socket = new Socket();
					socket.connect(new InetSocketAddress("localhost", port), 1);											
					System.out.println("Port " + port + " is open");
				} catch (Exception ex) {}
		}
	}
}
	

}

Open in new window


import java.io.*;
import java.net.*;

public class IDObject implements Serializable{
	
		
		public int myUDPPort;
		public String myPublicIP;
		public String myNATIP;
		public String host;
		public Integer hashco;
		
				
		
		public IDObject(int udpport, String publicip, String natip){
		
			
			this.myUDPPort = udpport;
			this.myPublicIP = publicip;
			this.myNATIP = natip;
			this.hashco = (int)System.currentTimeMillis();
			
		
			
			
		
		}
		
		public int getHashCode(){
		
			return(this.hashCode());
		
		}
		
	
	
	}

Open in new window


Read next comment.
krakatoaCommented:
I have made signif. changes to the above code. What now happens is that (over the loopback, (which would need to just be changed to point at an internet host instead)), the client(s) send messages to the server which reflects them in TCP and UDP printouts. Each client silently sends an inchoate IDObject to the server, which is stored in a hashtable. When a client sends "/p2pjoin" to the server as a message, the server sends that hashtable to each client, and the client removes itself from the received hashtable.

The idea of the client holding this hashtable, is that this could be used as the contact params for the other clients, and enable p2p networking. I may do some more on this at a later stage.

If a client sends the string "/burst" to the server, that client then sends 5000 UDP packets to the server, containing the string "/burst". This may or may not be useful in looking at UDP connectivity. A tally is kept of the number of packets sent.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.