From aaddee0a7e81ff268746f67afd964152c2be1393 Mon Sep 17 00:00:00 2001 From: rahusriv Date: Tue, 19 Mar 2019 11:00:14 +0530 Subject: [PATCH] Rahul/socket/read/pr2 (#6398) * Making examples simple * Changing variable names * Modificatons in naming * Read all data from server * Adding seperate TestSocketRead class having live test * Adding test case * Changing test name to live tests --- .../java/com/baeldung/socket/read/Client.java | 37 ++++++++++++++ .../java/com/baeldung/socket/read/Server.java | 51 +++++++++++++++++++ .../read/SocketReadAllDataLiveTest.java | 37 ++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 core-java-networking/src/main/java/com/baeldung/socket/read/Client.java create mode 100644 core-java-networking/src/main/java/com/baeldung/socket/read/Server.java create mode 100644 core-java-networking/src/test/java/com/baeldung/socket/read/SocketReadAllDataLiveTest.java diff --git a/core-java-networking/src/main/java/com/baeldung/socket/read/Client.java b/core-java-networking/src/main/java/com/baeldung/socket/read/Client.java new file mode 100644 index 0000000000..08292237f2 --- /dev/null +++ b/core-java-networking/src/main/java/com/baeldung/socket/read/Client.java @@ -0,0 +1,37 @@ +package com.baeldung.socket.read; + +import java.net.*; +import java.io.*; + +public class Client { + + //Initialize socket, input and output stream + private Socket socket = null; + private DataInputStream in = null; + private DataOutputStream out = null; + + public void runClient(String ip, int port) { + try { + socket = new Socket(ip, port); + System.out.println("Connected to server ..."); + in = new DataInputStream(System.in); + out = new DataOutputStream(socket.getOutputStream()); + } catch(Exception e) { + e.printStackTrace(); + } + char type = 's'; // s for string + int length = 29; + String data = "This is a string of length 29"; + byte[] dataInBytes = data.getBytes(); + //Sending data in TLV format + try { + out.writeChar(type); + out.writeInt(length); + out.write(dataInBytes); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/core-java-networking/src/main/java/com/baeldung/socket/read/Server.java b/core-java-networking/src/main/java/com/baeldung/socket/read/Server.java new file mode 100644 index 0000000000..bdb3a4449f --- /dev/null +++ b/core-java-networking/src/main/java/com/baeldung/socket/read/Server.java @@ -0,0 +1,51 @@ +package com.baeldung.socket.read; + +import java.net.*; +import java.io.*; + +public class Server { + + //Socket and input stream + private Socket socket = null; + private ServerSocket server = null; + private DataInputStream in = null; + + public void runServer(int port) { + //Start the server and wait for connection + try { + server = new ServerSocket(port); + System.out.println("Server Started. Waiting for connection ..."); + socket = server.accept(); + System.out.println("Got connection from client."); + //Get input stream from socket variable and convert the same to DataInputStream + in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + //Read type and length of data + char dataType = in.readChar(); + int length = in.readInt(); + System.out.println("Type : "+dataType); + System.out.println("Lenght :"+length); + //Read String data in bytes + byte[] messageByte = new byte[length]; + boolean end = false; + String dataString = ""; + int totalBytesRead = 0; + //We need to run while loop, to read all data in that stream + while(!end) { + int currentBytesRead = in.read(messageByte); + totalBytesRead = currentBytesRead + totalBytesRead; + if(totalBytesRead <= length) { + dataString += new String(messageByte,0,currentBytesRead); + } else { + dataString += new String(messageByte,0,length - totalBytesRead + currentBytesRead); + } + if(dataString.length()>=length) { + end = true; + } + } + System.out.println("Read "+length+" bytes of message from client. Message = "+dataString);; + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/core-java-networking/src/test/java/com/baeldung/socket/read/SocketReadAllDataLiveTest.java b/core-java-networking/src/test/java/com/baeldung/socket/read/SocketReadAllDataLiveTest.java new file mode 100644 index 0000000000..da7f5b8d3f --- /dev/null +++ b/core-java-networking/src/test/java/com/baeldung/socket/read/SocketReadAllDataLiveTest.java @@ -0,0 +1,37 @@ +package com.baeldung.socket.read; + +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; + +public class SocketReadAllDataLiveTest { + + @Test + public void givenServerAndClient_whenClientSendsAndServerReceivesData_thenCorrect() { + //Run server in new thread + Runnable runnable1 = () -> { runServer(); }; + Thread thread1 = new Thread(runnable1); + thread1.start(); + //Wait for 10 seconds + try { + TimeUnit.SECONDS.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + //Run client in a new thread + Runnable runnable2 = () -> { runClient(); }; + Thread thread2 = new Thread(runnable2); + thread2.start(); + } + + public static void runServer() { + //Run Server + Server server = new Server(); + server.runServer(5555); + } + + public static void runClient() { + //Run Client + Client client = new Client(); + client.runClient("127.0.0.1", 5555); + } +} \ No newline at end of file