Ryanman
Ryanman

Reputation: 880

Beginner Java Socket Programming Errors

I'm starting to write my first Java networking program, and long story short I'm having difficulty making sure that I'm taking the right approach. Our professor has given us a server program to test against this UDP client, but I'm getting some errors I can't seem to squash. Specifically, I get IO exceptions, either "Connection Refused" or "No route to host" exceptions.

public class Lab2Client {
/**
 * @param args[1] == server name, args[2] == server port, args[3] == myport
 */
public static void main(String[] args) {
    //Serverport is set to 10085, our client is 10086
    try {
        Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));
        System.out.println("Server connection Completed\n");
        DataOutputStream output = new DataOutputStream(echoSocket.getOutputStream());
        byte[] toSend = new byte[5];
        toSend[0] = 12; toSend[1] = 34;//Code Number
        toSend[2] = 15;//GroupId
        toSend[3] = 86;toSend[4] = 100;//Port number in Little Endian Order
        output.write(toSend);

        System.out.println("Sent Request. Waiting for reply...\n");

        DataInputStream input = new DataInputStream(echoSocket.getInputStream());

        byte[] toRecieve = new byte[]{0,0,0,0,0,0,0,0}; 
        input.read(toRecieve);
        checkMessage(toRecieve);            
    }
    catch (UnknownHostException e) {
        System.err.println("Servername Incorrect!");
        System.exit(1);
    }
    catch (IOException e){
        System.err.println("IO Exception. Exiting...");
        System.err.println(e);
        System.exit(1);
    }
}

I also have some questions about my implementation regarding receiving messages in Java. I'll be getting a datagram that contains either:

a) 3 formatting bytes (unimportant to the question) along with an IP and port number

or

b) 3 formatting bytes and a port.

Is using a DataInputStream the correct way to do this? I know using an array with 9 elements is lazy instead of dynamically allocating one that's either 5 or 9, but right now I'm just trying to get this working. That being said, is there a different approach anyone would suggest for this?

Upvotes: 0

Views: 504

Answers (2)

Ryanman
Ryanman

Reputation: 880

thought I'd leave this up for posterity. The problem is simple, and I'm a fool for not noticing it sooner.

The correct programs I was testing this against used the UDP protocol, and this program is written in TCP. The corrected code is:

public class Lab2Client {
/**
 * @param args[0] == server name, args[1] == server port, args[2] == myport
 */
public static void main(String[] args) {
    //Serverport is 10085, our client is 10086
    try {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName(args[0]);
        int portToSend = Integer.parseInt(args[2]);
        System.out.println("Clent Socket Created");
        byte[] toSend = new byte[5];
        toSend[0] = 0x12; toSend[1] = 0x34;//Code Number
        toSend[2] = 15;//GroupId, f in hex
        toSend[3] = 0x27;toSend[4] = 0x66;
        System.out.println("Byte Array Constructed");

        DatagramPacket sendPacket = new DatagramPacket(toSend, toSend.length, IPAddress, Integer.parseInt(args[1]));
        clientSocket.send(sendPacket);          
        System.out.println("Sent Request. Waiting for reply...\n");

        DataInputStream input = new DataInputStream(echoSocket.getInputStream());
        toRecieve can either be an error message, a return of what we sent,
        or a byte stream full of IP info and port numbers.
        the "heavy" byte stream is either 4 for IPv4 of 16 for IPv6, 2 bytes for port,
        and the magic number (2 bytes) for a total of 9-20 bytes*/

        byte[] toRecieve = new byte[9];
        DatagramPacket receivePacket = new DatagramPacket(toRecieve, toRecieve.length);
        clientSocket.receive(receivePacket);            
        checkMessage(toRecieve);            
    } //and so on and so forth...

Thanks to @Serge for the help, though nobody could have answered my question correctly with how I asked it. The byte shifting you suggested was important too.

Upvotes: 0

Serge
Serge

Reputation: 6095

You need not to wrap the stream returned by Socket.getOuputStream() with DataOutputStream - it is already the DataOutputStream

In this line:

Socket echoSocket = new Socket(args[0],Integer.parseInt(args[2]));

I suppose it should be args[1], not args[0].

Here you have to convert the integer value to its byte representation:

   toSend[3] = 10086 & 0xFF;toSend[4] = 10086>>8; //Port number in Little Endian Order

Answer to your question: case b as you are not sending the IP

Upvotes: 2

Related Questions