I am trying to stream an array of strings accross a network. I can send a single string but once I try sending an array of strings I run into problems. I did some research and came up fix which works without errors in the IDE, but then the program crashes when I run it externally. I have narrowed it down to an infinite loop that I have accidently created. I will pastebin all the code as there is too much to put up on here.
Here is the summary of what I am trying to achieve…
- Open text file
- Read in line by line (each line into a separate string inside of an
array) - Once file read is complete, start streaming each individual string
- Have a for loop on the recieve end placing the strings back into
another array - And finally, on the server side, break up each part of the string
into 5 different strings
Here is my client class:
public class Transfers {
public int port = 1223;
//public String Ip = LoginForm.IP;
//public String ip = null;
public static Socket login = null;
public static Socket sock = null;
public static PrintWriter outStream = null;
private static BufferedReader inStream = null;
private static boolean ON = false;
public static String authorize = null;
public static boolean connected = true;
public static void transfers(String IP, int port, String content) throws UnknownHostException, IOException {
try {
login = new Socket(IP, port);
//System.out.println("Connected for streaming");
outStream = new PrintWriter(login.getOutputStream(), true);
outStream.println(content);
} catch (UnknownHostException ex) {
Logger.getLogger(Transfers.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Transfers.class.getName()).log(Level.SEVERE, null, ex);
login.close();
}
}
public static String[] recieveArray(String IP) throws UnknownHostException, IOException {
String[] farray = null;
sock = new Socket(IP, 1224);
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String count = inStream.readLine();
int counter = Integer.parseInt(count);
System.out.println("counter");
for (int i = 0; i < counter; i++) {
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
farray[i] = inStream.readLine();
System.out.println(farray[i]);
}
return farray;
}
}
Here is my server class:
public class Specials {
private static ServerSocket server = null;
private static Socket client = null;
private static PrintWriter outStream = null;
private static BufferedReader inStream = null;
private static boolean ServerOn = true;
public static String message = "";
public static String command = null;
static public InetAddress IP = null;
public static String status = null;
private static String file = "accounts.dat";
private static int counter;
public static void arraysend(String filename) throws FileNotFoundException, IOException {
counter = 0;
FileInputStream fstream = new FileInputStream("AppData/" + filename);
String strLine;
String[] filearray;
try (DataInputStream in = new DataInputStream(fstream)) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
filearray = new String[1000];
for (int j = 0; j < 10; j++) {
filearray[j] = br.readLine();
counter++;
}
in.close();
}
try {
server = new ServerSocket(1224);
client = server.accept();
IP = client.getInetAddress();
outStream = new PrintWriter(client.getOutputStream(), true);
filearray[0] = Integer.toString(counter);
outStream.println(filearray[0]);
for (int i = 1; i < counter; i++) {
outStream = new PrintWriter(client.getOutputStream(), true);
outStream.println(filearray[i]);
}
client.close();
server.close();
} catch (IOException ex1) {
Logger.getLogger(Specials.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
I do not get any error messages, The application just crashes. I am including pastebins of my code below for convenience.
Any help would be appreciated, thanks.
c
There’s so many issues with this code I am not sure where to start. Here is a (non-exhaustive) list:
methods. They do not serve any purpose being defined this way and
and make debugging miserable. Remove them all.
to maintain state. Currently most of the variables you declare at a
class level could actually be declared within the methods that use
them.
recieveArraymethod you are reinitializing yourbuffered reader during every iteration of the receive loop. This is
most likely leading to missing data since you will be trashing some
data that had been buffered in the previous reader (and no longer
available from the socket input stream).
recieveArraymethod, you are reading up tocountstrings, but on your server side you sendcount - 1strings, because you are storing the count in the first element of
the array you had previously filled.
This code:
output stream to the client in every iteration of the send loop.
Fix all those problems and retest, then come ask followup questions if need be.