Streaming an array over TCP in Java - java

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.
Client code
Server code
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:
You are using static modifiers for your class variables and
methods. They do not serve any purpose being defined this way and
and make debugging miserable. Remove them all.
Create only those class variables that are actually required in order
to maintain state. Currently most of the variables you declare at a
class level could actually be declared within the methods that use
them.
In your client side recieveArray method you are reinitializing your
buffered 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).
In your client side recieveArray method, you are reading up to
count strings, but on your server side you send count - 1
strings, because you are storing the count in the first element of
the array you had previously filled.
This code:
for (int j = 0; j < 10; j++) {
filearray[j] = br.readLine();
counter++;
}
for (int i = 1; i < counter; i++) {
outStream = new PrintWriter(client.getOutputStream(), true);
outStream.println(filearray[i]);
}
In the code I posted above (server side) you are also recreating the
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.

see your code in Transfers class you are intialize farray array with null
.and also you are using this in for loop
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
and in Specials class you again use in for
outStream = new PrintWriter(client.getOutputStream(), true);

Related

No response from server side (basic java client-server)

Hi I struggle with my client/server homeworks in java
I have to connect to a given server and do some "talking", but I have a problem where after I write to the server I get no response from it (timeout in tests before my "never reaches" comment in the code)
public void run(PrintStream sysout, String host, int port, String myname) {
OutputStream out = null;
BufferedReader in = null;
for (int i = 0; i < COUNT; i++) {
try{
this.connect(host,port); // same as clientSocket constructor
out = this.clientSocket.getOutputStream();
in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
out.write(myname.getBytes());
out.flush();
String income = in.readLine(); // never reaches !!!
sysout.print(income);
sysout.flush();
out.close();
}catch(IOException e){
sysout.println(HelloServer.ERR_MESSAGE);
sysout.flush();
continue;
}
}

Datagram Packet FTP application

I write a FTP application using UDP Datagram Protocol , and I need the client side read from file 100 character and send it in 5 parts , 20 characters in each part , when I run my program I get this error :
Exception in thread "main" java.lang.IllegalArgumentException: illegal length or offset. I want the server get each line in five parts but sort it accordingly.
this is my code :
import java.net.*;
import java.io.*;
public class FTPClient
{
public static void main(String[] args)
{
final int SIZE=100;
DatagramSocket skt= null;
DatagramPacket pkt = null;
BufferedReader read= null;
int port = 3131;
try
{
skt=new DatagramSocket(2121);
read= new BufferedReader(new FileReader("input.txt"));
String line = read.readLine();
byte[] lineByte = new byte[SIZE];
lineByte = line.getBytes();
InetAddress add = InetAddress.getByName("localhost");
for(int i=0;i<100;i+=20)
{
pkt = new DatagramPacket(lineByte,i,20,add,port);
skt.send(pkt);
}
}
catch(IOException e)
{
System.out.println(e.getMessage()); }
finally
{
skt.close();
// read.close();
}
}
}
Your code with comments at problem areas. If you still have problems resolving the issues after reading my comments, just add a comment and I will explain more
public static void main(String[] args) {
final int SIZE = 100;
DatagramSocket skt = null;
DatagramPacket pkt = null;
BufferedReader read = null;
int port = 3131;
try {
skt = new DatagramSocket(2121);
read = new BufferedReader(new FileReader("input.txt"));
String line = read.readLine(); // how long is the line?
byte[] lineByte = new byte[SIZE]; // this is a redundant assignment
lineByte = line.getBytes(); // now the length of the lineBytes is "unknown"
InetAddress add = InetAddress.getByName("localhost");
for (int i = 0; i < 100; i += 20) { // you should check the length of lineBytes instead of 100
pkt = new DatagramPacket(lineByte, i, 20, add, port);
skt.send(pkt);
}
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
skt.close();
// read.close();
}
}

Program only printing out/receiving every other packet

I'm supposed to send packets of a file over to a server which then prints it out. The problem i have is that it prints out only every odd number (0-nothing, 1- text, 2- nothing, 4-text etc..). This gets done in the server class. Can anyone see what he problem is?
Client
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
Client myCli = new Client();
myCli.run();
}
public void run() throws Exception {
Socket mySkt = new Socket("localhost", 9999);
PrintStream myPS = new PrintStream(mySkt.getOutputStream());
BufferedReader in = new BufferedReader(new FileReader("C:/Users/Thormode/Dropbox/Skole 2013-2014/java/da/src/da/tekst.txt"));
while (in.ready()) {
String s = in.readLine();
myPS.println(s);
}
in.close();
//BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream()));
//String temp = myBR.readLine();
//System.out.println(temp);
mySkt.close();
myPS.close();
}
}
Server:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws Exception {
Server myServ = new Server();
myServ.run();
}
public void run() throws Exception {
ServerSocket mySS = new ServerSocket(9999);
Socket SS_accept = mySS.accept();
BufferedReader SS_BF = new BufferedReader(new InputStreamReader(
SS_accept.getInputStream()));
int i = 0;
String[] array = new String[10];
while (SS_BF.readLine() != null) {
array[i] = SS_BF.readLine();
i++;
}
for (int j = 0; j < i; j++) {
String temp = array[j];
System.out.println(temp);
}
SS_accept.close();
mySS.close();
}
}
while (SS_BF.readLine() != null) {
array[i] = SS_BF.readLine();
i++;
}
You are calling SS_BF.readLine twice. It discards the first line because of this.
while ((String line = SS_BF.readLine()) != null) {
array[i] = line;
i++;
}
The following code will read two lines a loop:
String[] array = new String[10];
while (SS_BF.readLine() != null) { // reads a line and forgets it's value
array[i] = SS_BF.readLine(); // reads every second line and puts it into the array
i++;
}
So you have to do it like this:
String[] array = new String[10];
String line = SS_BF.readLine(); // read first line and store it to 'line'
while (line != null) {
array[i++] = line; // set 'line' to array
line = SS_BF.readLine(); // read next line
}

