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)
- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist)
- [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) {
try {
out.println(msg);
String resp = in.readLine();
return resp;
return in.readLine();
} catch (Exception e) {
return null;
}

View File

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

View File

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

View File

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

View File

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