diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..061b9c4 --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project JavaSide. + + + diff --git a/build/built-jar.properties b/build/built-jar.properties new file mode 100644 index 0000000..13680d0 --- /dev/null +++ b/build/built-jar.properties @@ -0,0 +1,4 @@ +#Fri, 10 Aug 2018 22:26:22 +0430 + + +D\:\\Projects\\Temp\\SocketInVBAndJava\\JavaSide= diff --git a/build/classes/javaside/JavaSide$1.class b/build/classes/javaside/JavaSide$1.class new file mode 100644 index 0000000..76b04de Binary files /dev/null and b/build/classes/javaside/JavaSide$1.class differ diff --git a/build/classes/javaside/JavaSide$2.class b/build/classes/javaside/JavaSide$2.class new file mode 100644 index 0000000..c479184 Binary files /dev/null and b/build/classes/javaside/JavaSide$2.class differ diff --git a/build/classes/javaside/JavaSide.class b/build/classes/javaside/JavaSide.class new file mode 100644 index 0000000..7d95468 Binary files /dev/null and b/build/classes/javaside/JavaSide.class differ diff --git a/build/classes/javaside/Socket/TcpClient$1.class b/build/classes/javaside/Socket/TcpClient$1.class new file mode 100644 index 0000000..beeb809 Binary files /dev/null and b/build/classes/javaside/Socket/TcpClient$1.class differ diff --git a/build/classes/javaside/Socket/TcpClient.class b/build/classes/javaside/Socket/TcpClient.class new file mode 100644 index 0000000..6047d5f Binary files /dev/null and b/build/classes/javaside/Socket/TcpClient.class differ diff --git a/build/classes/javaside/Socket/TcpClientEventHandler.class b/build/classes/javaside/Socket/TcpClientEventHandler.class new file mode 100644 index 0000000..032b5e8 Binary files /dev/null and b/build/classes/javaside/Socket/TcpClientEventHandler.class differ diff --git a/build/classes/javaside/Socket/TcpServer$1$1.class b/build/classes/javaside/Socket/TcpServer$1$1.class new file mode 100644 index 0000000..82c3adf Binary files /dev/null and b/build/classes/javaside/Socket/TcpServer$1$1.class differ diff --git a/build/classes/javaside/Socket/TcpServer$1.class b/build/classes/javaside/Socket/TcpServer$1.class new file mode 100644 index 0000000..872b32f Binary files /dev/null and b/build/classes/javaside/Socket/TcpServer$1.class differ diff --git a/build/classes/javaside/Socket/TcpServer.class b/build/classes/javaside/Socket/TcpServer.class new file mode 100644 index 0000000..56c7af6 Binary files /dev/null and b/build/classes/javaside/Socket/TcpServer.class differ diff --git a/build/classes/javaside/Socket/TcpServerEventHandler.class b/build/classes/javaside/Socket/TcpServerEventHandler.class new file mode 100644 index 0000000..82fa079 Binary files /dev/null and b/build/classes/javaside/Socket/TcpServerEventHandler.class differ diff --git a/dist/JavaSide.jar b/dist/JavaSide.jar new file mode 100644 index 0000000..bfc2804 Binary files /dev/null and b/dist/JavaSide.jar differ diff --git a/dist/README.TXT b/dist/README.TXT new file mode 100644 index 0000000..234dde9 --- /dev/null +++ b/dist/README.TXT @@ -0,0 +1,32 @@ +======================== +BUILD OUTPUT DESCRIPTION +======================== + +When you build an Java application project that has a main class, the IDE +automatically copies all of the JAR +files on the projects classpath to your projects dist/lib folder. The IDE +also adds each of the JAR files to the Class-Path element in the application +JAR files manifest file (MANIFEST.MF). + +To run the project from the command line, go to the dist folder and +type the following: + +java -jar "JavaSide.jar" + +To distribute this project, zip up the dist folder (including the lib folder) +and distribute the ZIP file. + +Notes: + +* If two JAR files on the project classpath have the same name, only the first +JAR file is copied to the lib folder. +* Only JAR files are copied to the lib folder. +If the classpath contains other types of files or folders, these files (folders) +are not copied. +* If a library on the projects classpath also has a Class-Path element +specified in the manifest,the content of the Class-Path element has to be on +the projects runtime path. +* To set a main class in a standard Java project, right-click the project node +in the Projects window and choose Properties. Then click Run and enter the +class name in the Main Class field. Alternatively, you can manually type the +class name in the manifest Main-Class element. diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..221411f --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..3120cfc --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=c2254689 +build.xml.script.CRC32=9fd733ac +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=c2254689 +nbproject/build-impl.xml.script.CRC32=63684ee5 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties new file mode 100644 index 0000000..bc6d4a5 --- /dev/null +++ b/nbproject/private/private.properties @@ -0,0 +1,2 @@ +compile.on.save=true +user.properties.file=C:\\Users\\Abbas\\AppData\\Roaming\\NetBeans\\8.2\\build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml new file mode 100644 index 0000000..5ce4d8e --- /dev/null +++ b/nbproject/private/private.xml @@ -0,0 +1,12 @@ + + + + + + file:/D:/Projects/Temp/SocketInVBAndJava/JavaSide/src/javaside/JavaSide.java + file:/D:/Projects/Temp/SocketInVBAndJava/JavaSide/src/javaside/Socket/TcpServerEventHandler.java + file:/D:/Projects/Temp/SocketInVBAndJava/JavaSide/src/javaside/Socket/TcpServer.java + file:/D:/Projects/Temp/SocketInVBAndJava/JavaSide/src/javaside/Socket/TcpClient.java + + + diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..f27af17 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,74 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/JavaSide.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=javaside.JavaSide +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..a9206cd --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + JavaSide + + + + + + + + + diff --git a/src/javaside/JavaSide.java b/src/javaside/JavaSide.java new file mode 100644 index 0000000..ae77768 --- /dev/null +++ b/src/javaside/JavaSide.java @@ -0,0 +1,82 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaside; + +import java.util.Scanner; +import javaside.Socket.TcpServerEventHandler; +import javaside.Socket.TcpServer; +import javaside.Socket.TcpClientEventHandler; +import javaside.Socket.TcpClient; + +/** + * + * @author Abbas + */ +public class JavaSide { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + int port = 123; + System.out.println("Please Enter port of server:"); + Scanner scanner = new Scanner(System.in); + port = Integer.parseInt(scanner.nextLine()); + + TcpServer server = new TcpServer(port); + + System.out.println("TCP server start - port:" + port); + + // add event listener + final TcpServer that_server = server; + server.addEventHandler(new TcpServerEventHandler() { + @Override + public void onMessage(int client_id, String line) { + System.out.println("* <" + client_id + "> " + line); + } + + @Override + public void onAccept(int client_id) { + System.out.println("* <" + client_id + "> connection accepted"); + that_server.setReadInterval(100 + that_server.getClients().size() * 10); + } + + @Override + public void onClose(int client_id) { + System.out.println("* <" + client_id + "> closed"); + } + }); + + // start TCP socket server + server.listen(); + + // broadcast + new Thread() { + @Override + public void run() { + while (true) { + try { + Thread.sleep(100); + Scanner scanner = new Scanner(System.in); + String line = scanner.nextLine(); + if (line != null&&!"".equals(line)) { + int data = Integer.parseInt(line); + if (that_server != null && that_server.getClients().size() > 0) { + that_server.getClients().forEach((_item) -> { + _item.send(data); + }); + } + } + } catch (InterruptedException | NumberFormatException ex) { + ex.printStackTrace(); + } + } + } + }.start(); + } + +} diff --git a/src/javaside/Socket/TcpClient.java b/src/javaside/Socket/TcpClient.java new file mode 100644 index 0000000..52f1028 --- /dev/null +++ b/src/javaside/Socket/TcpClient.java @@ -0,0 +1,136 @@ +package javaside.Socket; + +import java.io.*; +import java.net.Socket; +import java.net.SocketException; +import java.net.ConnectException; + +public class TcpClient{ + private String host; + private int port; + private Socket sock; + private int readInterval; + public int getReadInterval(){ return readInterval; } + public void setReadInterval(int msec){ this.readInterval = msec; } + private BufferedWriter bWriter; + private BufferedReader bReader; + private InputStreamReader iReader; + private TcpClientEventHandler handler; + private boolean closer = false; + + public TcpClient(String host, int port){ + this.host = host; + this.port = port; + } + + public TcpClient(Socket connected_socket){ + this.sock = connected_socket; + } + + public boolean run(){ + this.closer = false; + try{ + this.bWriter = new BufferedWriter(new OutputStreamWriter(this.sock.getOutputStream())); + this.iReader = new InputStreamReader(this.sock.getInputStream()); + this.bReader = new BufferedReader(this.iReader); + } + catch(ConnectException ex){ + if(handler != null) handler.onClose(); + return false; + } + catch(Exception ex){ + this.close(); + if(handler != null) handler.onClose(); + return false; + } + final TcpClient that = this; + new Thread(){ + public void run(){ + while(!closer){ + try{ + Thread.sleep(readInterval); + String line = bReader.readLine(); + + if(handler != null) handler.onMessage(line); + + } + catch(SocketException ex){ + that.close(); + } + catch(IOException ex){ + that.close(); + } + catch(Exception ex){ + that.close(); + } + } + } + }.start(); + if(handler != null) handler.onOpen(); + return true; + } + + public boolean connect(){ + if(this.sock != null) return false; + try{ + this.sock = new Socket(host, port); + } + catch(ConnectException ex){ + if(handler != null) handler.onClose(); + return false; + } + catch(Exception ex){ + this.close(); + if(handler != null) handler.onClose(); + return false; + } + return run(); + } + + public void close(){ + try{ + closer = true; + bReader.close(); + bWriter.close(); + iReader.close(); + sock.close(); + sock = null; + if(handler != null) handler.onClose(); + } + catch(Exception ex){ + } + } + + public boolean send(String line){ + if(sock == null) return false; + try{ + bWriter.write(line+"\n"); + bWriter.flush(); + } + catch(Exception ex){ + this.close(); + if(handler != null) handler.onClose(); + return false; + } + return true; + } + + public boolean send(int data){ + if(sock == null) return false; + try{ + bWriter.write(String.valueOf(data)+"\n"); + bWriter.flush(); + } + catch(Exception ex){ + this.close(); + if(handler != null) handler.onClose(); + return false; + } + return true; + } + + public void addEventHandler(TcpClientEventHandler handler){ + this.handler = handler; + } + +} \ No newline at end of file diff --git a/src/javaside/Socket/TcpClientEventHandler.java b/src/javaside/Socket/TcpClientEventHandler.java new file mode 100644 index 0000000..9439835 --- /dev/null +++ b/src/javaside/Socket/TcpClientEventHandler.java @@ -0,0 +1,7 @@ +package javaside.Socket; + +public interface TcpClientEventHandler{ + public void onMessage(String line); + public void onOpen(); + public void onClose(); +} diff --git a/src/javaside/Socket/TcpServer.java b/src/javaside/Socket/TcpServer.java new file mode 100644 index 0000000..eff2b59 --- /dev/null +++ b/src/javaside/Socket/TcpServer.java @@ -0,0 +1,85 @@ +package javaside.Socket; + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.ConnectException; +import java.util.ArrayList; + +public class TcpServer{ + private ServerSocket server; + private TcpServerEventHandler handler; + private ArrayList clients; + public ArrayList getClients(){ return clients; } + private boolean closer = false; + private int readInterval; + public int getReadInterval(){ return readInterval; } + public void setReadInterval(int msec){ + this.readInterval = msec; + for(TcpClient sock : clients){ + sock.setReadInterval(msec); + } + } + + public TcpServer(int port){ + if(server != null && !server.isClosed()) return; + if(clients == null) clients = new ArrayList(); + try{ + server = new ServerSocket(port); + } + catch(Exception ex){ + } + } + + public void listen(){ + new Thread(){ + public void run(){ + while(!closer){ + try{ + TcpClient sock = new TcpClient(server.accept()); + clients.add(sock); + final int cid = clients.size()-1; // client id + handler.onAccept(cid); + if(handler != null){ + sock.addEventHandler(new TcpClientEventHandler(){ + public void onMessage(String line){ + handler.onMessage(cid, line); + } + public void onOpen(){ + } + public void onClose(){ + handler.onClose(cid); + } + }); + } + sock.run(); + } + catch(Exception ex){ + } + } + } + }.start(); + } + + public TcpClient getClient(int id){ + return clients.get(id); + } + + public void close(){ + closer = true; + try{ + server.close(); + for(TcpClient sock : clients){ + sock.close(); + } + } + catch(Exception ex){ + } + } + + public void addEventHandler(TcpServerEventHandler handler){ + this.handler = handler; + } + +} \ No newline at end of file diff --git a/src/javaside/Socket/TcpServerEventHandler.java b/src/javaside/Socket/TcpServerEventHandler.java new file mode 100644 index 0000000..4acbca0 --- /dev/null +++ b/src/javaside/Socket/TcpServerEventHandler.java @@ -0,0 +1,7 @@ +package javaside.Socket; + +public interface TcpServerEventHandler{ + public void onMessage(int client_id, String line); + public void onAccept(int client_id); + public void onClose(int client_id); +} \ No newline at end of file