Commit bee6491a authored by captainwong's avatar captainwong

win/unix test parse ok

parent 7f6c9215
...@@ -12,6 +12,12 @@ packages/ ...@@ -12,6 +12,12 @@ packages/
# Dev partner files # Dev partner files
*.DPbcd *.DPbcd
# else
*.class
build/
cmake-build-debug/
# vs # vs
*.sdf *.sdf
#*.sln #*.sln
......
...@@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "csharp_dll_demo", "csharp_d ...@@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "csharp_dll_demo", "csharp_d
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bench", "bench\bench.vcxproj", "{01B919A1-174A-45E5-A2B5-B509BD66E91A}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bench", "bench\bench.vcxproj", "{01B919A1-174A-45E5-A2B5-B509BD66E91A}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ademco_java", "javademo\cpp\ademco_java.vcxproj", "{B323151B-AD89-4545-B95E-E2442A7961BB}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
...@@ -122,6 +124,20 @@ Global ...@@ -122,6 +124,20 @@ Global
{01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x64.Build.0 = Release|x64 {01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x64.Build.0 = Release|x64
{01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x86.ActiveCfg = Release|Win32 {01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x86.ActiveCfg = Release|Win32
{01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x86.Build.0 = Release|Win32 {01B919A1-174A-45E5-A2B5-B509BD66E91A}.Release|x86.Build.0 = Release|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|Any CPU.ActiveCfg = Debug|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|ARM.ActiveCfg = Debug|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|ARM64.ActiveCfg = Debug|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|x64.ActiveCfg = Debug|x64
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|x64.Build.0 = Debug|x64
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|x86.ActiveCfg = Debug|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Debug|x86.Build.0 = Debug|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|Any CPU.ActiveCfg = Release|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|ARM.ActiveCfg = Release|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|ARM64.ActiveCfg = Release|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|x64.ActiveCfg = Release|x64
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|x64.Build.0 = Release|x64
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|x86.ActiveCfg = Release|Win32
{B323151B-AD89-4545-B95E-E2442A7961BB}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
#!/bin/bash
mkdir -p build
JAVADEMO_SRC='./javademo/cpp'
JAVA_INCLUDE='/usr/lib/jvm/java-8-openjdk-amd64/include'
javac -encoding utf-8 javademo/jni/AdemcoHbLibrary.java && javah -d javademo/cpp -jni javademo.jni.AdemcoHbLibrary
g++ -std=c++11 $JAVADEMO_SRC/pch.cpp $JAVADEMO_SRC/dllmain.cpp -I$JAVADEMO_SRC -Iinclude -I$JAVA_INCLUDE -I$JAVA_INCLUDE/linux -fPIC -shared -o build/libademcojava.so
java javademo.jni.AdemcoHbLibrary
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{b323151b-ad89-4545-b95e-e2442a7961bb}</ProjectGuid>
<RootNamespace>ademcojava</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;ADEMCOJAVA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;ADEMCOJAVA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;ADEMCOJAVA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;ADEMCOJAVA_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>$(SolutionDir)include;$(JAVA_HOME)\include;$(JAVA_HOME)\include\win32;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="javademo_jni_AdemcoHbLibrary.h" />
<ClInclude Include="javademo_jni_AdemcoHbLibrary_ParseResult.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="framework.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="javademo_jni_AdemcoHbLibrary.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="javademo_jni_AdemcoHbLibrary_ParseResult.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#define DISABLE_JLIB_LOG2
#include <ademco_packet.h>
#include "javademo_jni_AdemcoHbLibrary.h"
#ifdef _WIN32
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif
JNIEXPORT void JNICALL Java_javademo_jni_AdemcoHbLibrary_nativePrint
(JNIEnv*, jobject)
{
printf("native print\n");
}
JNIEXPORT jobject JNICALL Java_javademo_jni_AdemcoHbLibrary_nativeParse
(JNIEnv* env, jclass clazz, jstring pack, jint pack_len)
{
ademco::AdemcoPacket ap;
size_t commited = 0;
jboolean iscopy = 0;
std::string s = env->GetStringUTFChars(pack, &iscopy);
auto result = ap.parse(s.data(), static_cast<size_t>(pack_len), commited);
//*cb_commited = static_cast<int>(commited);
//return static_cast<int>(result);
// Attempt to find the Version class.
clazz = env->FindClass("javademo/jni/AdemcoHbLibrary$ParseResult");
// If this class does not exist then return null.
if (clazz == 0)
return 0;
// Allocate memory for a new Version class object. Do not bother calling
// the constructor (the default constructor does nothing).
jobject obj = env->AllocObject(clazz);
// Attempt to find the major field.
jfieldID fid = env->GetFieldID(clazz, "result", "I");
// If this field does not exist then return null.
if (fid == 0)
return 0;
// Set the major field to the operating system's major version.
env->SetIntField(obj, fid, static_cast<int>(result));
// Attempt to find the minor field.
fid = env->GetFieldID(clazz, "cb_commited", "I");
// If this field does not exist then return null.
if (fid == 0)
return 0;
// Set the minor field to the operating system's minor version.
env->SetIntField(obj, fid, static_cast<int>(commited));
return obj;
}
#pragma once
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class javademo_jni_AdemcoHbLibrary */
#ifndef _Included_javademo_jni_AdemcoHbLibrary
#define _Included_javademo_jni_AdemcoHbLibrary
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: javademo_jni_AdemcoHbLibrary
* Method: nativePrint
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_javademo_jni_AdemcoHbLibrary_nativePrint
(JNIEnv *, jobject);
/*
* Class: javademo_jni_AdemcoHbLibrary
* Method: nativeParse
* Signature: (Ljava/lang/String;I)Ljavademo/jni/AdemcoHbLibrary/ParseResult;
*/
JNIEXPORT jobject JNICALL Java_javademo_jni_AdemcoHbLibrary_nativeParse
(JNIEnv *, jclass, jstring, jint);
#ifdef __cplusplus
}
#endif
#endif
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class javademo_jni_AdemcoHbLibrary_ParseResult */
#ifndef _Included_javademo_jni_AdemcoHbLibrary_ParseResult
#define _Included_javademo_jni_AdemcoHbLibrary_ParseResult
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
// pch.h: This is a precompiled header file.
// Files listed below are compiled only once, improving build performance for future builds.
// This also affects IntelliSense performance, including code completion and many code browsing features.
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
// Do not add files here that you will be updating frequently as this negates the performance advantage.
#ifndef PCH_H
#define PCH_H
// add headers that you want to pre-compile here
#include "framework.h"
#endif //PCH_H
package javademo.jni;
import java.nio.file.FileSystems;
public class AdemcoHbLibrary
{
static
{
if (System.getProperty("os.name").startsWith("Windows")) { // Windows
System.load(
FileSystems.getDefault()
.getPath("./x64/Release/ademco_java.dll") // Dynamic link
.normalize().toAbsolutePath().toString());
}else{ // Unix based
System.load(
FileSystems.getDefault()
.getPath("./build/libademcojava.so") // Dynamic link
.normalize().toAbsolutePath().toString());
}
}
private native void nativePrint();
public class ParseResult {
// 0 解析到正确、完整的一包;
// 1 解析到正确、不完整的一包,需要缓存数据,下次继续处理;
// 2 解析到错误的数据,需要清空缓冲区
public int result;
// 可以正确解析的包长(单次函数调用最多解析一包)
public int cb_commited;
}
/*
* @brief 解析网络数据
* @param[in] pack 网络数据流
* @param[in] pack_len 网络数据流有效长度
* @return ParseResult
*/
private static native ParseResult nativeParse(String pack, int pack_len);
public void testPprint(){ nativePrint(); }
public ParseResult parse(String pack, int pack_len){return nativeParse(pack, pack_len);}
public static void main(String[] args){
AdemcoHbLibrary lib = new AdemcoHbLibrary();
lib.testPprint();
String data = "\nC5C30053\"HENG-BO\"0000R000000L000000#90219125916578[#000000|1737 00 000]_09:11:19,08-05-2019\r";
ParseResult res = lib.parse(data, data.length());
assert(res.result == 0);
assert(res.cb_commited == data.length());
System.out.println("parse result=" + res.result + ",cb_commited=" + res.cb_commited);
}
/*
* @brief 将远程控制命令打包成网络传输数据
* @param[in|out] buff 缓冲区
* @param[in] buff_len 缓冲区长度
* @param[in] seq 序号
* @param[in] ademco_id 安定宝ID
* @param[in] ademco_event 安定宝事件码
* @param[in] zone 防区号
* @param[in] gg 分防区号
* @return 大于0 成功,返回值代表包长;0 buff空间不足
*/
//DLL_FUNC int pack(char* buff, int buff_len, int seq, int ademco_id, int ademco_event, int zone, int gg);
/*
* @brief 打包ACK
* @param[in|out] buff 缓冲区
* @param[in] buff_len 缓冲区长度
* @param[in] seq 序号
* @param[in] ademco_id 安定宝ID
* @return 大于0 成功,返回值代表包长;0 buff空间不足
*/
//DLL_FUNC int pack_ack(char* buff, int buff_len, int seq, int ademco_id);
}
package javademo.jni;
import java.io.*;
import java.net.*;
public class SimpleServer {
public static void main(String[] args) {
int port = 12345;
if (args.length > 0){
port = Integer.parseInt(args[0]);
}
// try (ServerSocket serverSocket = new ServerSocket(port)) {
// System.out.println("Server is listening on port " + port);
// while (true) {
// Socket socket = serverSocket.accept();
// System.out.println("New client connected");
// new SimpleServerThread(socket).start();
// }
// } catch (IOException ex) {
// System.out.println("Server exception: " + ex.getMessage());
// ex.printStackTrace();
// }
}
}
\ No newline at end of file
package javademo.jni;
import java.io.*;
import java.net.*;
public class SimpleServerThread extends Thread {
private Socket socket;
public SimpleServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String text;
do {
text = reader.readLine();
System.out.println(text);
//String reverseText = new StringBuilder(text).reverse().toString();
// writer.println("Server: " + reverseText);
} while (!text.equals("bye"));
socket.close();
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment