Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,100 @@
# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

=======
.idea
Binary file not shown.
2 changes: 0 additions & 2 deletions src/module-info.java

This file was deleted.

6 changes: 5 additions & 1 deletion src/net/src/io/ListWritable.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void read(DataInput in) throws IOException {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("read complete: " + buff.toString());
}

@Override
Expand All @@ -51,4 +50,9 @@ public void write(DataOutput out) throws IOException {
out.writeInt(num);
}
}

@Override
public String toString() {
return buff.toString();
}
}
52 changes: 29 additions & 23 deletions src/net/src/io/Test.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
package net.src.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import net.src.socket.SimpleSocketClient;
import net.src.socket.SimpleSocketServer;

public class Test {

public static byte[] serialize(Writable w) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutput output = new DataOutputStream(out);

public static void main(String[] args) {
try {
w.write(output);
} catch (IOException e) {
socketServerTest();
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}

public static void deserialize(byte[] bytes, Writable obj) {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
try {
obj.read(in);
} catch (IOException e) {
e.printStackTrace();
}
public static void socketServerTest() throws IOException {
SimpleSocketServer server = new SimpleSocketServer(8888);
SimpleSocketClient client = new SimpleSocketClient("localhost", 8888);

/*
mode: "text" or "writable"
*/
String serverMode = "writable";

new Thread(() -> {
try {
server.start(serverMode);
} catch (IOException e) {
e.printStackTrace();
}
}).start();

}

public static void main(String[] args) {

public static void serializationTest() {
ListWritable listWritable = new ListWritable();
listWritable.add(1);
listWritable.add(2);
listWritable.add(3);
listWritable.add(999);
listWritable.add(4);

byte[] bytes = serialize(listWritable);
byte[] bytes = Utils.serialize(listWritable);
ListWritable l = new ListWritable();

deserialize(bytes, l);
Utils.deserialize(bytes, l);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
}

}
30 changes: 30 additions & 0 deletions src/net/src/io/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.src.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;

public class Utils {
public static byte[] serialize(Writable w) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutput output = new DataOutputStream(out);
try {
w.write(output);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}

public static void deserialize(byte[] bytes, Writable obj) {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
try {
obj.read(in);
} catch (IOException e) {
e.printStackTrace();
}
}
}
42 changes: 42 additions & 0 deletions src/net/src/socket/ClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.src.socket;

import java.io.IOException;
import java.util.Scanner;
import net.src.io.ListWritable;
import net.src.io.Utils;

public class ClientTest {
private static SimpleSocketClient client;
public static void main(String[] args) {
client = new SimpleSocketClient("localhost", 8888);
try {
client.connectToServer();
// sendTextFromConsole(client);
sendWritable();
} catch (IOException e) {
e.printStackTrace();
}

}

private static void sendTextFromConsole() throws IOException {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.print("type your text: ");
String message = sc.nextLine();
client.sendTextMessage(message);
client.receiveMessage();
}
}

private static void sendWritable() throws IOException {
ListWritable w = new ListWritable();
w.add(4);
w.add(5);
w.add(6);
client.sendByteArray(Utils.serialize(w));
client.sendByteArray(Utils.serialize(w));
client.sendByteArray(Utils.serialize(w));
client.receiveMessage();
}
}
61 changes: 61 additions & 0 deletions src/net/src/socket/SimpleSocketClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.src.socket;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SimpleSocketClient {
private String hostName;
private Integer port;
private Socket socket;
private InputStream input;
private OutputStream output;
private boolean isReading;

public SimpleSocketClient(String hostname, Integer port) {
this.hostName = hostname;
this.port = port;
}

public boolean connectToServer() throws IOException {
System.out.println("Connecting to Server " + hostName + ":" + port);
socket = new Socket(hostName, port);
output = socket.getOutputStream();
input = socket.getInputStream();
isReading = true;
new Thread(() -> {
}).start();

return socket.isConnected();
}

public void sendByteArray(byte[] bytes) throws IOException {
DataOutputStream out = new DataOutputStream(output);
out.write(bytes);
out.flush();
}

public void disconnect() throws IOException {
output.close();
input.close();
socket.close();
}

public void receiveMessage() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String message = reader.readLine();
System.out.println("Server: " + message);
}

public void sendTextMessage(String message) {
System.out.println("sending Message: " + message);
PrintWriter writer = new PrintWriter(output, true);
writer.println(message);
writer.flush();
}
}
Loading