Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
Hello everybody,
I need to use bufferedinputstream in blow code but not receive what is error in code?
thanks
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telemetry;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author Hossam
 */
public class ByteArrayReader {
 
    private String fileName;
 
    public ByteArrayReader(String fileName) {
        this.fileName = fileName;
    }
     private static ServerSocket servSock;
    private static final int PORT = 20100;
 
    public static void main(String[] args) throws IOException
    {
        System.out.println("Opening port...\n");
        try
        {
            servSock = new ServerSocket(PORT);      //Step 1.
        }
        catch(IOException ioEx)
        {
            System.out.println("Unable to attach to port!");
            System.exit(1);
        }
        do
        {
            handleClient();
        }
        while (true);
    }
 
    public static int[] handleClient() throws IOException
    {
        int[] values = null;
        Socket link;
        link = null; //Step 2.
        try
        {
            link = servSock.accept();        				//Step 2.

            Scanner input = new Scanner(link.getInputStream()); 	//Step 3.
            PrintWriter output =
		    new PrintWriter(link.getOutputStream(),true); 	//Step 3.
    BufferedInputStream is = new BufferedInputStream(link.getInputStream());
     DataInputStream din = new DataInputStream(is);
            int numMessages = 0;
            String message = input.nextLine(); 
           // //Step 4.
            while (!message.equals("***CLOSE***"))
            {
                  int count = (int) (message.length() );
        values = new int[count];
            for (int i = 0; i < count; i++) {
                values[i] = din.readUnsignedByte();
            }
             numMessages++;
                message = input.nextLine();
            }
        }
         catch(IOException ioEx)
        {
            ioEx.printStackTrace();
        }
        finally
        {
          System.out.println("bye");
            try
            {
                System.out.println( "\n* Closing connection... *");
                link.close();                    //Step 5.
            }
            catch(IOException ioEx)
            {
                System.out.println("Unable to disconnect!");
                System.exit(1);
            }
        }
        return values;
    }
 /* public static  int[] read() {
        int[] values = null;
        try {
            FileInputStream fin = null;
            File file = new File(fileName);
            fin = new FileInputStream(file);
            BufferedInputStream bin = new BufferedInputStream(fin);
            DataInputStream din = new DataInputStream(bin);
            int count = (int) (file.length() );
            //System.out.println(" count "+count);
            values = new int[count];
            for (int i = 0; i < count; i++) {
                values[i] = din.readUnsignedByte();
            }
 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return values;
    }*/
}
Posted 22-Apr-13 3:22am
Edited 22-Apr-13 4:40am
(no name)498.6K
v2
Comments
Prasad Khandekar at 22-Apr-13 10:09am
   
What is the format of your input message. Either use a scanner or DataInputStream, Don't use both simulteneously. From your code they operate on same stream and will not work correctly as read position will not get set properly.
 
Regards,
Amir Mohammad Nasrollahi at 15-Aug-13 2:32am
   
More info?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100