Java Multi-threaded Server with Array Programming - java

I am a new in distributed computing in Java. I came across a problem when I try to create a server with multi-threads that connects with clients.
The clients will calculate a Pi value using the random generator and then send its result to the server and the server will collect the incoming value and calculate and display the average.
I expect the clients to calculate pi. and send it to server. The server would collect each pi result and calculate & display it on the screen. For example, client 1 calculated value is 3.141, client 2 calculated value is 3.143, and then the server would connect each thread and display 3.142.
It does not come through the result. I don't know what's my problem. It seems that I got a problem in constructing my thread. Can anyone help me on this? Thank you.
The following is my code:
Server Code:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws IOException {
final int size = 5; //4, 5, 6, etc.
double sum = 0;
final int PORT = 1234;
ServerSocket serverSocket = null;
MyThread[] a = new MyThread[size];
Thread[] t = new Thread[size];
try {
serverSocket = new ServerSocket(PORT);
} catch (Exception e) {
System.out.println("Error: " + e);
System.exit(0);
}
for (int i = 0; i < size; i++) {
a[i] = new MyThread(serverSocket.accept());
System.out.println("server is ready...");
t[i] = new Thread(a);
t[i].start();
}
for (int i = 0; i < size; i++) {
try {
t[i].join();
} catch (Exception e) {
System.out.println("Error: " + e);
System.exit(0);
}
}
for (int i = 0; i < size; i++) {
sum += a[i].getValue();
}
System.out.println("The average is: " + sum / size);
}
}
Thread Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class MyThread implements Runnable {
private double value = 0;
public double getValue() {
return value;
}
Socket socket;
MyThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
String message = null;
BufferedReader buff = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((message = buff.readLine()) != null) {
System.out.println("Incoming client message: " + message);
value = Double.parseDouble(message);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Random;
public class MyClient {
public static void main(String[] args) throws IOException, IOException {
Socket socket = new Socket("localhost", 1234);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
long toss, number_of_tosses, number_in_circle = 0;
double x, y, distance_squared, pi_estimate;
Random generator = new Random();
number_of_tosses = generator.nextInt(5000) + 5000;
for (toss = 0; toss < number_of_tosses; toss++) {
x = 2 * Math.random() - 1;
y = 2 * Math.random() - 1;
distance_squared = x * x + y * y;
if (distance_squared <= 1) number_in_circle++;
}
pi_estimate = 4 * number_in_circle / ((double) number_of_tosses);
printWriter.println(pi_estimate);
}
}

When you are using PrintWriter it is advisable to call the flush() method afterwards.
Like this:`
try{
Socket socket = new Socket("127.0.0.1", 5000);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("message");
writer.flush(); // In your case: printWriter.flush();
catch(IOException exception) {
exception.printStackTrace();
}

Related

Using ObjectStreams to send Lists over sockets

I need to transfer a List between nodes and replace the existing one with the new one in the new node to achieve this, I'm using Sockets from Java.
I somehow have managed to transfer the data but only when I terminate the process. I need it to continue running, the process but at the same time transfer, the data in case any other new node joins the List.
How can I achieve this? I will have to introduce Threads in the Download along the road.
I got it working with files but now I need to change it to Sync lists, just having this is enough?
private static List<CloudByte> cloudByteList = Collections.synchronizedList(new ArrayList<>());
This is my current code:
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static FileData.getCloudByteList;
import static FileData.getFile;
public class FileData {
private static File file;
private static String fileName;
private static List<CloudByte> cloudByteList = Collections.synchronizedList(new ArrayList<>());
public FileData(String fileName) throws IOException {
if (fileName == null) {
this.fileName = "data2.bin";
this.file = new File(this.fileName);
Download.downloadFile();
} else {
this.file = new File(fileName);
this.fileName = fileName;
fillingList();
}
}
public void fillingList() throws IOException {
byte[] fileContents = Files.readAllBytes(file.toPath());
for (int i = 0; i < fileContents.length - 1; i++) {
cloudByteList.add(new CloudByte(fileContents[i]));
}
}
public static List<CloudByte> getCloudByteList() {
return cloudByteList;
}
public static File getFile() {
return file;
}
public String getFileName() {
return fileName;
}
public static void setFile(File file) {
FileData.file = file;
}
/*--------------------------Download--------------------------*/
}
class Download extends Thread {
static ConnectingDirectory connectingDirectory;
#Override
public void run() {
try {
downloadFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void downloadFile() throws IOException {
var nodes = ConnectingDirectory.getNodes();
Socket socket = null;
if (getFile().exists()) {
System.out.println("File: " + getFile() + " exists.");
new Upload().uploadFile();
}
FileOutputStream fos = new FileOutputStream(FileData.getFile());
//ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < nodes.size() - 1; i++) {
if (!(nodes.get(i).getHostPort() == ConnectingDirectory.getHostIP())) {
System.out.println("test33123");
ServerSocket serverSocket = new ServerSocket(nodes.get(i).getHostPort());
System.out.println(serverSocket);
socket = serverSocket.accept();
System.out.println("now socket");
System.out.println(socket);
//socket = new Socket(nodes.get(i).getName(), nodes.get(i).getHostPort());
//System.out.println(socket);
int bytes = 0;
DataInputStream ois = new DataInputStream(socket.getInputStream());
long size = ois.readLong();
System.out.println(size);
byte[] buffer = new byte[100 * 10000];
while (size > 0 && (bytes = ois.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
System.out.println("test3333");
fos.write(buffer, 0, bytes);
size -= bytes;
}
}
}
}
}
/*--------------------------Upload--------------------------*/
class Upload {
public void uploadFile() throws IOException {
int bytes = 0;
var nodes = ConnectingDirectory.getNodes();
FileInputStream fileInputStream = new FileInputStream("data.bin");
DataInputStream ois = new DataInputStream(fileInputStream);
if (!getFile().exists()) {
System.out.println("File doesn't exist." + "\nDownloading the file!");
new Download().downloadFile();
}
System.out.println("hello");
for (int i = 0; i < nodes.size() - 1; i++) {
System.out.println("hello2");
Socket socket = new Socket(nodes.get(i).getName(), nodes.get(i).getHostPort());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeLong(new File("data.bin").length());
byte[] buffer = new byte[100 * 10000];
while ((bytes = ois.read(buffer)) != -1) {
dos.write(buffer, 0, bytes);
dos.flush();
}
}
}
}
As you can see, I'm using DataInput because if I try to use the ObjectInputStream, I get a Corrupted Header Exception. I have more classes to add to this. My goal is as I said, to transfer the data inside the "data.bin" to a "data2.bin" file. I'm able to create it and delete it but at the same time, no Data is being written/sent to it.
How can I fix the CorruptedHeaderException and get it to send the content?
All help is appreciated.
StorageNode Class:
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import static FileData.*;
public class StorageNode extends Thread {
private static int serverPort = 8080;
private static int clientPort = 8082;
private static String fileName = null;
private static String addressName = "localhost";
private static ConnectingDirectory connectingDirectory;
private static FileData fileData;
static ErrorInjection errorInjection;
public static void main(String[] args) throws IOException, InterruptedException {
/* if (args.length > 3) {
addressName = args[0];
serverPort = Integer.parseInt(args[1]);
clientPort = Integer.parseInt(args[2]);
fileData = new FileData(args[3]);
} else {
fileName = null;
fileData = new FileData(fileName);
}*/
connectingDirectory = new ConnectingDirectory(addressName, clientPort, serverPort);
fileData = new FileData(fileName);
errorInjection = new ErrorInjection();
errorInjection.start();
if(fileData.getFile().exists()){
new Upload().uploadFile();
}else {
new Download().downloadFile();
}
}
ConnectingDirectory Class
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ConnectingDirectory {
private String hostName;
private static int hostIP;
private int directoryIP;
private InetAddress address;
private InputStream in;
private OutputStream out;
private static List<Nodes> nodes = new ArrayList<>();
List<String> nodess = new ArrayList<>();
private Socket socket;
private String sign = "INSC ";
public ConnectingDirectory(String hostName, int hostIP, int directoryIP) throws IOException {
this.hostName = hostName;
this.hostIP = hostIP;
this.directoryIP = directoryIP;
this.address = InetAddress.getByName(hostName);
this.socket = new Socket(address, directoryIP);
signUp();
askConnectedNodes();
}
public void signUp() throws IOException {
System.out.println("You are connecting to the following address: " + hostIP + "\n");
System.out.println("The port you are connected to: " + socket.getPort() + "\n");
in = socket.getInputStream();
out = socket.getOutputStream();
out.write(generateSignUp(address, hostIP).getBytes());
out.flush();
}
public String generateSignUp(InetAddress address, int hostIP) {
String signUpString = sign + address + " " + hostIP + "\n";
return signUpString;
}
public void askConnectedNodes() throws IOException {
String directoryNodesAvailable;
String a = "nodes\n";
out.write(a.getBytes());
out.flush();
Scanner scan = new Scanner(in);
while (true) {
directoryNodesAvailable = scan.nextLine();
addExistingNodes(directoryNodesAvailable);
//System.out.println("Eco: " + directoryNodesAvailable);
if (directoryNodesAvailable.equals("end")) {
out.flush();
printNodes();
break;
}
}
}
public void addExistingNodes(String sta) throws IOException {
if (sta.equals("end")) return;
if (!(nodess.contains(sta))) {
nodess.add(sta);
nodes.add(new Nodes(nodess.get(nodess.size() - 1)));
}
return;
}
public static List<Nodes> getNodes() {
return nodes;
}
public void printNodes() {
System.out.println("Checking for available nodes: \n");
nodes.forEach((z) -> System.out.println(z.getNode()));
}
public Socket getSocket() {
return socket;
}
public static int getHostIP() {
return hostIP;
}
public InetAddress getAddress() {
return address;
}
}
For all of those that need help in the future:
Sender side:
Socket socket = new Socket("localhost", hostPort);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ByteBlockRequest bbr = new ByteBlockRequest(getStoredData());
objectOutputStream.writeObject(bbr.blocksToSend(j));
Receiver side:
Socket = StorageNode.getServerSocket().accept();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
byte[] bit = (byte[]) ois.readObject();
In my case, I needed to use byte[], so I had to do a few additional functions in the back, to change Cloudbyte[] into byte[]. Once I did that, I was able to send the data using, ObjectInput/ObjectOutput.

Java (Raspberry pi) Thread

I am a student who is studying java.(Especially Raspberry pi) I have a question this multuthread. It can be compiled. But it doesn't work in my kit. If you don't mind guys, could you check my code and help me?
Thanks...
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
import java.io.FileWriter;
public class RcvThread2 implements Runnable{
private static final int sizeBuf = 50;
private Socket clientSocket;
private Logger logger;
private SocketAddress clientAddress;
public RcvThread2(Socket clntSock, SocketAddress clientAddress, Logger logger) {
this.clientSocket = clntSock;
this.logger = logger;
this.clientAddress = clientAddress;
}
static class CloseExtends extends Thread {
static final String GPIO_OUT = "out";
static final String GPIO_ON = "1";
static final String GPIO_OFF = "0";
static final String[] GpioChannels = {"18"};
public static void main(String[] args) {
FileWriter[] commandChannels;
try {
FileWriter unexportFile = new FileWriter("sys/class/gpio/unexport");
FileWriter exportFile = new FileWriter("sys/class/gpio/gpio/export");
for(String gpioChannel : GpioChannels) {
System.out.println(gpioChannel);
File exportFileCheck =
new File("sys/class/gpio/gpio" +gpioChannel);
if(exportFileCheck.exists()) {
unexportFile.write(gpioChannel);
exportFile.flush();
}
exportFile.write(gpioChannel);
exportFile.flush();
FileWriter directionFile = new FileWriter("/sys/class/gpio/gpio" + gpioChannel + "/direction");
directionFile.write(GPIO_OUT);
directionFile.flush();
}
FileWriter commandChannel = new FileWriter("sys/class/gpio/gpio" + GpioChannels[0] + "/value");
int period = 20;
int repeatLoop = 25;
int counter;
while(true) {
for(counter = 0; counter < repeatLoop; counter++) {
commandChannel.write(GPIO_ON);
commandChannel.flush();
java.lang.Thread.sleep(2, 20000);
commandChannel.write(GPIO_OFF);
commandChannel.flush();
java.lang.Thread.sleep(period);
}
break;
}
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
public void main(){
try {
InputStream ins = clientSocket.getInputStream();
OutputStream outs = clientSocket.getOutputStream();
int rcvBufSize;
byte[] rcvBuf = new byte[sizeBuf];
while ((rcvBufSize = ins.read(rcvBuf)) != -1) {
String rcvData = new String(rcvBuf, 0, rcvBufSize, "UTF-8");
if(rcvData.compareTo("MotorLock") == 0) {
CloseExtends te = new CloseExtends();
te.start();
}
if(rcvData.compareTo("MotorOpen") == 0) {
}
logger.info("Received data :" + rcvData + " (" + clientAddress + ")");
outs.write(rcvBuf, 0, rcvBufSize);
}
logger.info(clientSocket.getRemoteSocketAddress() + "Closed");
} catch (IOException ex) {
logger.log(Level.WARNING, "Exception in RcvThread", ex);
}finally {
try{
clientSocket.close();
System.out.println("Disconnected! Client IP :" + clientAddress);
} catch (IOException e) {}
}
}
}
The lower main method never gets called.
If you run your program it will execute the public static void main(String[] args) { method.
I think this is the method you want to run in the second thread?!
If you declare and run your new thread using
CloseExtends te = new CloseExtends();
te.start();
it will run the threads public void run() { method.
So if I understand your intention correctly you should change the name of the main method in the CloseExtends class to the threads run method and change the signature of the lower main method to the java programs main method public static void main(String[] args) {.
I would not name any other method "main" if it is not really a main method.
You can see an example of creating a new thread with the Runnable interface here: https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Persistent connection not working, opens new connection on every request

I am trying to implement a persistent http server in java, being new in socket programming, I am unaware of the complications.
But run function of ThreadHandler class, it stucks forever and keep waiting, but at the same time if new request comes a new connection is being made instead of using the previous connection itself (while the initial connection keep waiting for request).
Please suggest the correction
here is my server code that calls ThreadHandler for threading.
Server code
package httpServer;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Server {
static int countofconnections = 0;
public static void main(String[] args) throws IOException {
int port = 14998;
ServerSocket socketServerSocket = new ServerSocket(port);
while(true) {
Socket socketClientSocket = socketServerSocket.accept();
countofconnections++;
//Runnable handler = new TinyHttpdSocketHandler(socket);
ThreadHandler threadHandler = new ThreadHandler(socketClientSocket);
Thread socketThread = new Thread(threadHandler, "Thread for " + socketClientSocket.toString());
System.out.println(" connection count is now : " + countofconnections+ " ");
socketThread.setDaemon(true);
socketThread.start();
}
}
}
ThreadHandler code:
package httpServer;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketTimeoutException;
//import java.util.ArrayList;
public class ThreadHandler implements Runnable{
Socket socketClientSocket;
//ServerSocket socketServerSocket;
ThreadHandler(Socket socketClientSocket){
this.socketClientSocket = socketClientSocket;
//this.socketServerSocket = socketServerSocket;
}
#Override
public void run(){
boolean alive = false;
try {
int count = 0;
socketClientSocket.setSoTimeout(40000);
BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(socketClientSocket.getInputStream()));
DataOutputStream outputToClient = new DataOutputStream(socketClientSocket.getOutputStream());
String messegeFromClient = inputFromClient.readLine();
System.out.println(messegeFromClient);
while(messegeFromClient!=null) {
String requestString = messegeFromClient;
System.out.println("entering while loop");
//String in = inputFromClient.readLine();
while(messegeFromClient!=null && !messegeFromClient.equals("")) {
//System.out.printlnln(in);
messegeFromClient = inputFromClient.readLine();
//System.out.println(messegeFromClient);
if(messegeFromClient.startsWith("Connection:")) {
String[] connectionTimeString = messegeFromClient.split(" ");
if(connectionTimeString[1].equals("keep-alive")) {
alive = true;
//System.out.println("keep alive found");
}
}
}
count++;
System.out.println(count);
//System.out.printlnln( messegeFromClient);
String[] tokensStrings = requestString.split(" ");
if(tokensStrings[0].equals("GET")) {
String requestedFilename = tokensStrings[1];
if(requestedFilename.startsWith("/")) {
requestedFilename = requestedFilename.substring(1);
if(requestedFilename.startsWith("~")) {
requestedFilename = "/home/suman/users/"+requestedFilename.substring(1);
}
else {
requestedFilename = "/home/suman/public_html/"+requestedFilename;
}
System.out.println(requestedFilename);
}
File filetosend = new File(requestedFilename);
int filesize = (int) filetosend.length();
FileInputStream fileStream = null;
boolean fileFound = true;
try {
fileStream = new FileInputStream(requestedFilename);
} catch (Exception e) {
fileFound = false;
}
if(fileFound) {
byte[] fileinByte =new byte[filesize];
fileStream.read(fileinByte);
outputToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");
outputToClient.writeBytes("Content-Length: "+filesize+"\r\n\r\n");
outputToClient.write(fileinByte, 0, filesize);
}
else {
outputToClient.writeBytes("HTTP/1.0 404 Document NOT FOUND\r\n\r\n");
outputToClient.writeBytes("404 document not found");
}
outputToClient.flush();
if(alive) {
socketClientSocket.setKeepAlive(true);
socketClientSocket.setSoTimeout(100000);
}
else {
socketClientSocket.close();
System.out.println("closing socket");
break;
}
//socketClientSocket.setSoTimeout(5000);
}
System.out.println("waiting");
messegeFromClient=inputFromClient.readLine();
while( messegeFromClient == null ) {
//this code should be skipped when a new request is made since message from from client will no longer be null in that case
messegeFromClient=inputFromClient.readLine();
}
if(messegeFromClient==null) {
System.out.println("messege is null... I don't think it would be ever executed.");
}
}
socketClientSocket.close();
System.out.println("closing socket 2");
} catch ( SocketTimeoutException e) {
try {
socketClientSocket.close();
System.out.println("socket closed");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
}

Chat server spitting out memory addresses. Not sure why

I'm currently working on a threaded server that allows multiple clients to connect and send messages to each other via "/username message" format. The server runs and my client class is able to connect successfully and be asked for a username, but once I attempt to send a message instead of receiving the output "To [username]: message" I receive "To: [memory address]: [memory address]" which I am confused about as I have not tried to print any objects.
Additionally, nothing is displayed on the client that is supposed to be on the other end of the message. The way I am handling the client threads is with an ArrayList that they are added to after they are created and started. The way I am attempting to send the messages to the clients on the other end is with a method in the server class that iterates through the ArrayList of client threads and outputs the message to the one with the corresponding name.
In advance: I am aware that my parseUserName and parseMessage methods are less than nice, but I've opted to let them sit until I can deal with the bigger problems. For reference, here is my server class.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* #author
*/
public class ThreadedChatServer
{
//private ServerSocket server = new ServerSocket(5679);
private ServerSocket server;
ArrayList<ClientThread> clientThreads;
public ThreadedChatServer(ServerSocket s)
{
server = s;
clientThreads = new ArrayList<ClientThread>();
}
public void openServer() throws IOException
{
while(true)
{
Socket client = server.accept();
System.out.println("The server is connected to " + client.getInetAddress());
// starts a thread for this client
ClientThread c = new ClientThread(client, this);
c.start();
clientThreads.add(c);
}
}
// Iterates through the clientThreads ArrayList and prints the given message
// to the client whose name matches the "to" parameter.
public void sendMessage(String from, String to, String m)
{
for (int i = 0; i < clientThreads.size(); i++)
{
if (clientThreads.get(i).getUserName() != from &&
clientThreads.get(i).getUserName() == to)
{
clientThreads.get(i).toClient.println(m);
}
}
}
public static void main(String[] a) throws IOException
{
new ThreadedChatServer(new ServerSocket(5679)).openServer();
}
public class ClientThread extends Thread
{
private Socket s;
private String name;
private BufferedReader fromClient;
private PrintWriter toClient;
private ThreadedChatServer server;
public ClientThread(Socket c, ThreadedChatServer tc) throws IOException
{
s = c;
name = null;
fromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
toClient = new PrintWriter(s.getOutputStream(), true);
server = tc;
}
public void run()
{
String s = null;
int size = 0;
char[] c = null;
try
{
toClient.println("Enter a username: ");
s = fromClient.readLine();
name = s;
// Accept/send messages from the user
while ((s = fromClient.readLine()) != null)
{
size = s.length();
c = new char[size];
for (int i = 0; i < size; i++)
{
c[i] = s.charAt(size - i - 1);
}
String output = c.toString();
String s2 = "To " + parseUserName(output) + ": "
+ parseMessage(output);
toClient.println(s2);
server.sendMessage(this.name, parseUserName(output),
parseMessage(output));
}
// Close the connection
fromClient.close();
toClient.close();
this.s.close();
clientThreads.remove(c);
} catch (IOException e) {e.printStackTrace();}
}
public String getUserName() {return name;}
public String parseUserName(String s)
{
Scanner in = new Scanner(s);
String temp = in.next();
if (temp.charAt(0) == '/')
{
temp = temp.substring(1, temp.length());
return temp;
}
return temp;
}
public String parseMessage(String s)
{
Scanner in = new Scanner(s);
String temp = in.next();
Boolean firstSpaceCheck = false;
for (int i = 0; i < temp.length(); i++)
{
if (temp.charAt(i) == ' ')
{
temp = temp.substring(i + 1, temp.length());
firstSpaceCheck = true;
}
}
return temp;
}
}
}
It's not the memory address, its the output from Object#toString which takes the format
getClass().getName() + '#' + Integer.toHexString(hashCode())
In this case it looks like [C#15b7986 which tells you you're sending the output of the character array Object back to the client so the output is that returned by Object#toString. Replace
String output = c.toString();
with
String output = new String(c);

ArrayIndexOutOfBoundsException in DatagramPacket-based application

At the beginning, I need to apologise for polish names of variables/functions that I've used in my program.
So, there's little dictionary:
Klient - Client
Serwer - Server
wejscie - input
wyjscie - output
klienci - clients
teraz - now
teraz - text
nawiazPolaczenie - establishConnection
czyscBufor - clearBuffer
odbierzDane - receiveData
pakiet - packet
wyslijDane - sendData
The problem is in moment, when the client is receiving data from server - there's ArrayIndexOutOfBoundsException.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at kontrolerklient.Klient.czyscBuforWejscia(Klient.java:43)
at kontrolerklient.Klient.odbierzDane(Klient.java:48)
at kontrolerklient.Klient.nawiazPolaczenie(Klient.java:33)
at kontrolerklient.Klient.<init>(Klient.java:25)
at kontrolerklient.KontrolerKlient.main (KontrolerKlient.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
Whole server's code:
package kontrolerserwer;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
public class Serwer
{
private DatagramSocket dgSocket;
private byte[] bufferIn;
private byte[] bufferOut;
private InetAddress[] klienci;
public Serwer() throws IOException
{
dgSocket = new DatagramSocket(9998, InetAddress.getByName("192.168.1.100"));
bufferIn = new byte[1024];
bufferOut = new byte[1024];
klienci = new InetAddress[256];
dgSocket.setSoTimeout(1000);
wyslijDane("ready?", InetAddress.getByName("192.168.1.100"));
Date teraz = new Date();
teraz.setTime(teraz.getTime()+10000);
while (teraz.after(new Date()))
{
}
}
public void wyslijDane(String tekst, InetAddress ip) throws IOException
{
bufferOut = tekst.getBytes("ASCII");
dgSocket.send(new DatagramPacket(bufferOut, bufferOut.length, ip, 9999));
}
}
..and client's code:
package kontrolerklient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
class Klient
{
private DatagramSocket dgSocket;
private InetAddress host;
private byte[] wejscie;
private byte[] wyjscie;
public Klient() throws UnknownHostException, SocketException, IOException
{
wejscie = new byte[1024];
wyjscie = new byte[1024];
host = null;
dgSocket = new DatagramSocket(9999, InetAddress.getByName("192.168.1.100"));
nawiazPolaczenie();
}
private void nawiazPolaczenie() throws IOException
{
while (true)
{
if (odbierzDane().equals("ready?"))
{
wyslijDane("ready!", 9998);
}
}
}
private void czyscBuforWejscia()
{
for (int i = 0; i < 1024; i++)
wejscie[i] = 0;
}
public String odbierzDane() throws IOException
{
czyscBuforWejscia();
DatagramPacket pakiet = new DatagramPacket(wejscie, wejscie.length);
System.out.println(pakiet.getLength());
try
{
dgSocket.receive(pakiet);
host = pakiet.getAddress();
// getting packet's data
String s = new String(pakiet.getData(), 0, wejscie.length);
// getting packet's data length
int i;
for (i = 0; (i < 1024) && (wejscie[i] != 0); i++);
// returning packet's data
return s.substring(0, i);
}
catch (Exception e) { }
return "";
}
public void wyslijDane(String dane, int port) throws IOException
{
wejscie = dane.getBytes("ASCII");
dgSocket.send(new DatagramPacket(wyjscie, wyjscie.length, host, port));
}
}
I guess that here:
wejscie = dane.getBytes("ASCII")
you are overwriting the original declaration:
wejscie = new byte[1024];
with some byte array of unknown size. But then you clear the buffer:
for (int i = 0; i < 1024; i++)
with fixed size. It should have been:
for (int i = 0; i < wejscie.length; i++)
when reading the packet, you need to use the length of the data received (DatagramPacket.getLength()), not the length of the byte array you created.
wyslijDane sets wejscie to "ready!".getBytes(), but czyscBuforWejscia expects its size to be 1024

Categories