I've made a program to send a line of text from one computer to another, which is the receiver. To do this i have to run the receiver from one computer and on the other computer i run send and then the text appears on the receiver, however i want all this to happen automatically so i just run the program and it does both of the these programs, any ideas on how to do this thanks.
here are the codes for my programs
/* Program: ex10receiver Date: August 98 Version: 1 Use: to send a packet to a reciever */ import java.net.*; // for network import java.util.*; // for utilities import java.io.*; // for streams //import Command; public class Command02 { public static void main(String[] argv) { try { int portNumber = 3000; byte[] buffer = new byte[1024]; // assume that port to send to is in arg vector if (argv.length == 1) { portNumber = Integer.parseInt(argv[0]); } // now create and send packet DatagramPacket packet = new DatagramPacket(buffer,buffer.length); DatagramSocket socket = new DatagramSocket(portNumber); socket.setSoTimeout(0); System.out.println("ready to receive on " + socket.getLocalAddress().toString() + " port: " + socket.getLocalPort()); socket.receive(packet); Command data = new Command(packet.getData()); System.out.println("Received by " + data.toString()); System.out.println(" by "); } catch (IOException ioe) { System.out.println("ERROR: " + ioe); } } }
/* Program: ex10sender Date: August 98 Version: 1 Use: to send a packet to a reciever */ import java.net.*; // for network import java.util.*; // for utilities import java.io.*; // for streams //import Command; public class Command03 { public static void main(String[] argv) { try { InetAddress addr = InetAddress.getLocalHost(); //InetAddress addr = InetAddress.getByName("192.168.0.114");//IP address of recieving computer int sendPortNumber = 4000; int portNumber = 3000; Command data = new Command("Danny Meldrum","N0251665", 4); byte[] buffer = data.toString().getBytes(); // assume that machine and port to send to are in arg vector if (argv.length == 2) { try { addr = InetAddress.getByName(argv[0]); portNumber = Integer.parseInt(argv[1]); } catch (UnknownHostException uh) { addr = InetAddress.getLocalHost(); } } // now create and send packet DatagramPacket packet = new DatagramPacket(buffer,buffer.length,addr,portNumber); DatagramSocket socket = new DatagramSocket(sendPortNumber); System.out.println("ready to send on " + socket.getLocalAddress().toString() + " port: " + socket.getLocalPort()); socket.send(packet); System.out.println("Packet data sent"); } catch (UnknownHostException uh) { System.out.println("ERROR: " + uh); System.exit(0); } catch (IOException ioe) { System.out.println("ERROR: " + ioe); } } }
/* Program: ex10command Date: August 98 Version: 1 */ import java.util.*; class Command implements java.io.Serializable { public final static String Name = "Danny"; public final static String NTUNumber = "N0251665"; public final static String SHOW = "SHOW"; public String type; public String item; public int number; public Command(String type, String item, int number) { this.type = type; this.item = item; this.number = number; } public Command() { type = null; item = null; number = 0; } public Command(String data) { StringTokenizer st = new StringTokenizer(data,","); if (st.countTokens() < 3) { // empty data type = null; item = null; number = 0; } else { type = st.nextToken().trim(); item = st.nextToken().trim(); number = Integer.parseInt(st.nextToken().trim()); } } public Command (byte[] data) { this(new String(data)); } public String toString() { String sp = ","; return type + sp + item + sp + String.valueOf(number); } public byte[] toByte() { return toString().getBytes(); } }