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..