From 88342a96fa164b1e855a5c6fb9df486e7eaffd0e Mon Sep 17 00:00:00 2001 From: egimaben Date: Mon, 22 Aug 2016 20:24:47 +0300 Subject: [PATCH] added project code for sockets --- sockets/pom.xml | 30 ++++++++ .../java/com/baeldung/socket/EchoClient.java | 43 +++++++++++ .../com/baeldung/socket/EchoMultiServer.java | 71 +++++++++++++++++++ .../java/com/baeldung/socket/EchoServer.java | 50 +++++++++++++ .../java/com/baeldung/socket/GreetClient.java | 46 ++++++++++++ .../java/com/baeldung/socket/GreetServer.java | 47 ++++++++++++ .../com/baeldung/socket/EchoMultiTest.java | 44 ++++++++++++ .../java/com/baeldung/socket/EchoTest.java | 36 ++++++++++ .../com/baeldung/socket/GreetServerTest.java | 16 +++++ 9 files changed, 383 insertions(+) create mode 100644 sockets/pom.xml create mode 100644 sockets/src/main/java/com/baeldung/socket/EchoClient.java create mode 100644 sockets/src/main/java/com/baeldung/socket/EchoMultiServer.java create mode 100644 sockets/src/main/java/com/baeldung/socket/EchoServer.java create mode 100644 sockets/src/main/java/com/baeldung/socket/GreetClient.java create mode 100644 sockets/src/main/java/com/baeldung/socket/GreetServer.java create mode 100644 sockets/src/test/java/com/baeldung/socket/EchoMultiTest.java create mode 100644 sockets/src/test/java/com/baeldung/socket/EchoTest.java create mode 100644 sockets/src/test/java/com/baeldung/socket/GreetServerTest.java diff --git a/sockets/pom.xml b/sockets/pom.xml new file mode 100644 index 0000000000..1227667198 --- /dev/null +++ b/sockets/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + com.baeldung + sockets + 1.0 + sockets + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 7 + 7 + + + + + + + junit + junit + 4.3 + test + + + + diff --git a/sockets/src/main/java/com/baeldung/socket/EchoClient.java b/sockets/src/main/java/com/baeldung/socket/EchoClient.java new file mode 100644 index 0000000000..d008f4031a --- /dev/null +++ b/sockets/src/main/java/com/baeldung/socket/EchoClient.java @@ -0,0 +1,43 @@ +package com.baeldung.socket; + +import java.io.*; +import java.net.*; + +public class EchoClient { + private Socket clientSocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + + public void startConnection(String ip, int port) { + try { + clientSocket = new Socket(ip, port); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + } catch (IOException e) { + + } + + } + + public String sendMessage(String msg) { + try { + out.println(msg); + String resp = in.readLine(); + return resp; + } catch (Exception e) { + return null; + } + } + + public void stopConnection() { + try { + in.close(); + out.close(); + clientSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/sockets/src/main/java/com/baeldung/socket/EchoMultiServer.java b/sockets/src/main/java/com/baeldung/socket/EchoMultiServer.java new file mode 100644 index 0000000000..a02cf6ef3e --- /dev/null +++ b/sockets/src/main/java/com/baeldung/socket/EchoMultiServer.java @@ -0,0 +1,71 @@ +package com.baeldung.socket; + +import java.net.*; +import java.io.*; + +public class EchoMultiServer { + private ServerSocket serverSocket = null; + + public void start(int port) { + try { + serverSocket = new ServerSocket(port); + while (true) + new EchoClientHandler(serverSocket.accept()).run(); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + stop(); + } + + } + + public void stop() { + try { + + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static class EchoClientHandler extends Thread { + private Socket clientSocket; + private PrintWriter out = null; + private BufferedReader in = null; + + public EchoClientHandler(Socket socket) { + this.clientSocket = socket; + } + + public void run() { + try { + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + String inputLine; + while ((inputLine = in.readLine()) != null) { + if (".".equals(inputLine)) { + out.println("bye"); + break; + } + out.println(inputLine); + } + + in.close(); + out.close(); + clientSocket.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static void main(String[] args) { + EchoMultiServer server = new EchoMultiServer(); + server.start(5555); + } + +} diff --git a/sockets/src/main/java/com/baeldung/socket/EchoServer.java b/sockets/src/main/java/com/baeldung/socket/EchoServer.java new file mode 100644 index 0000000000..45eccb6b0d --- /dev/null +++ b/sockets/src/main/java/com/baeldung/socket/EchoServer.java @@ -0,0 +1,50 @@ +package com.baeldung.socket; + +import java.net.*; +import java.io.*; + +public class EchoServer { + private ServerSocket serverSocket = null; + private Socket clientSocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + + public void start(int port) { + try { + serverSocket = new ServerSocket(port); + clientSocket = serverSocket.accept(); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + String inputLine; + while ((inputLine = in.readLine()) != null) { + if (".".equals(inputLine)) { + out.println("good bye"); + break; + } + out.println(inputLine); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public void stop() { + try { + in.close(); + out.close(); + clientSocket.close(); + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + EchoServer server = new EchoServer(); + server.start(4444); + } + +} diff --git a/sockets/src/main/java/com/baeldung/socket/GreetClient.java b/sockets/src/main/java/com/baeldung/socket/GreetClient.java new file mode 100644 index 0000000000..a91b61d3ad --- /dev/null +++ b/sockets/src/main/java/com/baeldung/socket/GreetClient.java @@ -0,0 +1,46 @@ +package com.baeldung.socket; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; + +public class GreetClient { + private Socket clientSocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + + public void startConnection(String ip, int port) { + try { + clientSocket = new Socket(ip, port); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + } catch (IOException e) { + + } + + } + + public String sendMessage(String msg) { + try { + out.println(msg); + String resp = in.readLine(); + return resp; + } catch (Exception e) { + return null; + } + } + + public void stopConnection() { + try { + in.close(); + out.close(); + clientSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/sockets/src/main/java/com/baeldung/socket/GreetServer.java b/sockets/src/main/java/com/baeldung/socket/GreetServer.java new file mode 100644 index 0000000000..34c801aea8 --- /dev/null +++ b/sockets/src/main/java/com/baeldung/socket/GreetServer.java @@ -0,0 +1,47 @@ +package com.baeldung.socket; + +import java.net.*; +import java.io.*; + +public class GreetServer { + private ServerSocket serverSocket = null; + private Socket clientSocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + + + public void start(int port) { + try { + serverSocket = new ServerSocket(port); + clientSocket = serverSocket.accept(); + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + clientSocket.getInputStream())); + String greeting = in.readLine(); + if ("hello server".equals(greeting)) + out.println("hello client"); + else + out.println("unrecognised greetin"); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public void stop() { + try { + in.close(); + out.close(); + clientSocket.close(); + serverSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + public static void main(String[] args) { + GreetServer server=new GreetServer(); + server.start(6666); + } + +} diff --git a/sockets/src/test/java/com/baeldung/socket/EchoMultiTest.java b/sockets/src/test/java/com/baeldung/socket/EchoMultiTest.java new file mode 100644 index 0000000000..55f41a539e --- /dev/null +++ b/sockets/src/test/java/com/baeldung/socket/EchoMultiTest.java @@ -0,0 +1,44 @@ +package com.baeldung.socket; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class EchoMultiTest { + @Test + public void givenClient1_whenServerResponds_thenCorrect() { + EchoClient client1 = new EchoClient(); + client1.startConnection("127.0.0.1", 5555); + String msg1 = client1.sendMessage("hello"); + String msg2 = client1.sendMessage("world"); + String terminate = client1.sendMessage("."); + assertEquals(msg1, "hello"); + assertEquals(msg2, "world"); + assertEquals(terminate, "bye"); + } + + @Test + public void givenClient2_whenServerResponds_thenCorrect() { + EchoClient client1 = new EchoClient(); + client1.startConnection("127.0.0.1", 5555); + String msg1 = client1.sendMessage("hello"); + String msg2 = client1.sendMessage("world"); + String terminate = client1.sendMessage("."); + assertEquals(msg1, "hello"); + assertEquals(msg2, "world"); + assertEquals(terminate, "bye"); + } + + @Test + public void givenClient3_whenServerResponds_thenCorrect() { + EchoClient client1 = new EchoClient(); + client1.startConnection("127.0.0.1", 5555); + String msg1 = client1.sendMessage("hello"); + String msg2 = client1.sendMessage("world"); + String terminate = client1.sendMessage("."); + assertEquals(msg1, "hello"); + assertEquals(msg2, "world"); + assertEquals(terminate, "bye"); + } + +} diff --git a/sockets/src/test/java/com/baeldung/socket/EchoTest.java b/sockets/src/test/java/com/baeldung/socket/EchoTest.java new file mode 100644 index 0000000000..fee626bc78 --- /dev/null +++ b/sockets/src/test/java/com/baeldung/socket/EchoTest.java @@ -0,0 +1,36 @@ +package com.baeldung.socket; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class EchoTest { + EchoClient client = null; + + @Before + public void setup() { + client = new EchoClient(); + client.startConnection("127.0.0.1", 4444); + } + + @Test + public void givenClient_whenServerEchosMessage_thenCorrect() { + + String resp1 = client.sendMessage("hello"); + String resp2 = client.sendMessage("world"); + String resp3 = client.sendMessage("!"); + String resp4 = client.sendMessage("."); + assertEquals("hello", resp1); + assertEquals("world", resp2); + assertEquals("!", resp3); + assertEquals("good bye", resp4); + } + + @After + public void tearDown() { + client.stopConnection(); + } + +} diff --git a/sockets/src/test/java/com/baeldung/socket/GreetServerTest.java b/sockets/src/test/java/com/baeldung/socket/GreetServerTest.java new file mode 100644 index 0000000000..9126fe9757 --- /dev/null +++ b/sockets/src/test/java/com/baeldung/socket/GreetServerTest.java @@ -0,0 +1,16 @@ +package com.baeldung.socket; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class GreetServerTest { + @Test + public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() { + GreetClient client = new GreetClient(); + client.startConnection("127.0.0.1", 6666); + String response = client.sendMessage("hello server"); + assertEquals("hello client", response); + } + +}