Merge pull request #628 from egimaben/master

added project code for sockets
This commit is contained in:
Alex Theedom 2016-08-23 20:06:14 +01:00 committed by GitHub
commit 50afba45f7
9 changed files with 383 additions and 0 deletions

30
sockets/pom.xml Normal file
View File

@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>sockets</artifactId>
<version>1.0</version>
<name>sockets</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}