Merge pull request #766 from eugenp/sockets_merged_to_core_java

sockets -> core-java
This commit is contained in:
Eugen 2016-10-23 05:52:20 -05:00 committed by GitHub
commit 7d1b3ab5ff
11 changed files with 33 additions and 55 deletions

View File

@ -18,3 +18,4 @@
- [MD5 Hashing in Java](http://www.baeldung.com/java-md5) - [MD5 Hashing in Java](http://www.baeldung.com/java-md5)
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist) - [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) - [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)

View File

@ -23,8 +23,7 @@ public class EchoClient {
public String sendMessage(String msg) { public String sendMessage(String msg) {
try { try {
out.println(msg); out.println(msg);
String resp = in.readLine(); return in.readLine();
return resp;
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }

View File

@ -26,8 +26,7 @@ public class GreetClient {
public String sendMessage(String msg) { public String sendMessage(String msg) {
try { try {
out.println(msg); out.println(msg);
String resp = in.readLine(); return in.readLine();
return resp;
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }

View File

@ -1,7 +1,6 @@
package com.baeldung.socket; package com.baeldung.socket;
import org.junit.After; import org.junit.BeforeClass;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -10,18 +9,22 @@ import static org.junit.Assert.assertEquals;
public class EchoMultiTest { public class EchoMultiTest {
{ private static final Integer PORT = 5555;
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555));
}
@BeforeClass
public static void start() throws InterruptedException {
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(PORT));
Thread.sleep(500);
}
@Test @Test
public void givenClient1_whenServerResponds_thenCorrect() { public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555); client.startConnection("127.0.0.1", PORT);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
assertEquals(msg1, "hello"); assertEquals(msg1, "hello");
assertEquals(msg2, "world"); assertEquals(msg2, "world");
assertEquals(terminate, "bye"); assertEquals(terminate, "bye");
@ -31,7 +34,7 @@ public class EchoMultiTest {
@Test @Test
public void givenClient2_whenServerResponds_thenCorrect() { public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555); client.startConnection("127.0.0.1", PORT);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -44,7 +47,7 @@ public class EchoMultiTest {
@Test @Test
public void givenClient3_whenServerResponds_thenCorrect() { public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient(); EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555); client.startConnection("127.0.0.1", PORT);
String msg1 = client.sendMessage("hello"); String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world"); String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("."); String terminate = client.sendMessage(".");
@ -53,5 +56,4 @@ public class EchoMultiTest {
assertEquals(terminate, "bye"); assertEquals(terminate, "bye");
client.stopConnection(); client.stopConnection();
} }
} }

View File

@ -2,6 +2,7 @@ package com.baeldung.socket;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -9,15 +10,19 @@ import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class EchoTest { public class EchoTest {
{ private static final Integer PORT = 4444;
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(4444));
}
EchoClient client = new EchoClient(); @BeforeClass
public static void start() throws InterruptedException {
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(PORT));
Thread.sleep(500);
}
private EchoClient client = new EchoClient();
@Before @Before
public void init() { public void init() {
client.startConnection("127.0.0.1", 4444); client.startConnection("127.0.0.1", PORT);
} }
@Test @Test
@ -37,5 +42,4 @@ public class EchoTest {
public void tearDown() { public void tearDown() {
client.stopConnection(); client.stopConnection();
} }
} }

View File

@ -2,6 +2,7 @@ package com.baeldung.socket;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -10,16 +11,20 @@ import static org.junit.Assert.assertEquals;
public class GreetServerTest { public class GreetServerTest {
GreetClient client; private GreetClient client;
{ private static final Integer PORT = 6666;
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
@BeforeClass
public static void start() throws InterruptedException {
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(PORT));
Thread.sleep(500);
} }
@Before @Before
public void init() { public void init() {
client = new GreetClient(); client = new GreetClient();
client.startConnection("127.0.0.1", 6666); client.startConnection("127.0.0.1", PORT);
} }

View File

@ -1,2 +0,0 @@
### Relevant Articles:
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)

View File

@ -1,30 +0,0 @@
<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>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>