Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Socket connection client read incoming byte array

793912Dec 24 2008 — edited Dec 24 2008
Hi all

I have socket based client server application. And my question is how to read a byte array at client side. Here I'm sending byte array from server and using BufferedReader at client side how to write incoming data to a byte array?

Server code
 public static void main(String args[])
 {
        ServerSocket sock = null;                              // original server socket
        Socket clientSocket = null;                      // socket created by accept
        PrintWriter pw = null;                              // socket output stream
        BufferedReader br = null;  

        try
        {
            sock = new ServerSocket(serverPort);              
            clientSocket = sock.accept();                             
            pw = new PrintWriter(clientSocket.getOutputStream(), true);
            
            pw.println(getImageasByteArray());          // write byte array to socket
          
           // rest oh the code
        }
}

public static byte[] getImageasByteArray()
{
    // implementation
}
client code
public static void main(String args[])
{
        Socket sock = null;                              
        BufferedReader br = null;      
        try
        {
            sock = new Socket(serverIPname, serverPort);
            br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            
           String answer = br.readLine();  // here how to read the content to byte array
        }
}
So instead of reading the content to string(above case) I need to write whole content to byte array. So I have to create a byte array with the size known. So to do this I need to know the size of the incoming byte array size isn't it?
So can some one please help me.
It's great if u can direct me to implemented example.
Any help highly appreciated

Thank You
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 21 2009
Added on Dec 24 2008
2 comments
455 views