Results 1 to 5 of 5
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default New to networking in java...

    Ok so my first lovely project for networking in java is a distrubuted primenumber generator... shouldn't be too hard, and I got most of it done.

    However, it just seems to freeze when I say
    reader.readLine());


    Let me show you the code: (This is based on the celsius converter from Sun, heavily modified, shares some old variable names for auto generated code from the GUI modifier in netbeans, really cool feature I never knew about.)

    Anyway
    Java Code:
    /*
     * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     *   - Redistributions of source code must retain the above copyright
     *     notice, this list of conditions and the following disclaimer.
     *
     *   - Redistributions in binary form must reproduce the above copyright
     *     notice, this list of conditions and the following disclaimer in the
     *     documentation and/or other materials provided with the distribution.
     *
     *   - Neither the name of Sun Microsystems nor the names of its
     *     contributors may be used to endorse or promote products derived
     *     from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     * TheGUI.java
     *
     */
    package learn;
    
    import java.util.LinkedList;
    import java.io.*;
    
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.imageio.*;
    
    public class TheGUI extends javax.swing.JFrame {
    
        int PrimestoCompute = 0;
        int NumberofPrimes = 0;
        LinkedList TempNodes = new LinkedList();
        LinkedList RealNodes = new LinkedList();
        BufferedReader reader;
    
        /** Creates new form TheGUI */
        public TheGUI() {
            initComponents();
            setTitle("3dslice.net PrimeGen");
            this.setIconImage(Toolkit.getDefaultToolkit().getImage("lolface.jpg"));
    
    
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            tempTextField = new javax.swing.JTextField();
            celsiusLabel = new javax.swing.JLabel();
            convertButton = new javax.swing.JButton();
            fahrenheitLabel = new javax.swing.JLabel();
            tempTextField1 = new javax.swing.JTextField();
            tempTextField2 = new javax.swing.JTextField();
            tempTextField3 = new javax.swing.JTextField();
            tempTextField4 = new javax.swing.JTextField();
            tempTextField5 = new javax.swing.JTextField();
            tempTextField6 = new javax.swing.JTextField();
            tempTextField7 = new javax.swing.JTextField();
            tempTextField8 = new javax.swing.JTextField();
            tempTextField9 = new javax.swing.JTextField();
            celsiusLabel1 = new javax.swing.JLabel();
            celsiusLabel2 = new javax.swing.JLabel();
            celsiusLabel3 = new javax.swing.JLabel();
            celsiusLabel4 = new javax.swing.JLabel();
            celsiusLabel5 = new javax.swing.JLabel();
            celsiusLabel6 = new javax.swing.JLabel();
            celsiusLabel7 = new javax.swing.JLabel();
            celsiusLabel8 = new javax.swing.JLabel();
            celsiusLabel9 = new javax.swing.JLabel();
            tempTextField10 = new javax.swing.JTextField();
            celsiusLabel10 = new javax.swing.JLabel();
            convertButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Celsius Converter");
    
            tempTextField.setText("0");
            tempTextField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextFieldActionPerformed(evt);
                }
            });
    
            celsiusLabel.setText("# Of Prime Numbers to Calculate");
    
            convertButton.setText("Calculate!");
            convertButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButtonActionPerformed(evt);
                }
            });
    
            fahrenheitLabel.setText("3dslice.net");
    
            tempTextField1.setText("0.0.0.0");
            tempTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField1ActionPerformed(evt);
                }
            });
    
            tempTextField2.setText("0.0.0.0");
            tempTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField2ActionPerformed(evt);
                }
            });
    
            tempTextField3.setText("0.0.0.0");
            tempTextField3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField3ActionPerformed(evt);
                }
            });
    
            tempTextField4.setText("0.0.0.0");
            tempTextField4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField4ActionPerformed(evt);
                }
            });
    
            tempTextField5.setText("0.0.0.0");
            tempTextField5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField5ActionPerformed(evt);
                }
            });
    
            tempTextField6.setText("0.0.0.0");
            tempTextField6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField6ActionPerformed(evt);
                }
            });
    
            tempTextField7.setText("0.0.0.0");
            tempTextField7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField7ActionPerformed(evt);
                }
            });
    
            tempTextField8.setText("0.0.0.0");
            tempTextField8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField8ActionPerformed(evt);
                }
            });
    
            tempTextField9.setText("0.0.0.0");
            tempTextField9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField9ActionPerformed(evt);
                }
            });
    
            celsiusLabel1.setText("Client 1");
    
            celsiusLabel2.setText("Client 2");
    
            celsiusLabel3.setText("Client 4");
    
            celsiusLabel4.setText("Client 3");
    
            celsiusLabel5.setText("Client 5");
    
            celsiusLabel6.setText("Client 6");
    
            celsiusLabel7.setText("Client 7");
    
            celsiusLabel8.setText("Client 8");
    
            celsiusLabel9.setText("Client 9");
    
            tempTextField10.setText("0.0.0.0");
            tempTextField10.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField10ActionPerformed(evt);
                }
            });
    
            celsiusLabel10.setText("Client 10");
    
            convertButton1.setText("Clients Ready");
            convertButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tempTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField5)
                            .addComponent(tempTextField6)
                            .addComponent(tempTextField7)
                            .addComponent(tempTextField8)
                            .addComponent(tempTextField9)
                            .addComponent(tempTextField10, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(tempTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(convertButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(fahrenheitLabel)
                        .addComponent(celsiusLabel9)
                        .addComponent(celsiusLabel10)
                        .addComponent(celsiusLabel8)
                        .addComponent(celsiusLabel7)
                        .addComponent(celsiusLabel6)
                        .addComponent(celsiusLabel5)
                        .addComponent(celsiusLabel3)
                        .addComponent(celsiusLabel4)
                        .addComponent(celsiusLabel2)
                        .addComponent(celsiusLabel1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(celsiusLabel)
                            .addComponent(convertButton)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel5))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel6))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel7))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel8))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel9))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(celsiusLabel10))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(convertButton1)
                        .addComponent(convertButton))
                    .addGap(27, 27, 27)
                    .addComponent(fahrenheitLabel)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    //Parse degrees Celsius as a double and convert to Fahrenheit
    
    
            int numberofPrimes = (int) ((Double.parseDouble(tempTextField.getText())));
            NumberofPrimes = numberofPrimes;
            System.out.println(NumberofPrimes);
    
    
    
    
    
            String get1 = tempTextField1.getText();
            TempNodes.add(get1);
    
            String get2 = tempTextField2.getText();
            TempNodes.add(get2);
    
            String get3 = tempTextField3.getText();
            TempNodes.add(get3);
    
            String get4 = tempTextField4.getText();
            TempNodes.add(get4);
    
            String get5 = tempTextField5.getText();
            TempNodes.add(get5);
    
            String get6 = tempTextField6.getText();
            TempNodes.add(get6);
    
            String get7 = tempTextField7.getText();
            TempNodes.add(get7);
    
            String get8 = tempTextField8.getText();
            TempNodes.add(get8);
    
            String get9 = tempTextField9.getText();
            TempNodes.add(get9);
    
            String get10 = tempTextField10.getText();
            TempNodes.add(get10);
    
    
            for (int i = 0; i < 10; i++) {
                if (!(TempNodes.get(i).equals("0.0.0.0"))) {
                    RealNodes.add(TempNodes.get(i));
                }
            }
    
            for (int k = 0; k < RealNodes.size(); k++) {
                System.out.println(RealNodes.get(k));
            }
            int count = 0;
    
            try {
                ServerSocket serverSock = new ServerSocket(4242);
                while (true) {
                    Socket sock = serverSock.accept();
                    PrintWriter writer = new PrintWriter(sock.getOutputStream());
    
                    if (count < RealNodes.size()) {
                        writer.println(NumberofPrimes / RealNodes.size() + count * NumberofPrimes / RealNodes.size() + " " + (count + 1 * NumberofPrimes / RealNodes.size() - NumberofPrimes / RealNodes.size()));
    
                        count++;
    
    
                    InputStreamReader isreader = new InputStreamReader(sock.getInputStream());
                   reader = new BufferedReader(isreader);
    
                 [B]  System.out.println(reader.readLine()); //FREEZES HERE![/B]
                   writer.close();
    
    
                    }
    
    
                  
                    //convertStreamToString(isreader);
    
             //       String lol = reader.toString();
    
           //         System.out.println(lol);
                    writer.close();
                 
    
                }
    
    
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
    
    
    
            //System.out.println(primeGen(NumberofPrimes));
    
        }                                             
    
        private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
        }                                             
    
        private void tempTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField7ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField8ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField9ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField10ActionPerformed(java.awt.event.ActionEvent evt) {                                                
            // TODO add your handling code here:
        }                                               
    
        private void convertButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
    
    
    
            
    
    
    
    
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new TheGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JLabel celsiusLabel;
        private javax.swing.JLabel celsiusLabel1;
        private javax.swing.JLabel celsiusLabel10;
        private javax.swing.JLabel celsiusLabel2;
        private javax.swing.JLabel celsiusLabel3;
        private javax.swing.JLabel celsiusLabel4;
        private javax.swing.JLabel celsiusLabel5;
        private javax.swing.JLabel celsiusLabel6;
        private javax.swing.JLabel celsiusLabel7;
        private javax.swing.JLabel celsiusLabel8;
        private javax.swing.JLabel celsiusLabel9;
        private javax.swing.JButton convertButton;
        private javax.swing.JButton convertButton1;
        private javax.swing.JLabel fahrenheitLabel;
        private javax.swing.JTextField tempTextField;
        private javax.swing.JTextField tempTextField1;
        private javax.swing.JTextField tempTextField10;
        private javax.swing.JTextField tempTextField2;
        private javax.swing.JTextField tempTextField3;
        private javax.swing.JTextField tempTextField4;
        private javax.swing.JTextField tempTextField5;
        private javax.swing.JTextField tempTextField6;
        private javax.swing.JTextField tempTextField7;
        private javax.swing.JTextField tempTextField8;
        private javax.swing.JTextField tempTextField9;
        // End of variables declaration
    
        public String convertStreamToString(InputStream is) throws IOException {
    
    
            if (is != null) {
                StringBuilder sb = new StringBuilder();
                String line;
                try {
                    BufferedReader reader2 = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    while ((line = reader2.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                } finally {
                    is.close();
                }
                return sb.toString();
            } else {
                return "";
    
            }
        }
    }



    Then here is the Client
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author JigglyWiggly
     */
    import java.io.*;
    import java.net.*;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    
    public class PrimeClient {
        BufferedReader reader;
        PrintWriter writer;
    
        public void go() {
            try {
                String socker = "";
                socker = JOptionPane.showInputDialog("Enter the Server's IP Address");
    
    
    
    
                Socket s = new Socket(socker, 4242);
                InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
                 reader = new BufferedReader(streamReader);
                 writer = new PrintWriter(s.getOutputStream());
    
          //      Thread readerThread = new Thread(new IncomingReader());
            //    readerThread.start();
    
                String numberstocomputeupto = reader.readLine();
                reader.close();
                System.out.println(numberstocomputeupto);
    
                StringTokenizer st = new StringTokenizer(numberstocomputeupto);
                int first = 0;
                int second = 0;
    
                first = Integer.parseInt(st.nextToken());
                second = Integer.parseInt(st.nextToken());
    
                System.out.println("Check");
                System.out.println(first);
                System.out.println(second);
    
                primeGen(first, second);
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public void primeGen(int compute, int begin) {
    
            int i = 0;
            int k = 0;
            int count = 0;
            for (i = begin; i < compute; i++) {
                if (!(i % 2 == 0 && i != 2)) //on purposely confusing
                {
                    for (k = 2; k <= i / 2; k++) {
                        if (i % k == 0) {
                            break;
                        }
                    }
    
                    if (k > i / 2) {
                        System.out.println(i);
                        count++;
                    }
                }
            }
            System.out.println("\nTotal: " + count);
    
    
            try {
            writer.println(count);
            writer.flush();
            writer.close();
            reader.close();
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    
      /*  public class IncomingReader implements Runnable {
    
            public void run() {
            String message;
            try {
                while((message = reader.readLine()) !=null) {
                    System.out.println("read " + message);
                   
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            }
    
        }
    */
        public static void main(String[] args) {
            PrimeClient client = new PrimeClient();
            client.go();
        }
    }
    I mean I saw something with threads, do I need threads if I want to read and send from the server/client? I don't see why I would...
    All feedback is greatly appreciated.

  2. #2
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Anyone...? I know it's really long, but on the first code thingy, just look to where I say //FREEZES HERE

    You can ctrl +F that, I mean I would give the line number, except this forum doesn't have that.

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ok I changed my code a lot, and now this happens I needed threading.

    Java Code:
    /*
     * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     *   - Redistributions of source code must retain the above copyright
     *     notice, this list of conditions and the following disclaimer.
     *
     *   - Redistributions in binary form must reproduce the above copyright
     *     notice, this list of conditions and the following disclaimer in the
     *     documentation and/or other materials provided with the distribution.
     *
     *   - Neither the name of Sun Microsystems nor the names of its
     *     contributors may be used to endorse or promote products derived
     *     from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     * TheGUI.java
     *
     */
    package learn;
    
    
    
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.DataLine;
    
    
    public class TheGUI extends javax.swing.JFrame {
    
        int PrimestoCompute = 0;
        int NumberofPrimes = 0;
        LinkedList TempNodes = new LinkedList();
        LinkedList RealNodes = new LinkedList();
        BufferedReader reader;
        ServerSocket serverSock = new ServerSocket(4242);
        Socket sock;
    
    
        /** Creates new form TheGUI */
        public TheGUI() throws IOException {
            initComponents();
            setTitle("3dslice.net PrimeGen");
            this.setIconImage(Toolkit.getDefaultToolkit().getImage("lolface.jpg"));
    
    
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            tempTextField = new javax.swing.JTextField();
            LabelTOP = new javax.swing.JLabel();
            convertButton = new javax.swing.JButton();
            LabelBottom = new javax.swing.JLabel();
            tempTextField1 = new javax.swing.JTextField();
            tempTextField2 = new javax.swing.JTextField();
            tempTextField3 = new javax.swing.JTextField();
            tempTextField4 = new javax.swing.JTextField();
            tempTextField5 = new javax.swing.JTextField();
            tempTextField6 = new javax.swing.JTextField();
            tempTextField7 = new javax.swing.JTextField();
            tempTextField8 = new javax.swing.JTextField();
            tempTextField9 = new javax.swing.JTextField();
            Client1 = new javax.swing.JLabel();
            Client2 = new javax.swing.JLabel();
            Client4 = new javax.swing.JLabel();
            Client3 = new javax.swing.JLabel();
            Client5 = new javax.swing.JLabel();
            Client6 = new javax.swing.JLabel();
            Client7 = new javax.swing.JLabel();
            Client8 = new javax.swing.JLabel();
            Client9 = new javax.swing.JLabel();
            tempTextField10 = new javax.swing.JTextField();
            Client10 = new javax.swing.JLabel();
            convertButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Celsius Converter");
    
            tempTextField.setText("0");
            tempTextField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextFieldActionPerformed(evt);
                }
            });
    
            LabelTOP.setText("# Of Prime Numbers to Calculate");
    
            convertButton.setText("Calculate!");
            convertButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButtonActionPerformed(evt);
                }
            });
    
            LabelBottom.setText("3dslice.net");
    
            tempTextField1.setText("0.0.0.0");
            tempTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField1ActionPerformed(evt);
                }
            });
    
            tempTextField2.setText("0.0.0.0");
            tempTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField2ActionPerformed(evt);
                }
            });
    
            tempTextField3.setText("0.0.0.0");
            tempTextField3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField3ActionPerformed(evt);
                }
            });
    
            tempTextField4.setText("0.0.0.0");
            tempTextField4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField4ActionPerformed(evt);
                }
            });
    
            tempTextField5.setText("0.0.0.0");
            tempTextField5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField5ActionPerformed(evt);
                }
            });
    
            tempTextField6.setText("0.0.0.0");
            tempTextField6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField6ActionPerformed(evt);
                }
            });
    
            tempTextField7.setText("0.0.0.0");
            tempTextField7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField7ActionPerformed(evt);
                }
            });
    
            tempTextField8.setText("0.0.0.0");
            tempTextField8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField8ActionPerformed(evt);
                }
            });
    
            tempTextField9.setText("0.0.0.0");
            tempTextField9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField9ActionPerformed(evt);
                }
            });
    
            Client1.setText("Client 1");
    
            Client2.setText("Client 2");
    
            Client4.setText("Client 4");
    
            Client3.setText("Client 3");
    
            Client5.setText("Client 5");
    
            Client6.setText("Client 6");
    
            Client7.setText("Client 7");
    
            Client8.setText("Client 8");
    
            Client9.setText("Client 9");
    
            tempTextField10.setText("0.0.0.0");
            tempTextField10.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    tempTextField10ActionPerformed(evt);
                }
            });
    
            Client10.setText("Client 10");
    
            convertButton1.setText("Help");
            convertButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    convertButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tempTextField2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField5)
                            .addComponent(tempTextField6)
                            .addComponent(tempTextField7)
                            .addComponent(tempTextField8)
                            .addComponent(tempTextField9)
                            .addComponent(tempTextField10, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                            .addComponent(tempTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tempTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(convertButton1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(LabelBottom)
                        .addComponent(Client9)
                        .addComponent(Client10)
                        .addComponent(Client8)
                        .addComponent(Client7)
                        .addComponent(Client6)
                        .addComponent(Client5)
                        .addComponent(Client4)
                        .addComponent(Client3)
                        .addComponent(Client2)
                        .addComponent(Client1)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(LabelTOP)
                            .addComponent(convertButton)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(LabelTOP))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(tempTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client2))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client5))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client6))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client7))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client8))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client9))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tempTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Client10))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(convertButton1)
                        .addComponent(convertButton))
                    .addGap(27, 27, 27)
                    .addComponent(LabelBottom)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    
    
           
    
    
            int numberofPrimes = (int) ((Double.parseDouble(tempTextField.getText())));
            NumberofPrimes = numberofPrimes;
            System.out.println(NumberofPrimes);
    
    
    
    
    
            String get1 = tempTextField1.getText();
            TempNodes.add(get1);
    
            String get2 = tempTextField2.getText();
            TempNodes.add(get2);
    
            String get3 = tempTextField3.getText();
            TempNodes.add(get3);
    
            String get4 = tempTextField4.getText();
            TempNodes.add(get4);
    
            String get5 = tempTextField5.getText();
            TempNodes.add(get5);
    
            String get6 = tempTextField6.getText();
            TempNodes.add(get6);
    
            String get7 = tempTextField7.getText();
            TempNodes.add(get7);
    
            String get8 = tempTextField8.getText();
            TempNodes.add(get8);
    
            String get9 = tempTextField9.getText();
            TempNodes.add(get9);
    
            String get10 = tempTextField10.getText();
            TempNodes.add(get10);
    
    
            for (int i = 0; i < 10; i++) {
                if (!(TempNodes.get(i).equals("0.0.0.0"))) {
                    RealNodes.add(TempNodes.get(i));
                }
            }
            if(RealNodes.size()==0) {
                JOptionPane.showMessageDialog(this, "Enter an IP address!");
                System.exit(0);
            }
    
             try{
    		AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("leeroy.wav"));
    		AudioFormat format = stream.getFormat();
    		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
    		Clip clip = (Clip) AudioSystem.getLine(info);
    		 clip.open(stream);
    		 clip.start();
    	} catch (Exception e)
    	{
    	 	e.printStackTrace();
    	}
    
            for (int k = 0; k < RealNodes.size(); k++) {
                System.out.println(RealNodes.get(k));
            }
            int count = 0;
    
            try {
               
               while(true) {
                     sock = serverSock.accept();
                    PrintWriter writer = new PrintWriter(sock.getOutputStream());
    
                    if (count < RealNodes.size()) {
    
                        writer.println(NumberofPrimes / RealNodes.size() + count * NumberofPrimes / RealNodes.size() + " " + (count + 1 * NumberofPrimes / RealNodes.size() - NumberofPrimes / RealNodes.size()));
                        writer.close();
                        count++;
    
    
          //             System.out.println(sock.isBound());
             
    
                    Thread readerThread = new Thread(new IncomingReader());
                    readerThread.start();
    
    
    
    
    
    
                  
               //    writer.close();
                    }
    
    
    
                    //convertStreamToString(isreader);
    
             //       String lol = reader.toString();
    
           //         System.out.println(lol);
               //     writer.close();
    
    
                
               }
                    
    
    
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
    
    
    
            //System.out.println(primeGen(NumberofPrimes));
    
        }                                             
    
        private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
        }                                             
    
        private void tempTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField7ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField8ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField9ActionPerformed(java.awt.event.ActionEvent evt) {                                               
            // TODO add your handling code here:
        }                                              
    
        private void tempTextField10ActionPerformed(java.awt.event.ActionEvent evt) {                                                
            // TODO add your handling code here:
        }                                               
    
        private void convertButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
    
    try{
    		AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("epiclaugh.wav"));
    		AudioFormat format = stream.getFormat();
    		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
    		Clip clip = (Clip) AudioSystem.getLine(info);
    		 clip.open(stream);
    		 clip.start();
    	} catch (Exception e)
    	{
    	 	e.printStackTrace();
    	}
    
    
        }                                              
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    try {
                        new TheGUI().setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(TheGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLabel Client1;
        private javax.swing.JLabel Client10;
        private javax.swing.JLabel Client2;
        private javax.swing.JLabel Client3;
        private javax.swing.JLabel Client4;
        private javax.swing.JLabel Client5;
        private javax.swing.JLabel Client6;
        private javax.swing.JLabel Client7;
        private javax.swing.JLabel Client8;
        private javax.swing.JLabel Client9;
        private javax.swing.JLabel LabelBottom;
        private javax.swing.JLabel LabelTOP;
        private javax.swing.JButton convertButton;
        private javax.swing.JButton convertButton1;
        private javax.swing.JTextField tempTextField;
        private javax.swing.JTextField tempTextField1;
        private javax.swing.JTextField tempTextField10;
        private javax.swing.JTextField tempTextField2;
        private javax.swing.JTextField tempTextField3;
        private javax.swing.JTextField tempTextField4;
        private javax.swing.JTextField tempTextField5;
        private javax.swing.JTextField tempTextField6;
        private javax.swing.JTextField tempTextField7;
        private javax.swing.JTextField tempTextField8;
        private javax.swing.JTextField tempTextField9;
        // End of variables declaration                   
    
    public class IncomingReader implements Runnable {
        public void run() {
            String message;
            String fart = "";
                try {
    
                  [B]  InputStreamReader isreader = new InputStreamReader(sock.getInputStream()); //HERE IS WHER EIT COMPLAINS!
    
                    reader = new BufferedReader(isreader);[/B]  //HERE IS WHER EIT COMPLAINS!
    
                    System.out.println("hi");
                    while ((message = reader.readLine()) != null) {
                         System.out.println("read" + message);
                          fart+=message;
                    }
                    
                   
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        }
    }
        
    }
    Client:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author JigglyWiggly
     */
    import java.io.*;
    import java.net.*;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    
    
    public class PrimeClient {
        BufferedReader reader;
        PrintWriter writer;
        String numberstocomputeupto="";
        public void go() {
            try {
                String socker = "";
                socker = JOptionPane.showInputDialog("Enter the Server's IP Address");
    
    
    
    
                 Socket s = new Socket("127.0.0.1", 4242);
                 InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
                 reader = new BufferedReader(streamReader);
                 writer = new PrintWriter(s.getOutputStream());
    
    
    
                 //Thread readerThread = new Thread (new IncomingReader());
                 //readerThread.start();
    
    
           numberstocomputeupto = reader.readLine();    
             reader.close();
          //      System.out.println(numberstocomputeupto);
                System.out.println(numberstocomputeupto);
                StringTokenizer st = new StringTokenizer(numberstocomputeupto);
                int first = 0;
                int second = 0;
    
                first = Integer.parseInt(st.nextToken());
                second = Integer.parseInt(st.nextToken());
    
                System.out.println("Check");
                System.out.println(first);
                System.out.println(second);
    
                primeGen(first, second);
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public void primeGen(int compute, int begin) {
    
            int i = 0;
            int k = 0;
            int count = 0;
            String total="";
            for (i = begin; i < compute; i++) {
                if (!(i % 2 == 0 && i != 2)) //on purposely confusing
                {
                    for (k = 2; k <= i / 2; k++) {
                        if (i % k == 0) {
                            break;
                        }
                    }
    
                    if (k > i / 2) {
                        System.out.println(i);
                        total+=i;
                        count++;
                    }
                }
            }
            total+=count;
            System.out.println("\nTotal: " + count);
    
    
            try {
            writer.println(total);
            writer.close();
    
         //   reader.close();
            } catch(Exception ex) {
                ex.printStackTrace();
            }
        }
    
      /*  public class IncomingReader implements Runnable {
    
            public void run() {
            String message;
            try {
                while((message = reader.readLine()) !=null) {
                    System.out.println("read " + message);
                   
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            }
    
        }
    */
        public static void main(String[] args) {
            PrimeClient client = new PrimeClient();
            client.go();
        }
    
       /*  public class IncomingReader implements Runnable {
            public void run() {
                String message;
                try {
                    while ((message = reader.readLine()) != null) {
                        System.out.println("client read " + message);
                        numberstocomputeupto+=reader.readLine();
                    }
                } catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    
    
    
        * 
        */
    }
    Error log:
    Java Code:
    init:
    deps-jar:
    Deleted 16 out of date files in 0 seconds
    Compiling 1 source file to C:\Users\JigglyWiggly\Documents\My Dropbox\Java\PrimeNumberGeneratorCluster\build\classes
    Note: C:\Users\JigglyWiggly\Documents\My Dropbox\Java\PrimeNumberGeneratorCluster\src\learn\TheGUI.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    compile-single:
    run-single:
    1000
    127.0.0.1
    java.net.SocketException: Socket is closed
            at java.net.Socket.getInputStream(Socket.java:788)
          [B]  at learn.TheGUI$IncomingReader.run(TheGUI.java:559)[/B]
            at java.lang.Thread.run(Thread.java:619)
    BUILD STOPPED (total time: 32 seconds)
    Why is the socket being closed when I want to open it there? (It shows the error at the end of TheGUI.java (the first long book of code lol)

  4. #4
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ok I'll post a much more condensed version, anyone...? I changed the code around a bit...
    Java Code:
    public class TheGUI extends javax.swing.JFrame {
    
        int PrimestoCompute = 0;
        int NumberofPrimes = 0;
        LinkedList TempNodes = new LinkedList();
        LinkedList RealNodes = new LinkedList();
        BufferedReader reader;
        ServerSocket serverSock = new ServerSocket(4242);
        Socket sock;
           Thread readerThread = new Thread(new IncomingReader());
    
    
        /** Creates new form TheGUI */
        public TheGUI() throws IOException {
            initComponents();
            setTitle("3dslice.net PrimeGen");
            this.setIconImage(Toolkit.getDefaultToolkit().getImage("lolface.jpg"));
    
    
        }
    
     private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    
    
           
    
    
            int numberofPrimes = (int) ((Double.parseDouble(tempTextField.getText())));
            NumberofPrimes = numberofPrimes;
            System.out.println(NumberofPrimes);
    
    
    
    
    
            String get1 = tempTextField1.getText();
            TempNodes.add(get1);
    
            String get2 = tempTextField2.getText();
            TempNodes.add(get2);
    
            String get3 = tempTextField3.getText();
            TempNodes.add(get3);
    
            String get4 = tempTextField4.getText();
            TempNodes.add(get4);
    
            String get5 = tempTextField5.getText();
            TempNodes.add(get5);
    
            String get6 = tempTextField6.getText();
            TempNodes.add(get6);
    
            String get7 = tempTextField7.getText();
            TempNodes.add(get7);
    
            String get8 = tempTextField8.getText();
            TempNodes.add(get8);
    
            String get9 = tempTextField9.getText();
            TempNodes.add(get9);
    
            String get10 = tempTextField10.getText();
            TempNodes.add(get10);
    
    
            for (int i = 0; i < 10; i++) {
                if (!(TempNodes.get(i).equals("0.0.0.0"))) {
                    RealNodes.add(TempNodes.get(i));
                }
            }
            if(RealNodes.size()==0) {
                JOptionPane.showMessageDialog(this, "Enter an IP address!");
                System.exit(0);
            }
    
             try{
    		AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("leeroy.wav"));
    		AudioFormat format = stream.getFormat();
    		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());
    		Clip clip = (Clip) AudioSystem.getLine(info);
    		 clip.open(stream);
    		 clip.start();
    	} catch (Exception e)
    	{
    	 	e.printStackTrace();
    	}
    
            for (int k = 0; k < RealNodes.size(); k++) {
                System.out.println(RealNodes.get(k));
            }
            int count = 0;
    
            try {
              
               
    
    
               while(true) {
                     sock = serverSock.accept();
                    PrintWriter writer = new PrintWriter(sock.getOutputStream());
    
                    if (count < RealNodes.size()) {
    
                        writer.println(NumberofPrimes / RealNodes.size() + count * NumberofPrimes / RealNodes.size() + " " + (count + 1 * NumberofPrimes / RealNodes.size() - NumberofPrimes / RealNodes.size()));
                        writer.close();
                        count++;
                      readerThread.start();
                     
    
          //             System.out.println(sock.isBound());
             
    
                  
    
    
    
    
    
                  
               //    writer.close();
                    }
    
    
    
                    //convertStreamToString(isreader);
    
             //       String lol = reader.toString();
    
           
               //     writer.close();
    
    
         
               }
                    
    
    
    
            } catch (IOException ex) {
                ex.printStackTrace();
            }
     public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    try {
                        new TheGUI().setVisible(true);
                    } catch (IOException ex) {
                        Logger.getLogger(TheGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        }
    
    public class IncomingReader implements Runnable {
        public void run() {
            String message;
            String fart = "";
                try {
                 //    sock2 = serverSock.accept();
                   // InputStreamReader isreader = new InputStreamReader(sock2.getInputStream());
                    //reader = new BufferedReader(isreader);
    
                    
                    System.out.println("GOGOGO");
                    while ((message = reader.readLine()) != null) {
                          System.out.println("read" + message);
                          fart+=message;
                         
                    }
                     reader.close();
                   
                   
                } catch (IOException ex) {
                   ex.printStackTrace();
                }
        }
        }

    Here is the output
    Java Code:
    init:
    deps-jar:
    Deleted 16 out of date files in 0 seconds
    Compiling 1 source file to C:\Users\JigglyWiggly\Documents\My Dropbox\Java\PrimeNumberGeneratorCluster\build\classes
    Note: C:\Users\JigglyWiggly\Documents\My Dropbox\Java\PrimeNumberGeneratorCluster\src\learn\TheGUI.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    compile-single:
    run-single:
    1000
    127.0.0.1
    GOGOGO
    Exception in thread "Thread-2" java.lang.NullPointerException
            at learn.TheGUI$IncomingReader.run(TheGUI.java:568)
            at java.lang.Thread.run(Thread.java:619)
    [/code]

    It's pointing to when I say while ((message = reader.readLine()) != null) {
    And it says nullpointer exception... I mean I understand if there is no data, but like why is it saying this, I am saying if it's not null!

    This is driving me bonkers help plz.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jigglywiggly View Post
    It's pointing to when I say while ((message = reader.readLine()) != null) {
    And it says nullpointer exception... I mean I understand if there is no data, but like why is it saying this, I am saying if it's not null!
    The 'reader' itself could be null ...

    kind regards,

    Jos

Similar Threads

  1. Networking in Java
    By thangavel14 in forum Networking
    Replies: 3
    Last Post: 07-30-2009, 01:47 AM
  2. Java networking.
    By tim in forum New To Java
    Replies: 7
    Last Post: 07-16-2009, 10:43 PM
  3. java networking via browser
    By pratik.ac in forum Networking
    Replies: 2
    Last Post: 04-21-2008, 05:25 PM
  4. Java networking programming (II)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 12-27-2007, 06:19 PM
  5. Java networking programming (I)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 12-24-2007, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •