I am working on this for hours..Your help is highly appreciated.

The client will send 32 integers to the server. These 32 integers will be read from a file called DataInput.txt that is the same directory as the source of the client. I am trying to implement this delivery using Java UDP datagrams.(Because I have to,this is mandatory for this project.) The server will receive packets from the client and track which packets were received from the client. The server will track the order of segment delivered and reconstruct the correct order of the 32 integers and output it to the terminal.

Here's my client code;

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

public class Client {

	private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)
	private final String myServerAddr = "127.0.0.1";

	public static void main(String[] args) {

		try {
			new Client().go();
		} catch (Exception e) {
			System.err.println(e);
		}
	}// end of main

	private void go() throws IOException, SocketException, UnknownHostException {

		DatagramSocket dgSocket = new DatagramSocket();
		InetAddress inAddr = InetAddress.getByName(myServerAddr);

		Scanner s = new Scanner(new File("DataInput.txt"));
		int[] array = new int[32];

		for (int i = 0; i < array.length; i++) {

			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			PrintStream pout = new PrintStream(bout);
			pout.print(array[i] = s.nextInt());

			byte[] barray = bout.toByteArray();

			DatagramPacket dgPacket = new DatagramPacket(barray, barray.length,
					inAddr, myServerPort);

			dgSocket.send(dgPacket);

		}// end for

		dgSocket.close();

	}// end of go()

}// end of class

Here's my server code;

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

public class Server {

	private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)

	public static void main(String[] args) {
		try {
			new Server().go();
		} catch (Exception e) {
			System.err.println(e);
		}
	}// end of main

	private void go() throws IOException, SocketException, UnknownHostException {

		byte[] barray = new byte[4];
		DatagramSocket dgSocket = new DatagramSocket(myServerPort);
		DatagramPacket dgPacket = new DatagramPacket(barray, barray.length);

		dgSocket.receive(dgPacket);

		ByteArrayInputStream bin = new ByteArrayInputStream(dgPacket.getData());

		for (int i = 0; i < dgPacket.getLength(); i++) {

			int data = bin.read();

			if (data == -1)
				break;
			else
				System.out.println(new String(dgPacket.getData()));

		}// end for

		dgSocket.close();

	}// end of go()

}// end class

When I hard code the integer without reading it from the file, I am able to transfer it.
After I added the part where I read the number from the file and etc..I got the following when I run Client.java

java.util.InputMismatchException

I don't know where I'm doing wrong..

Recommended Answers

All 2 Replies

The exception also tells you the line number where that happened -- essential info!
But... normally you'll get that from s.nextInt() when the next thing in the scanner isn't recognised as an integer.

I am working on this for hours..Your help is highly appreciated.

The client will send 32 integers to the server. These 32 integers will be read from a file called DataInput.txt that is the same directory as the source of the client. I am trying to implement this delivery using Java UDP datagrams.(Because I have to,this is mandatory for this project.) The server will receive packets from the client and track which packets were received from the client. The server will track the order of segment delivered and reconstruct the correct order of the 32 integers and output it to the terminal.

Here's my client code;

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

public class Client {

	private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)
	private final String myServerAddr = "127.0.0.1";

	public static void main(String[] args) {

		try {
			new Client().go();
		} catch (Exception e) {
			System.err.println(e);
		}
	}// end of main

	private void go() throws IOException, SocketException, UnknownHostException {

		DatagramSocket dgSocket = new DatagramSocket();
		InetAddress inAddr = InetAddress.getByName(myServerAddr);

		Scanner s = new Scanner(new File("DataInput.txt"));
		int[] array = new int[32];

		for (int i = 0; i < array.length; i++) {

			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			PrintStream pout = new PrintStream(bout);
			pout.print(array[i] = s.nextInt());

			byte[] barray = bout.toByteArray();

			DatagramPacket dgPacket = new DatagramPacket(barray, barray.length,
					inAddr, myServerPort);

			dgSocket.send(dgPacket);

		}// end for

		dgSocket.close();

	}// end of go()

}// end of class

Here's my server code;

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

public class Server {

	private final int myServerPort = 0x28b5; // 0x28b5 = 10421 (decimal)

	public static void main(String[] args) {
		try {
			new Server().go();
		} catch (Exception e) {
			System.err.println(e);
		}
	}// end of main

	private void go() throws IOException, SocketException, UnknownHostException {

		byte[] barray = new byte[4];
		DatagramSocket dgSocket = new DatagramSocket(myServerPort);
		DatagramPacket dgPacket = new DatagramPacket(barray, barray.length);

		dgSocket.receive(dgPacket);

		ByteArrayInputStream bin = new ByteArrayInputStream(dgPacket.getData());

		for (int i = 0; i < dgPacket.getLength(); i++) {

			int data = bin.read();

			if (data == -1)
				break;
			else
				System.out.println(new String(dgPacket.getData()));

		}// end for

		dgSocket.close();

	}// end of go()

}// end class

When I hard code the integer without reading it from the file, I am able to transfer it.
After I added the part where I read the number from the file and etc..I got the following when I run Client.java

java.util.InputMismatchException

I don't know where I'm doing wrong..

lookin at you client code in which you accept the integer i would definitely say that its coded to give you hell, the reason is you are asking for an intger inside the for statement, adding it to an array and all of this at once, i would think it would need some revision:

for (int i = 0; i < array.length; i++) {
                        ...
			PrintStream pout = new PrintStream(bout);
			pout.print(array[i] = s.nextInt());
                         ....
}

why not rather get ll the intgers ad it to the array and then enter the for statement and send each integer? but again as JamesCherrill said look at the line number the exception is being thrown at as the above could be working ok? but i highly doubt it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.