[BAEL-11401] - Moved articles out of core-java (part 2)
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.heapdump;
|
||||
|
||||
import com.sun.management.HotSpotDiagnosticMXBean;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class HeapDump {
|
||||
|
||||
public static void dumpHeap(String filePath, boolean live) throws IOException {
|
||||
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(
|
||||
server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);
|
||||
mxBean.dumpHeap(filePath, live);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String file = Paths.get("dump.hprof").toFile().getPath();
|
||||
|
||||
dumpHeap(file, true);
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.baeldung.http;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class FullResponseBuilder {
|
||||
public static String getFullResponse(HttpURLConnection con) throws IOException {
|
||||
StringBuilder fullResponseBuilder = new StringBuilder();
|
||||
|
||||
fullResponseBuilder.append(con.getResponseCode())
|
||||
.append(" ")
|
||||
.append(con.getResponseMessage())
|
||||
.append("\n");
|
||||
|
||||
con.getHeaderFields()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getKey() != null)
|
||||
.forEach(entry -> {
|
||||
|
||||
fullResponseBuilder.append(entry.getKey())
|
||||
.append(": ");
|
||||
|
||||
List<String> headerValues = entry.getValue();
|
||||
Iterator<String> it = headerValues.iterator();
|
||||
if (it.hasNext()) {
|
||||
fullResponseBuilder.append(it.next());
|
||||
|
||||
while (it.hasNext()) {
|
||||
fullResponseBuilder.append(", ")
|
||||
.append(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
fullResponseBuilder.append("\n");
|
||||
});
|
||||
|
||||
Reader streamReader = null;
|
||||
|
||||
if (con.getResponseCode() > 299) {
|
||||
streamReader = new InputStreamReader(con.getErrorStream());
|
||||
} else {
|
||||
streamReader = new InputStreamReader(con.getInputStream());
|
||||
}
|
||||
|
||||
BufferedReader in = new BufferedReader(streamReader);
|
||||
String inputLine;
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
fullResponseBuilder.append("Response: ")
|
||||
.append(content);
|
||||
|
||||
return fullResponseBuilder.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.http;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
public class ParameterStringBuilder {
|
||||
public static String getParamsString(Map<String, String> params) throws UnsupportedEncodingException {
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
|
||||
result.append("=");
|
||||
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
||||
result.append("&");
|
||||
}
|
||||
|
||||
String resultString = result.toString();
|
||||
return resultString.length() > 0 ? resultString.substring(0, resultString.length() - 1) : resultString;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Game implements GameMBean {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Game.class);
|
||||
|
||||
private String playerName;
|
||||
|
||||
@Override
|
||||
public void playFootball(String clubName) {
|
||||
LOG.debug(this.playerName + " playing football for " + clubName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPlayerName() {
|
||||
LOG.debug("Return playerName " + this.playerName);
|
||||
return playerName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPlayerName(String playerName) {
|
||||
LOG.debug("Set playerName to value " + playerName);
|
||||
this.playerName = playerName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
public interface GameMBean {
|
||||
|
||||
public void playFootball(String clubName);
|
||||
|
||||
public String getPlayerName();
|
||||
|
||||
public void setPlayerName(String playerName);
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.jmx;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.management.*;
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class JMXTutorialMainlauncher {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(JMXTutorialMainlauncher.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
LOG.debug("This is basic JMX tutorial");
|
||||
ObjectName objectName = null;
|
||||
try {
|
||||
objectName = new ObjectName("com.baeldung.tutorial:type=basic,name=game");
|
||||
} catch (MalformedObjectNameException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||
Game gameObj = new Game();
|
||||
try {
|
||||
server.registerMBean(gameObj, objectName);
|
||||
} catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
LOG.debug("Registration for Game mbean with the platform server is successfull");
|
||||
LOG.debug("Please open jconsole to access Game mbean");
|
||||
while (true) {
|
||||
// to ensure application does not terminate
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.memoryleaks.equalshashcode;
|
||||
|
||||
public class Person {
|
||||
public String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.memoryleaks.equalshashcode;
|
||||
|
||||
public class PersonOptimized {
|
||||
public String name;
|
||||
|
||||
public PersonOptimized(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof PersonOptimized)) {
|
||||
return false;
|
||||
}
|
||||
PersonOptimized person = (PersonOptimized) o;
|
||||
return person.name.equals(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.memoryleaks.finalize;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObject {
|
||||
private String data[];
|
||||
|
||||
public BulkyObject() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finalize() {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Finalizer called");
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.memoryleaks.finalize;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObjectOptimized {
|
||||
private String data[];
|
||||
|
||||
public BulkyObjectOptimized() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Random;
|
||||
|
||||
public class BulkyObject {
|
||||
private String data[];
|
||||
|
||||
public BulkyObject() {
|
||||
data = new String[1000000];
|
||||
|
||||
for(int i=0; i<1000000; i++) {
|
||||
data[i] = getRandomString();
|
||||
}
|
||||
}
|
||||
|
||||
private String getRandomString() {
|
||||
byte[] array = new byte[1000];
|
||||
new Random().nextBytes(array);
|
||||
return new String(array, Charset.forName("UTF-8"));
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
public class InnerClassDriver {
|
||||
public static InnerClassWrapper.SimpleInnerClass getSimpleInnerClassObj() {
|
||||
return new InnerClassWrapper().new SimpleInnerClass();
|
||||
}
|
||||
|
||||
public static void main2(String[] args) {
|
||||
InnerClassWrapper.SimpleInnerClass simpleInnerClassObj = getSimpleInnerClassObj();
|
||||
System.out.println("Debug point");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
StaticNestedClassWrapper.StaticNestedClass simpleInnerClassObj = new StaticNestedClassWrapper.StaticNestedClass();
|
||||
System.out.println("Debug point");
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
|
||||
public class InnerClassWrapper {
|
||||
private BulkyObject bulkyObject = new BulkyObject();
|
||||
|
||||
public class SimpleInnerClass {
|
||||
|
||||
}
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.memoryleaks.innerclass;
|
||||
|
||||
|
||||
public class StaticNestedClassWrapper {
|
||||
private BulkyObject bulkyObject = new BulkyObject();
|
||||
|
||||
public static class StaticNestedClass {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
public class InternedString {
|
||||
private static final String FILEPATH = "C:\\bigstring.txt";
|
||||
|
||||
public void readString() {
|
||||
String s1 = ReadStringFromFileUtil.read(FILEPATH).intern();
|
||||
String s2 = ReadStringFromFileUtil.read(FILEPATH).intern();
|
||||
|
||||
if (s1 == s2) {
|
||||
System.out.println("Both the strings objects are same");
|
||||
}
|
||||
else {
|
||||
System.out.println("Both the strings objects are different");
|
||||
}
|
||||
}
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReadStringFromFileUtil {
|
||||
|
||||
public static String read(String fileName) {
|
||||
BufferedReader br = null;
|
||||
try {
|
||||
br = new BufferedReader(new FileReader(fileName));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line = br.readLine();
|
||||
|
||||
while (line != null) {
|
||||
sb.append(line);
|
||||
sb.append("\n");
|
||||
line = br.readLine();
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.memoryleaks.internedstrings;
|
||||
|
||||
public class StringObject {
|
||||
private static final String FILEPATH = "C:\\bigstring.txt";
|
||||
|
||||
public void readString() {
|
||||
String s1 = ReadStringFromFileUtil.read(FILEPATH);
|
||||
String s2 = ReadStringFromFileUtil.read(FILEPATH);
|
||||
|
||||
if (s1 == s2) {
|
||||
System.out.println("Both the strings objects are same");
|
||||
}
|
||||
else {
|
||||
System.out.println("Both the strings objects are different");
|
||||
}
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NonStaticFieldsDemo {
|
||||
public List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Debug Point 1");
|
||||
new NonStaticFieldsDemo().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.memoryleaks.staticfields;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StaticFieldsDemo {
|
||||
public static List<Double> list = new ArrayList<>();
|
||||
|
||||
public void populateList() {
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
list.add(Math.random());
|
||||
}
|
||||
System.out.println("Debug Point 2");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Debug Point 1");
|
||||
new StaticFieldsDemo().populateList();
|
||||
System.out.println("Debug Point 3");
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.outofmemoryerror;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class OutOfMemoryGCLimitExceed {
|
||||
public static void addRandomDataToMap() {
|
||||
Map<Integer, String> dataMap = new HashMap<>();
|
||||
Random r = new Random();
|
||||
while (true) {
|
||||
dataMap.put(r.nextInt(), String.valueOf(r.nextInt()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
OutOfMemoryGCLimitExceed.addRandomDataToMap();
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class EchoClient {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EchoClient.class);
|
||||
|
||||
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) {
|
||||
LOG.debug("Error when initializing connection", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String sendMessage(String msg) {
|
||||
try {
|
||||
out.println(msg);
|
||||
return in.readLine();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopConnection() {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
} catch (IOException e) {
|
||||
LOG.debug("error when closing", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class EchoMultiServer {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EchoMultiServer.class);
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
public void start(int port) {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
while (true)
|
||||
new EchoClientHandler(serverSocket.accept()).start();
|
||||
|
||||
} 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) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EchoMultiServer server = new EchoMultiServer();
|
||||
server.start(5555);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class EchoServer {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EchoServer.class);
|
||||
|
||||
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) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EchoServer server = new EchoServer();
|
||||
server.start(4444);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class GreetClient {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EchoMultiServer.class);
|
||||
|
||||
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) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String sendMessage(String msg) {
|
||||
try {
|
||||
out.println(msg);
|
||||
return in.readLine();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopConnection() {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
} catch (IOException e) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.baeldung.socket;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class GreetServer {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GreetServer.class);
|
||||
|
||||
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) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
LOG.debug(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GreetServer server = new GreetServer();
|
||||
server.start(6666);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user