How to make sure that the file passed using java.net is as it was before transmission

I'm studying java.net and trying to transmit som file. here is the sender code
public static final FilesGetter filesGetter = new FilesGetter();
public static Socket s;
public static File[] files;
public static void main(String args[]) throws Exception{
s = new Socket("localhost", 3128);
while (true){
try{
files = filesGetter.getFilesList("/etc/dlp/templates/");
Socket s = new Socket("localhost", 3128);
args[0] = args[0]+"\n"+s.getInetAddress().getHostAddress()
+":"+s.getLocalPort();
if (files != null){
for (int i = 0; i < files.length; i++){
InputStream is = new FileInputStream(files[i]);
byte[] message = IOUtils.toByteArray(is);
s.getOutputStream().write(message);
byte buf[] = new byte[64*1024];
int r = s.getInputStream().read(buf);
String data = new String(buf, 0, r);
System.out.println(data);
}
}
} catch(Exception e){
System.out.println("init error: "+e);
}
}
}
And here is the receiver code:
public class Consumer extends Thread{
public static Socket s;
public String customerId;
int num;
public static final Filter filter = new Filter();
public MimeParser mimeParser = new MimeParser(true);
public Consumer(int num, final Socket s, final String customerId){
this.num = num;
this.s = s;
this.customerId = customerId;
setDaemon(true);
setPriority(NORM_PRIORITY);
start();
}
public static void receive(final String customerId){
try {
int i = 0;
ServerSocket server = new ServerSocket(3128, 0, InetAddress.getByName("localhost"));
System.out.println("server started");
while (true){
new Consumer(i, server.accept(), customerId);
i++;
}
} catch (Exception e){
e.printStackTrace();
}
}
public void run(){
try {
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
byte buf[] = new byte[64*1024];
int r = is.read(buf);
if (r < 0)
return;
ByteArrayInputStream bais = new ByteArrayInputStream(buf, 0, r);
MessageInfo mi = mimeParser.parseMessages(bais);
filter.getMessageAcceptability(mi.getPlainText(), customerId);
s.close();
} catch (Exception e){
System.out.println("init error: " + e);
}
}
}
I'm not sure of data integrity because processing of data on the server-side is not fully successful and don't know if I need to look for mistakes in the processing code(which worked well when I've been Using rabbitmq) or in client-server code. I also don't know what buffer size must be chosen.
At the sender side you can send as much as you want, 64*1024 it's OK, the send method will loop until all the data has been delivery. But at the receiver it could be that read returns before the whole file has been read, you must loop reading until the other side closes the socket.
In these cases it's better to send, in advance, an integer indicating how much data you are going to send, the receiver will loops until that much bytes are read.
For example:
int ret=0;
int offset=0;
int BUFF_LEN=64*1024;
byte[] buffer = new byte[BUFF_LEN];
while ((ret = is.read(buffer, offset, BUFF_LEN - offset)) > 0)
{
offset+=ret;
// just in case the file is bigger that the buffer size
if (offset >= BUFF_LEN) break;
}
You need to compute a checksum like SHA-1 on both the sender and the receiver's side. When they match, you can assume that there was no transmission error. Your question is really more of a protocol question.
A much simpler and more practical solution than to implement your own integrity checks would be to use the SSL protocol. That will provide you with integrity checked transmissions.

Reading a large number of BitSet Objects from a file in Java

I want read a large number of BitSet objects from a file (12MB). I used following code but only read first object from file and repeated it. thanks
public static void main(String[] args) {
// TODO code application logic here
ObjectInputStream Input = null;
FileInputStream Database = null;
Object Buffer = null;
BitSet H = null;
try
{
Database = new FileInputStream("BloomFilters.txt");
Input = new ObjectInputStream(Database);
while((Buffer = Input.readObject()) != null)
{
H = (BitSet)Buffer;
System.out.println(H);
System.out.println("Yes" );
}
}
catch(Exception e)
{
System.out.println("Exp = " + e.getMessage());
}
and following code create a file of BitSet objects, I want read objects from this file
public class Main {
public static void main(String[] args) {
BloomFilter Set = new BloomFilter(512, 100);
ObjectOutputStream Output = null;
DataInputStream Input = null;
FileOutputStream DBOut = null;
FileInputStream DBIn = null;
String Sequence = "";
try
{
DBOut = new FileOutputStream("Bloomfilters.txt");
Output = new ObjectOutputStream(DBOut);
DBIn = new FileInputStream("DB.txt");
Input = new DataInputStream(DBIn);
while((Sequence = (String) Input.readLine()) != null)
{
Set.clear();
for(int i = 0; i < Sequence.length() - 1; i++)
Set.add((Sequence.substring(i, i + 2)));
BitSet buffer = Set.getBitSet();
Output.writeObject(buffer);
}
Input.close();
Output.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I think you need a Scanner see this code Java Bitset error with large index. It's a different question but the first loop is to read a large file with numbers into a bitset,

Categories

Resources