All pastes #375779 Raw Edit

Something

public java v1 · immutable
#375779 ·published 2007-02-28 16:38 UTC
rendered paste body
/*	Program: 	ex10sender	Date:		Febuary 06	Version:	1	Use:		to send a packet to a reciever*/import java.net.*;	// for network import java.util.*;	// for utilitiesimport java.io.*;	// for streamsimport Command;public class Sender{	public static void main(String[] argv)	{		try		{			InetAddress addr 	= InetAddress.getLocalHost();			int sendPortNumber 	= 4000;			int portNumber		= 3000;						Command data = new Command("BUY","forks",10);			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);		}	}}