Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:53:05+00:00 2026-06-02T10:53:05+00:00

I am trying to stream an array of strings accross a network. I can

  • 0

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…

  1. Open text file
  2. Read in line by line (each line into a separate string inside of an
    array)
  3. Once file read is complete, start streaming each individual string
  4. Have a for loop on the recieve end placing the strings back into
    another array
  5. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:53:10+00:00Added an answer on June 2, 2026 at 10:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to round-trip a JSON string to a byte array with DeflaterOutputStream, but
I am trying to create a md5 string from the byte array of an
I am trying to read a message of known length from the network stream.
I'm trying to stream binary data to the standard output in .NET. However you
I'm trying to stream video to a server from another machine using Java and
I'm trying to stream sound from my soundcard using sox's default audio device and
I am currently trying to stream content out to the web after a trans-coding
I'm generating a MJpeg Stream and trying to stream it to VLC and play
I 've a stream of Jpegs images and I'm trying to stream them with
I am trying to upload a file or stream of data to our web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.