Sending message across network

Joined
Apr 21, 2008
Messages
1
Reaction score
0
Using netbeans i'm trying to get a message from one computer to another. Below is my sender and reciever code, can anyone tell me if im on the right track. First is sender second is reciever, i'm also having problems with cannot find symbol.

Cheers

package javaapplication1;

public class Main
{

public static void main(String[] argv)
{
try
{
InetAddress addr = InetAddress.getLocalHost();
int sendPortNumber = 4000;
int portNumber = 3000;

Command data = new Command("BUY","forks",10); // data to send

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);
}
}
}


public class ex10receiver
{
public static void main(String[] argv)
{
try
{
int portNumber = 3000;

byte[] buffer = new byte[1024];

// assume that machine and port to send to are in arg vector
if (argv.length == 1)
{
portNumber = Integer.parseInt(argv[1]);
}

// 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 " + data.toString());

}
catch (IOException ioe)
{
System.out.println("ERROR: " + ioe);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top