Sockets moved to core-java

This commit is contained in:
Grzegorz Piwowarek
2016-10-23 11:25:09 +02:00
parent bfddf3344d
commit 4670b8dbda
11 changed files with 7 additions and 38 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

@@ -0,0 +1,43 @@
package com.baeldung.socket;
import java.io.*;
import java.net.*;
public class EchoClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
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) {
System.out.print(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;
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;
private BufferedReader in;
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;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
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;
private PrintWriter out;
private BufferedReader in;
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;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
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 greeting");
} 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,57 @@
package com.baeldung.socket;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoMultiTest {
{
Executors.newSingleThreadExecutor().submit(() -> new EchoMultiServer().start(5555));
}
@Test
public void givenClient1_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");
client.stopConnection();
}
@Test
public void givenClient2_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");
client.stopConnection();
}
@Test
public void givenClient3_whenServerResponds_thenCorrect() {
EchoClient client = new EchoClient();
client.startConnection("127.0.0.1", 5555);
String msg1 = client.sendMessage("hello");
String msg2 = client.sendMessage("world");
String terminate = client.sendMessage("");
assertEquals(msg1, "hello");
assertEquals(msg2, "world");
assertEquals(terminate, "bye");
client.stopConnection();
}
}

View File

@@ -0,0 +1,41 @@
package com.baeldung.socket;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class EchoTest {
{
Executors.newSingleThreadExecutor().submit(() -> new EchoServer().start(4444));
}
EchoClient client = new EchoClient();
@Before
public void init() {
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,36 @@
package com.baeldung.socket;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
public class GreetServerTest {
GreetClient client;
{
Executors.newSingleThreadExecutor().submit(() -> new GreetServer().start(6666));
}
@Before
public void init() {
client = new GreetClient();
client.startConnection("127.0.0.1", 6666);
}
@Test
public void givenGreetingClient_whenServerRespondsWhenStarted_thenCorrect() {
String response = client.sendMessage("hello server");
assertEquals("hello client", response);
}
@After
public void finish() {
client.stopConnection();
}
}