Sending multiple byte arrays over a socket

I want to send multiple byte arrays from client and server?

I managed to send / receive one byte array from the client and send / receive one byte array from the server:

My code looks like this:

server:

 Socket sock=null;
  ByteArrayOutputStream input=null;
  OutputStream out=null;
    InputStream in=null;
  try{
      ServerSocket server_sock=new ServerSocket(2972);


    sock=server_sock.accept(); 

   in = 
       sock.getInputStream();
    out=sock.getOutputStream();
    }catch(IOException e){
      System.out.println(e.getMessage());
  }
 String word="";

  //1-Receive

  try{

    ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
 int len=0;
byte[] buf=new byte[1000];
        while ((len = in.read(buf))>=0) {
              serverinput.write(buf, 0, len);

        }

      sock.shutdownInput();

        word=new String(serverinput.toByteArray());
    System.out.println("Client send 1"+word);

    }catch(Exception e){
      System.out.println(e.getMessage());
  }


   String st="Server is a king";
try{ 
   out.write(st.getBytes());

   out.flush();

}catch(Exception e){
      System.out.println(e.getMessage());
  }

customer:

 Socket sock=null;
    OutputStream out=null;
    InputStream in=null;


  try{
      sock=new Socket("127.0.0.1",2972);
      }catch(IOException e){
      System.out.println(e.getMessage());
  }


String word="Hellow World"  ;
    try{
    in = 
       sock.getInputStream();
    out=sock.getOutputStream();
    }catch(IOException e){
      System.out.println(e.getMessage());
  }

//1- send
   try{

       System.out.println("Your string is"+word+"converted to byte"+word.getBytes());

    out.write(word.getBytes());
    out.flush();
 sock.shutdownOutput();
    }catch(Exception e){
      System.out.println(e.getMessage());
  }

    try{  ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
 int len=0;
byte[] buf=new byte[1000];
        while ((len = in.read(buf))>=0) {
              serverinput.write(buf, 0, len);


        }
    System.out.println("server send 1 "+new String(serverinput.toByteArray()));
     System.out.println("Your string is"+word+"converted to byte"+word.getBytes());

    }catch(Exception e){
      System.out.println(e.getMessage());
  }

This code works fine for a single send from the client and server, but it doesn’t work when I want to send / receive more byte array?

It only works when I use shutdown, because both read and write to client and server data.

Therefore, I cannot use the socket channel again ... is there an alternative solution? ... this does not lead to a deadlock.

+5
source share
4 answers

, , . ( " " . , , /, , Socket ..).

: DataOutputStream DataInputStream, :

  • :

    • .

    • DataOutputStream.writeInt(int).

    • DataOutputStream.write(byte[]).

  • :

    • DataInputStream.readInt().

    • .

    • DataInputStream.read(byte[], int, int)... , .

, , . , . , , .

. . , Java.

BufferedInputStreams BufferedOutputStreams ..., .

+13

DataInputStream DataOutputStream. , .

0

:

It seems to describe how to read and write to a socket. Reading should be as simple as creating a server socket and listening to the expected port, and then waiting for data.

0
source

You can also create an object that will contain your byte array and send it using ObjectOutputStream, and then use the method writeObject(Object)to send the information.

0
source

All Articles