[SOLVED] Java Socket Example for sending and receiving byte array


Welcome again to my blog! A lot of people have asked me to share a working example of sending and receiving byte array using Java socket programming. So, below is an example for you to get started:
Server.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// A Java program for a Server 
import java.net.*;
import java.util.Arrays;
import java.io.*; 

public class Server 
{ 
 //initialize socket and input stream 
 private Socket   socket = null; 
 private ServerSocket server = null; 
 private InputStream in  = null; 
 private OutputStream out = null;

 // constructor with port 
 public Server(int port) 
 { 
  // starts server and waits for a connection 
  try
  { 
   server = new ServerSocket(port); 
   System.out.println("Server started"); 

   System.out.println("Waiting for a client ..."); 

   socket = server.accept(); 

   System.out.println("Client accepted"); 

   // takes input from the client socket 
   in = new DataInputStream(socket.getInputStream()); 
   //writes on client socket
   out = new DataOutputStream(socket.getOutputStream());

   // Receiving data from client
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   byte buffer[] = new byte[1024];
   baos.write(buffer, 0 , in.read(buffer));
   
   byte result[] = baos.toByteArray();

   String res = Arrays.toString(result);
   System.out.println("Recieved from client : "+res); 
   
   //echoing back to client
   out.write(result);
   
   System.out.println("Closing connection"); 

   // close connection 
   socket.close(); 
   in.close(); 
  } 
  catch(IOException i) 
  { 
   System.out.println(i); 
  } 
 } 

 public static void main(String args[]) 
 { 
  new Server(5000); 
 } 
}

Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// A Java program for a Client 
import java.net.*;
import java.util.Arrays;
import java.io.*;

public class Client {
 // initialize socket and input output streams
 private Socket socket = null;
 private OutputStream out = null;
 private InputStream in = null;

 // constructor to put ip address and port
 public Client(String address, int port) {
  // establish a connection
  try {
   socket = new Socket(address, port);
   if (socket.isConnected()) {
    System.out.println("Connected");
   }

   // sends output to the socket
   out = new DataOutputStream(socket.getOutputStream());
   //takes input from socket
   in = new DataInputStream(socket.getInputStream());
  } catch (UnknownHostException u) {
   System.out.println(u);
  } catch (IOException i) {
   System.out.println(i);
  }

  
  try {
   byte[] arr = {(byte)0x00, (byte)0x01, (byte)0x00, (byte)0x10, (byte)0x00, (byte)0x01,
       (byte)0x00, (byte)0x1F, (byte)0x60, (byte)0x1D, (byte)0xA1, (byte)0x09,
       (byte)0x06, (byte)0x07, (byte)0x60, (byte) 0x85, (byte)0x74, (byte)0x05,
       (byte) 0x08, (byte)0x01, (byte)0x01, (byte)0xBE, (byte)0x10, (byte)0x04,
       (byte)0x0E, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x06,
       (byte)0x5F, (byte)0x1F, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x18,
       (byte)0x1D, (byte)0xFF, (byte)0xFF};

   // sending data to server
   out.write(arr);

   String req = Arrays.toString(arr);
   //printing request to console
   System.out.println("Sent to server : " + req);

   // Receiving reply from server
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   byte buffer[] = new byte[1024];
   baos.write(buffer, 0 , in.read(buffer));
   
   byte result[] = baos.toByteArray();

   String res = Arrays.toString(result);

   // printing reply to console
   System.out.println("Recieved from server : " + res);
  } catch (IOException i) {
   System.out.println(i);
  }
  // }

  // close the connection
  try {
   // input.close();
   in.close();
   out.close();
   socket.close();
  } catch (IOException i) {
   System.out.println(i);
  }
 }

 public static void main(String args[]) {
  new Client("127.0.0.1", 5000);
 }
}

The Client sends a byte array to theServerServer receives the byte array from the Client and sends the same byte array to the Client.
I have added comments in the code to explain the workflow more clearly.
I hope you have understood the concept. In case of any doubts, you are free to comment below.
Thanks for reading! Happy coding :)
Buy Me a Coffee - https://www.buymeacoffee.com/agautam

Comments

  1. Thanks for sharing this information. This is really useful. Keep doing more.
    hire ASP.NET developer

    ReplyDelete
  2. The content you've posted here is fantastic because it provides some excellent information that will be quite beneficial to me. Thank you for sharing that. Keep up the good work. app development company in usa

    ReplyDelete
  3. You've provided some very useful information. I'm glad I came into this article because it provides a lot of important information. Thank you for sharing this storey with us. healthcare social media agency

    ReplyDelete
  4. Obrigado brother material estraordinário parabéns por compartilhar.

    ReplyDelete

Post a Comment