diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..85e7c1d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.idea/
diff --git a/.idea/modules.xml b/.idea/modules.xml
index fbd90b4..71b387f 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -2,7 +2,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f70f571..5ff8c17 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,51 +1,19 @@
+
+
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
@@ -112,6 +101,9 @@
+
+
+
@@ -228,70 +220,26 @@
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -327,21 +275,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -404,25 +337,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -472,22 +386,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -497,174 +395,72 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
project
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -682,6 +478,14 @@
+
+ 1672632258940
+
+
+
+ 1672632258940
+
+
@@ -722,15 +526,31 @@
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
-
-
-
+
+
+
diff --git a/JSocketServer.iml b/JSocketServer.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/JSocketServer.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b7a7ea0
--- /dev/null
+++ b/README.md
@@ -0,0 +1,12 @@
+# JSocketServer
+
+written in pure Java, a socket/websocket server
+- it can listen, receive and send replies
+- socket version is using native Java serialization to transfer binary objects between sockets
+- websocket is sending casted to string binary frame
+- tested under heavy traffic
+
+Java socket server. Currently Connection plugin done for handling WebSocket connection.
+WebSocket server is able to receive, read and send handshake also read/send data from/to client.
+
+some code snippets refactored from stackoverflow and mozilla.org
diff --git a/bin/production/JSocketServer/config/server.xml b/bin/production/JSocketServer/config/server.xml
new file mode 100644
index 0000000..03b77b8
--- /dev/null
+++ b/bin/production/JSocketServer/config/server.xml
@@ -0,0 +1,4 @@
+
+
+ 6050
+
diff --git a/src/server/config/ServerConfig.java b/src/server/config/ServerConfig.java
index f934110..98eb19e 100644
--- a/src/server/config/ServerConfig.java
+++ b/src/server/config/ServerConfig.java
@@ -1,77 +1,79 @@
-package server.config;
-
-
-import server.utils.FileUtils;
-
-import java.io.File;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-
-
-public class ServerConfig {
- private Map parameters = new HashMap();
-
- public ServerConfig(String file) {
- read(file);
- }
-
- protected void read(String file) {
- try {
- String rootDir = null;
- try {
- rootDir =
- getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ");
- rootDir = new File(rootDir).getParent();
- } catch (Exception e) {
- //throw e;
- }
-
- file = rootDir + FileUtils.FILE_SEPARATOR + file;
-
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- Document doc = docBuilder.parse(new File(file));
- doc.getDocumentElement().normalize();
-
- NodeList childNodes = doc.getChildNodes();
- parameters.clear();
- for (int s = 0; s < childNodes.getLength(); s++) {
-
- Node item = childNodes.item(s);
- if (item.getNodeType() == Node.ELEMENT_NODE) {
- Element portElement = (Element)item;
-
- NodeList tagName = portElement.getElementsByTagName("port");
- parameters.put("port", tagName.item(0).getChildNodes().item(0).getNodeValue().trim());
- }
- }
-
- } catch (SAXParseException err) {
- System.out.println("parsing error");
- } catch (ParserConfigurationException e) {
- System.out.println("configuration error");
- } catch (SAXException e) {
- System.out.println("general sax parser error");
- } catch (IOException e) {
- System.out.println("config file error");
- }
- }
-
- public String get(String key) {
- return parameters.get(key);
- }
-}
+package server.config;
+
+
+import server.utils.FileUtils;
+
+import java.io.File;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+
+public class ServerConfig {
+ private Map parameters = new HashMap();
+
+ public ServerConfig(String file) {
+ read(file);
+ }
+
+ protected void read(String file) {
+ try {
+ String rootDir = null;
+ try {
+ rootDir =
+ getClass().getProtectionDomain().getCodeSource().getLocation().getPath().replace("%20", " ");
+ System.out.println(rootDir);
+// rootDir = new File(rootDir).getParent();
+// System.out.println(rootDir);
+ } catch (Exception e) {
+ //throw e;
+ }
+
+ file = rootDir + FileUtils.FILE_SEPARATOR + file;
+
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+ Document doc = docBuilder.parse(new File(file));
+ doc.getDocumentElement().normalize();
+
+ NodeList childNodes = doc.getChildNodes();
+ parameters.clear();
+ for (int s = 0; s < childNodes.getLength(); s++) {
+
+ Node item = childNodes.item(s);
+ if (item.getNodeType() == Node.ELEMENT_NODE) {
+ Element portElement = (Element)item;
+
+ NodeList tagName = portElement.getElementsByTagName("port");
+ parameters.put("port", tagName.item(0).getChildNodes().item(0).getNodeValue().trim());
+ }
+ }
+
+ } catch (SAXParseException err) {
+ System.out.println("parsing error");
+ } catch (ParserConfigurationException e) {
+ System.out.println("configuration error");
+ } catch (SAXException e) {
+ System.out.println("general sax parser error");
+ } catch (IOException e) {
+ System.out.println("config file error");
+ }
+ }
+
+ public String get(String key) {
+ return parameters.get(key);
+ }
+}
diff --git a/src/server/core/Server.java b/src/server/core/Server.java
index b106017..2fb5ef1 100644
--- a/src/server/core/Server.java
+++ b/src/server/core/Server.java
@@ -24,7 +24,7 @@ public class Server extends Thread {
private static ServerConfig config;
private Socket client;
- private Connection conn;
+ private WebSocketConnection conn;
public Server() {
try {
@@ -57,9 +57,12 @@ public void run() {
while (true) {
try {
+ client = serverSocket.accept(); //give it for socket
sleep(1);
} catch (InterruptedException e) {
System.err.println("sleep failed");
+ } catch (IOException e) {
+ System.err.println("cant accept socket");
}
}
}
diff --git a/src/server/core/WebSocketConnection.java b/src/server/core/WebSocketConnection.java
index 971549f..c775961 100644
--- a/src/server/core/WebSocketConnection.java
+++ b/src/server/core/WebSocketConnection.java
@@ -16,7 +16,7 @@
/**
* @author andrzej.salamon@gmail.com
*/
-class WebSocketConnection extends Thread implements Connection {
+class WebSocketConnection extends Thread {
ObjectOutputStream out;
ObjectInputStream in;
@@ -35,7 +35,6 @@ public WebSocketConnection(ServerSocket serverSocket) {
this.start();
}
- @Override
public void handleStream() {
try {
client = serverSocket.accept();
@@ -51,7 +50,21 @@ public void handleStream() {
}
}
}
-
+ public void handleStream(Socket client) {
+ try {
+ this.client = client;
+ out = new ObjectOutputStream(client.getOutputStream());
+ in = new ObjectInputStream(client.getInputStream());
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try { //try to close gracefully
+ client.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
public void run() {
while (true) {
handleStream();
@@ -94,7 +107,6 @@ public void run() {
}
}
- @Override
public void sendHandshake() throws NoSuchAlgorithmException, IOException {
byte[] response;
response = ("HTTP/1.1 101 Switching Protocols\r\n"
@@ -112,27 +124,23 @@ public void sendHandshake() throws NoSuchAlgorithmException, IOException {
out.write(response, 0, response.length);
}
- @Override
public boolean isHandshake(String data) {
Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data);
secWebSocketKey = match.group(1);
return match.find();
}
- @Override
public boolean isGet(String data) {
Matcher get = Pattern.compile("^GET").matcher(data);
return get.find();
}
- @Override
public String getRequestAsString() {
return new Scanner(in, "UTF-8").useDelimiter("\\r\\n\\r\\n").next();
}
- @Override
public String read() throws IOException {
- byte[] buffer = new byte[MAX_BUFFER];
+ byte[] buffer = new byte[5000];
int len;
int messLen;
byte rLength = 0;
@@ -172,7 +180,6 @@ public String read() throws IOException {
return new String(message);
}
- @Override
public void brodcast(String data) throws IOException{
byte[] rawData = data.getBytes();