From 167ed91542b8359e199bdb33d5284e79afff050b Mon Sep 17 00:00:00 2001 From: Asilbek199816 Date: Wed, 7 Aug 2019 15:19:54 +0500 Subject: [PATCH] init --- .gitignore | 15 + app/.gitignore | 1 + app/build.gradle | 35 +++ app/proguard-rules.pro | 21 ++ .../clientserver/ExampleInstrumentedTest.kt | 24 ++ app/src/main/AndroidManifest.xml | 28 ++ .../main/java/uz/anor/clientserver/Client.kt | 260 ++++++++++++++++ .../uz/anor/clientserver/ClientActivity.java | 277 ++++++++++++++++++ .../java/uz/anor/clientserver/MainActivity.kt | 24 ++ .../main/java/uz/anor/clientserver/Server.kt | 275 +++++++++++++++++ .../uz/anor/clientserver/ServerActivity.java | 265 +++++++++++++++++ .../clientserver/client/ClientActivity.java | 146 +++++++++ .../clientserver/server/ServerActivity.java | 169 +++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 34 +++ .../res/drawable/ic_launcher_background.xml | 74 +++++ app/src/main/res/layout/ac_client.xml | 49 ++++ app/src/main/res/layout/ac_ser.xml | 53 ++++ app/src/main/res/layout/activity_client.xml | 57 ++++ app/src/main/res/layout/activity_main.xml | 19 ++ app/src/main/res/layout/activity_server.xml | 54 ++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes app/src/main/res/values/colors.xml | 7 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + .../uz/anor/clientserver/ExampleUnitTest.kt | 17 ++ build.gradle | 28 ++ gradle.properties | 21 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 +++++++++++ gradlew.bat | 84 ++++++ settings.gradle | 1 + sockettransfermessagebywifi/.gitignore | 1 + sockettransfermessagebywifi/build.gradle | 37 +++ .../proguard-rules.pro | 21 ++ .../ExampleInstrumentedTest.java | 27 ++ .../src/main/AndroidManifest.xml | 2 + .../sockettransfermessagebywifi/Client.kt | 207 +++++++++++++ .../sockettransfermessagebywifi/Server.kt | 218 ++++++++++++++ .../src/main/res/values/strings.xml | 3 + .../ExampleUnitTest.java | 17 ++ 52 files changed, 2773 insertions(+) create mode 100644 .gitignore create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/uz/anor/clientserver/ExampleInstrumentedTest.kt create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/uz/anor/clientserver/Client.kt create mode 100644 app/src/main/java/uz/anor/clientserver/ClientActivity.java create mode 100644 app/src/main/java/uz/anor/clientserver/MainActivity.kt create mode 100644 app/src/main/java/uz/anor/clientserver/Server.kt create mode 100644 app/src/main/java/uz/anor/clientserver/ServerActivity.java create mode 100644 app/src/main/java/uz/anor/clientserver/client/ClientActivity.java create mode 100644 app/src/main/java/uz/anor/clientserver/server/ServerActivity.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/ac_client.xml create mode 100644 app/src/main/res/layout/ac_ser.xml create mode 100644 app/src/main/res/layout/activity_client.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/activity_server.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/uz/anor/clientserver/ExampleUnitTest.kt create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 sockettransfermessagebywifi/.gitignore create mode 100644 sockettransfermessagebywifi/build.gradle create mode 100644 sockettransfermessagebywifi/proguard-rules.pro create mode 100644 sockettransfermessagebywifi/src/androidTest/java/uz/anor/sockettransfermessagebywifi/ExampleInstrumentedTest.java create mode 100644 sockettransfermessagebywifi/src/main/AndroidManifest.xml create mode 100644 sockettransfermessagebywifi/src/main/java/uz/anor/sockettransfermessagebywifi/Client.kt create mode 100644 sockettransfermessagebywifi/src/main/java/uz/anor/sockettransfermessagebywifi/Server.kt create mode 100644 sockettransfermessagebywifi/src/main/res/values/strings.xml create mode 100644 sockettransfermessagebywifi/src/test/java/uz/anor/sockettransfermessagebywifi/ExampleUnitTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b34e7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild + diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..471f377 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,35 @@ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 29 + buildToolsVersion "29.0.0" + defaultConfig { + applicationId "uz.anor.clientserver" + minSdkVersion 21 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.core:core-ktx:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/uz/anor/clientserver/ExampleInstrumentedTest.kt b/app/src/androidTest/java/uz/anor/clientserver/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..64784c9 --- /dev/null +++ b/app/src/androidTest/java/uz/anor/clientserver/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package uz.anor.clientserver + +import androidx.test.InstrumentationRegistry +import androidx.test.runner.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + assertEquals("uz.anor.clientserver", appContext.packageName) + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..4967b41 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/uz/anor/clientserver/Client.kt b/app/src/main/java/uz/anor/clientserver/Client.kt new file mode 100644 index 0000000..ab42cbd --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/Client.kt @@ -0,0 +1,260 @@ +package uz.anor.clientserver + +import android.content.Context +import android.graphics.Color +import android.net.nsd.NsdManager +import android.net.nsd.NsdServiceInfo +import android.os.Bundle +import android.os.Handler +import android.util.Log +import android.view.View +import android.widget.EditText +import android.widget.LinearLayout +import android.widget.TextView +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import java.io.* +import java.net.InetAddress +import java.net.Socket +import java.net.UnknownHostException +import java.text.SimpleDateFormat +import java.util.* + +class Client : AppCompatActivity(), View.OnClickListener { + private val TAG = "MRX" + private var clientThread: ClientThread? = null + private var thread: Thread? = null + private var handler: Handler? = null + + + private val SERVICE_NAME = "Client Device" + private val SERVICE_TYPE = "_http._tcp." + + private var hostAddress: InetAddress? = null + private var hostPort: Int = 0 + private var mNsdManager: NsdManager? = null + private var clientTextColor: Int = 0 + private var msgList: LinearLayout? = null + private var edMessage: EditText? = null + + internal var mDiscoveryListener: NsdManager.DiscoveryListener = object : NsdManager.DiscoveryListener { + + // Called as soon as service discovery begins. + override fun onDiscoveryStarted(regType: String) { + Log.d(TAG, "Service discovery started") + } + + override fun onServiceFound(service: NsdServiceInfo) { + // A service was found! Do something with it. + Log.d(TAG, "Service discovery success : $service") + Log.d(TAG, "Host = " + service.serviceName) + Log.d(TAG, "port = " + service.port.toString()) + + if (service.serviceType != SERVICE_TYPE) { + // Service type is the string containing the protocol and + // transport layer for this service. + Log.d(TAG, "Unknown Service Type: " + service.serviceType) + } else if (service.serviceName == SERVICE_NAME) { + // The name of the service tells the user what they'd be + // connecting to. It could be "Bob's Chat App". + Log.d(TAG, "Same machine: $SERVICE_NAME") + } else { + Log.d(TAG, "Diff Machine : " + service.serviceName) + // connect to the service and obtain serviceInfo + + mNsdManager!!.resolveService(service, object : NsdManager.ResolveListener { + + override fun onResolveFailed(nsdServiceInfo: NsdServiceInfo, errorCode: Int) { + Log.e(TAG, "Resolve failed $errorCode") + Log.e(TAG, "serivce = $nsdServiceInfo") + } + + override fun onServiceResolved(serviceInfo: NsdServiceInfo) { + Log.d(TAG, "Resolve Succeeded. $serviceInfo") + + if (serviceInfo.serviceName == SERVICE_NAME) { + Log.d(TAG, "Same IP.") + return + } + + // Obtain port and IP + hostPort = serviceInfo.port + hostAddress = serviceInfo.host + Log.d(TAG, "hostPort=$hostPort") + Log.d(TAG, "hostAddess=" + hostAddress!!) + if (serviceInfo.serviceName.equals(Server.SERVICE_NAME) && serviceInfo.serviceType.equals(Server.SERVICE_TYPE)) + SERVER_IP = hostAddress!!.hostAddress + + } + }) + } + } + + override fun onServiceLost(service: NsdServiceInfo) { + // When the network service is no longer available. + // Internal bookkeeping code goes here. + Log.e(TAG, "service lost$service") + } + + override fun onDiscoveryStopped(serviceType: String) { + Log.i(TAG, "Discovery stopped: $serviceType") + } + + override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) { + Log.e(TAG, "Discovery failed: Error code:$errorCode") + mNsdManager!!.stopServiceDiscovery(this) + } + + override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) { + Log.e(TAG, "Discovery failed: Error code:$errorCode") + mNsdManager!!.stopServiceDiscovery(this) + } + } + + internal val time: String + get() { + val sdf = SimpleDateFormat("HH:mm:ss") + return sdf.format(Date()) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.ac_client) + + title = "Client" + clientTextColor = ContextCompat.getColor(this, R.color.green) + msgList = findViewById(R.id.msgList) + edMessage = findViewById(R.id.edMessage) + + handler = Handler() + mNsdManager = getSystemService(Context.NSD_SERVICE) as NsdManager + // mNsdManager.discoverServices(SERVICE_TYPE, + // NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); + } + + override fun onPause() { + if (mNsdManager != null) { + mNsdManager!!.stopServiceDiscovery(mDiscoveryListener) + } + super.onPause() + } + + override fun onResume() { + super.onResume() + if (mNsdManager != null) { + mNsdManager!!.discoverServices( + SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener + ) + } + + } + ////////////////////////////////////////////// + + fun textView(message: String?, color: Int): TextView { + var message = message + if (null == message || message.trim { it <= ' ' }.isEmpty()) { + message = "" + } + val tv = TextView(this) + tv.setTextColor(color) + tv.text = "$message [$time]" + tv.textSize = 20f + tv.setPadding(0, 5, 0, 0) + return tv + } + + fun showMessage(message: String, color: Int) { + handler!!.post { msgList!!.addView(textView(message, color)) } + } + + override fun onClick(view: View) { + + if (view.id == R.id.connect_server) { + msgList!!.removeAllViews() + showMessage("Connecting to Server...", clientTextColor) + clientThread = ClientThread() + thread = Thread(clientThread) + thread!!.start() + showMessage("Connected to Server...", clientTextColor) + return + } + + if (view.id == R.id.send_data) { + val clientMessage = edMessage!!.text.toString().trim { it <= ' ' } + showMessage(clientMessage, Color.BLUE) + if (null != clientThread) { + clientThread!!.sendMessage(clientMessage) + } + } + } + + internal inner class ClientThread : Runnable { + + private var socket: Socket? = null + private var input: BufferedReader? = null + + override fun run() { + + try { + val serverAddr = InetAddress.getByName(SERVER_IP) + socket = Socket(serverAddr, SERVERPORT) + + while (!Thread.currentThread().isInterrupted) { + + this.input = BufferedReader(InputStreamReader(socket!!.getInputStream())) + var message: String? = input!!.readLine() + if (null == message || "Disconnect".contentEquals(message)) { + Thread.interrupted() + message = "Server Disconnected." + showMessage(message, Color.RED) + break + } + showMessage("Server: $message", clientTextColor) + } + + } catch (e1: UnknownHostException) { + e1.printStackTrace() + } catch (e1: IOException) { + e1.printStackTrace() + } + + } + + fun sendMessage(message: String) { + Thread(Runnable { + try { + if (null != socket) { + val out = PrintWriter( + BufferedWriter( + OutputStreamWriter(socket!!.getOutputStream()) + ), + true + ) + out.println(message) + } + } catch (e: Exception) { + e.printStackTrace() + } + }).start() + } + + } + + override fun onDestroy() { + // if (mNsdManager != null) { + // mNsdManager.stopServiceDiscovery(mDiscoveryListener); + // } + super.onDestroy() + if (null != clientThread) { + clientThread!!.sendMessage("Disconnect") + clientThread = null + } + } + + companion object { + + val SERVERPORT = 3003 + + var SERVER_IP = "192.168.0.189" + } +} \ No newline at end of file diff --git a/app/src/main/java/uz/anor/clientserver/ClientActivity.java b/app/src/main/java/uz/anor/clientserver/ClientActivity.java new file mode 100644 index 0000000..7aa4de5 --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/ClientActivity.java @@ -0,0 +1,277 @@ +package uz.anor.clientserver; + +import android.content.Context; +import android.graphics.Color; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; +import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ClientActivity extends AppCompatActivity implements View.OnClickListener { + + public static final int SERVERPORT = 3003; + private String TAG = "MRX"; + + public static String SERVER_IP = "192.168.0.189"; + private ClientThread clientThread; + private Thread thread; + private LinearLayout msgList; + private Handler handler; + private int clientTextColor; + private EditText edMessage; + + private String SERVICE_NAME = "Client Device"; + private String SERVICE_TYPE = "_http._tcp."; + + private InetAddress hostAddress; + private int hostPort; + private NsdManager mNsdManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.ac_client); + + setTitle("Client"); + clientTextColor = ContextCompat.getColor(this, R.color.green); + handler = new Handler(); + msgList = findViewById(R.id.msgList); + edMessage = findViewById(R.id.edMessage); + + mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); +// mNsdManager.discoverServices(SERVICE_TYPE, +// NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); + } + + @Override + protected void onPause() { + if (mNsdManager != null) { + mNsdManager.stopServiceDiscovery(mDiscoveryListener); + } + super.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + if (mNsdManager != null) { + mNsdManager.discoverServices( + SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); + } + + } + + + NsdManager.DiscoveryListener mDiscoveryListener = new NsdManager.DiscoveryListener() { + + // Called as soon as service discovery begins. + @Override + public void onDiscoveryStarted(String regType) { + Log.d(TAG, "Service discovery started"); + } + + @Override + public void onServiceFound(NsdServiceInfo service) { + // A service was found! Do something with it. + Log.d(TAG, "Service discovery success : " + service); + Log.d(TAG, "Host = " + service.getServiceName()); + Log.d(TAG, "port = " + String.valueOf(service.getPort())); + + if (!service.getServiceType().equals(SERVICE_TYPE)) { + // Service type is the string containing the protocol and + // transport layer for this service. + Log.d(TAG, "Unknown Service Type: " + service.getServiceType()); + } else if (service.getServiceName().equals(SERVICE_NAME)) { + // The name of the service tells the user what they'd be + // connecting to. It could be "Bob's Chat App". + Log.d(TAG, "Same machine: " + SERVICE_NAME); + } else { + Log.d(TAG, "Diff Machine : " + service.getServiceName()); + // connect to the service and obtain serviceInfo + + mNsdManager.resolveService(service, new NsdManager.ResolveListener() { + + @Override + public void onResolveFailed(NsdServiceInfo nsdServiceInfo, int errorCode) { + Log.e(TAG, "Resolve failed " + errorCode); + Log.e(TAG, "serivce = " + nsdServiceInfo); + } + + @Override + public void onServiceResolved(NsdServiceInfo serviceInfo) { + Log.d(TAG, "Resolve Succeeded. " + serviceInfo); + + if (serviceInfo.getServiceName().equals(SERVICE_NAME)) { + Log.d(TAG, "Same IP."); + return; + } + + // Obtain port and IP + hostPort = serviceInfo.getPort(); + hostAddress = serviceInfo.getHost(); + Log.d(TAG, "hostPort=" + hostPort); + Log.d(TAG, "hostAddess=" + hostAddress); + SERVER_IP = hostAddress.getHostAddress(); + } + }); + } + } + + @Override + public void onServiceLost(NsdServiceInfo service) { + // When the network service is no longer available. + // Internal bookkeeping code goes here. + Log.e(TAG, "service lost" + service); + } + + @Override + public void onDiscoveryStopped(String serviceType) { + Log.i(TAG, "Discovery stopped: " + serviceType); + } + + @Override + public void onStartDiscoveryFailed(String serviceType, int errorCode) { + Log.e(TAG, "Discovery failed: Error code:" + errorCode); + mNsdManager.stopServiceDiscovery(this); + } + + @Override + public void onStopDiscoveryFailed(String serviceType, int errorCode) { + Log.e(TAG, "Discovery failed: Error code:" + errorCode); + mNsdManager.stopServiceDiscovery(this); + } + }; +////////////////////////////////////////////// + + public TextView textView(String message, int color) { + if (null == message || message.trim().isEmpty()) { + message = ""; + } + TextView tv = new TextView(this); + tv.setTextColor(color); + tv.setText(message + " [" + getTime() + "]"); + tv.setTextSize(20); + tv.setPadding(0, 5, 0, 0); + return tv; + } + + public void showMessage(final String message, final int color) { + handler.post(new Runnable() { + @Override + public void run() { + msgList.addView(textView(message, color)); + } + }); + } + + @Override + public void onClick(View view) { + + if (view.getId() == R.id.connect_server) { + msgList.removeAllViews(); + showMessage("Connecting to Server...", clientTextColor); + clientThread = new ClientThread(); + thread = new Thread(clientThread); + thread.start(); + showMessage("Connected to Server...", clientTextColor); + return; + } + + if (view.getId() == R.id.send_data) { + String clientMessage = edMessage.getText().toString().trim(); + showMessage(clientMessage, Color.BLUE); + if (null != clientThread) { + clientThread.sendMessage(clientMessage); + } + } + } + + class ClientThread implements Runnable { + + private Socket socket; + private BufferedReader input; + + @Override + public void run() { + + try { + InetAddress serverAddr = InetAddress.getByName(SERVER_IP); + socket = new Socket(serverAddr, SERVERPORT); + + while (!Thread.currentThread().isInterrupted()) { + + this.input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + String message = input.readLine(); + if (null == message || "Disconnect".contentEquals(message)) { + Thread.interrupted(); + message = "Server Disconnected."; + showMessage(message, Color.RED); + break; + } + showMessage("Server: " + message, clientTextColor); + } + + } catch (UnknownHostException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } + + } + + void sendMessage(final String message) { + new Thread(new Runnable() { + @Override + public void run() { + try { + if (null != socket) { + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(socket.getOutputStream())), + true); + out.println(message); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + + } + + String getTime() { + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); + return sdf.format(new Date()); + } + + @Override + protected void onDestroy() { +// if (mNsdManager != null) { +// mNsdManager.stopServiceDiscovery(mDiscoveryListener); +// } + super.onDestroy(); + if (null != clientThread) { + clientThread.sendMessage("Disconnect"); + clientThread = null; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/uz/anor/clientserver/MainActivity.kt b/app/src/main/java/uz/anor/clientserver/MainActivity.kt new file mode 100644 index 0000000..9e49cfd --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/MainActivity.kt @@ -0,0 +1,24 @@ +package uz.anor.clientserver + +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import kotlinx.android.synthetic.main.activity_main.* +import uz.anor.clientserver.client.ClientActivity +import uz.anor.clientserver.server.ServerActivity + +class MainActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + server.setOnClickListener { + startActivity(Intent(this, Server::class.java)) + } + client.setOnClickListener { + startActivity(Intent(this, Client::class.java)) + + } + + } +} diff --git a/app/src/main/java/uz/anor/clientserver/Server.kt b/app/src/main/java/uz/anor/clientserver/Server.kt new file mode 100644 index 0000000..f55180f --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/Server.kt @@ -0,0 +1,275 @@ +package uz.anor.clientserver + +import android.content.Context +import android.graphics.Color +import android.net.nsd.NsdManager +import android.net.nsd.NsdServiceInfo +import android.os.Build +import android.os.Bundle +import android.os.Handler +import android.util.Log +import android.view.View +import android.widget.EditText +import android.widget.LinearLayout +import android.widget.TextView +import androidx.annotation.RequiresApi +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import java.io.* +import java.net.ServerSocket +import java.net.Socket +import java.text.SimpleDateFormat +import java.util.* + +class Server : AppCompatActivity(), View.OnClickListener { + + private var serverSocket: ServerSocket? = null + private var tempClientSocket: Socket? = null + internal var serverThread: Thread? = null + private var handler: Handler? = null + private var mNsdManager: NsdManager? = null + private var msgList: LinearLayout? = null + private var greenColor: Int = 0 + private var edMessage: EditText? = null + + + + internal var mRegistrationListener: NsdManager.RegistrationListener = object : NsdManager.RegistrationListener { + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + override fun onServiceRegistered(NsdServiceInfo: NsdServiceInfo) { + val mServiceName = NsdServiceInfo.serviceName + SERVICE_NAME = mServiceName + Log.d("MRX", "Registered name : $mServiceName") + } + + override fun onRegistrationFailed( + serviceInfo: NsdServiceInfo, + errorCode: Int + ) { + // Registration failed! Put debugging code here to determine + // why. + } + + override fun onServiceUnregistered(serviceInfo: NsdServiceInfo) { + // Service has been unregistered. This only happens when you + // call + // NsdManager.unregisterService() and pass in this listener. + Log.d( + "MRX", + "Service Unregistered : " + serviceInfo.serviceName + ) + } + + override fun onUnregistrationFailed( + serviceInfo: NsdServiceInfo, + errorCode: Int + ) { + // Unregistration failed. Put debugging code here to determine + // why. + } + } + + internal val time: String + get() { + val sdf = SimpleDateFormat("HH:mm:ss") + return sdf.format(Date()) + } + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.ac_ser) + title = "Server" + greenColor = ContextCompat.getColor(this, R.color.green) + handler = Handler() + msgList = findViewById(R.id.msgList) + edMessage = findViewById(R.id.edMessage) + + mNsdManager = getSystemService(Context.NSD_SERVICE) as NsdManager + // registerService(SERVER_PORT); + } + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + override fun onPause() { + if (mNsdManager != null) { + mNsdManager!!.unregisterService(mRegistrationListener) + } + super.onPause() + } + + + override fun onResume() { + super.onResume() + if (mNsdManager != null) { + registerService(SERVER_PORT) + } + } + + fun registerService(port: Int) { + var serviceInfo: NsdServiceInfo? = null + serviceInfo = NsdServiceInfo() + serviceInfo.serviceName = SERVICE_NAME + serviceInfo.serviceType = SERVICE_TYPE + serviceInfo.port = port + + mNsdManager!!.registerService( + serviceInfo, + NsdManager.PROTOCOL_DNS_SD, + mRegistrationListener + ) + } + + //////////////////////////////////////////////////////////////////// + + fun textView(message: String?, color: Int): TextView { + var message = message + if (null == message || message.trim { it <= ' ' }.isEmpty()) { + message = "" + } + val tv = TextView(this) + tv.setTextColor(color) + tv.text = "$message [$time]" + tv.textSize = 20f + tv.setPadding(0, 5, 0, 0) + return tv + } + + fun showMessage(message: String, color: Int) { + handler!!.post { msgList!!.addView(textView(message, color)) } + } + + override fun onClick(view: View) { + if (view.id == R.id.start_server) { + msgList!!.removeAllViews() + showMessage("Server Started.", Color.BLACK) + this.serverThread = Thread(ServerThread()) + this.serverThread!!.start() + return + } + if (view.id == R.id.send_data) { + val msg = edMessage!!.text.toString().trim { it <= ' ' } + showMessage("Server : $msg", Color.BLUE) + clientsSocket.forEach { + sendMessage(msg, it) + } + } + } + + private fun sendMessage(message: String, socket: Socket) { + try { + if (null != tempClientSocket) { + Thread(Runnable { + var out: PrintWriter? = null + try { + out = PrintWriter( + BufferedWriter( + OutputStreamWriter(socket.getOutputStream()) + ), + true + ) + } catch (e: IOException) { + e.printStackTrace() + } + + out!!.println(message) + }).start() + } + } catch (e: Exception) { + e.printStackTrace() + } + + } + + private val clientsSocket = arrayListOf() + + internal inner class ServerThread : Runnable { + + override fun run() { + var socket: Socket + try { + serverSocket = ServerSocket(SERVER_PORT) + // findViewById(R.id.start_server).setVisibility(View.GONE); + } catch (e: IOException) { + e.printStackTrace() + showMessage("Error Starting Server : " + e.message, Color.RED) + } + + if (null != serverSocket) { + while (!Thread.currentThread().isInterrupted) { + try { + socket = serverSocket!!.accept() + clientsSocket.add(socket) + val commThread = CommunicationThread(socket) + Thread(commThread).start() + } catch (e: IOException) { + e.printStackTrace() + showMessage("Error Communicating to Client :" + e.message, Color.RED) + } + + } + } + } + } + + internal inner class CommunicationThread(private val clientSocket: Socket) : Runnable { + + private var input: BufferedReader? = null + + init { + tempClientSocket = clientSocket + try { + this.input = BufferedReader(InputStreamReader(this.clientSocket.getInputStream())) + } catch (e: IOException) { + e.printStackTrace() + showMessage("Error Connecting to Client!!", Color.RED) + } + + showMessage("Connected to Client!!", greenColor) + } + + override fun run() { + + while (!Thread.currentThread().isInterrupted) { + try { + var read: String? = input!!.readLine() + if (null == read || "Disconnect".contentEquals(read)) { + Thread.interrupted() + read = "Client Disconnected" + showMessage("Client : $read", greenColor) + break + } + showMessage("Client : $read", greenColor) + clientsSocket.forEach { + sendMessage(read,it) + } + } catch (e: IOException) { + e.printStackTrace() + } + + } + } + + } + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + override fun onDestroy() { +// if (mNsdManager != null) { +// mNsdManager!!.unregisterService(mRegistrationListener) +// } + super.onDestroy() + if (null != serverThread) { + clientsSocket.forEach { + sendMessage("Disconnect", it) + } + serverThread!!.interrupt() + serverThread = null + } + } + + companion object { + val SERVER_PORT = 3003 + var SERVICE_NAME = "Server Device" + val SERVICE_TYPE = "_http._tcp." + } +} \ No newline at end of file diff --git a/app/src/main/java/uz/anor/clientserver/ServerActivity.java b/app/src/main/java/uz/anor/clientserver/ServerActivity.java new file mode 100644 index 0000000..3ecb3ba --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/ServerActivity.java @@ -0,0 +1,265 @@ +package uz.anor.clientserver; + +import android.content.Context; +import android.graphics.Color; +import android.net.nsd.NsdManager; +import android.net.nsd.NsdServiceInfo; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; +import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ServerActivity extends AppCompatActivity implements View.OnClickListener { + + private ServerSocket serverSocket; + private Socket tempClientSocket; + Thread serverThread = null; + public static final int SERVER_PORT = 3003; + private LinearLayout msgList; + private Handler handler; + private int greenColor; + private EditText edMessage; + + private String SERVICE_NAME = "Server Device"; + private String SERVICE_TYPE = "_http._tcp."; + private NsdManager mNsdManager; + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.ac_ser); + setTitle("Server"); + greenColor = ContextCompat.getColor(this, R.color.green); + handler = new Handler(); + msgList = findViewById(R.id.msgList); + edMessage = findViewById(R.id.edMessage); + + mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); +// registerService(SERVER_PORT); + } + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + protected void onPause() { + if (mNsdManager != null) { + mNsdManager.unregisterService(mRegistrationListener); + } + super.onPause(); + } + + + + @Override + protected void onResume() { + super.onResume(); + if (mNsdManager != null) { + registerService(SERVER_PORT); + } + } + + public void registerService(int port) { + NsdServiceInfo serviceInfo = null; + serviceInfo = new NsdServiceInfo(); + serviceInfo.setServiceName(SERVICE_NAME); + serviceInfo.setServiceType(SERVICE_TYPE); + serviceInfo.setPort(port); + + mNsdManager.registerService(serviceInfo, + NsdManager.PROTOCOL_DNS_SD, + mRegistrationListener); + } + + NsdManager.RegistrationListener mRegistrationListener = new NsdManager.RegistrationListener() { + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) { + String mServiceName = NsdServiceInfo.getServiceName(); + SERVICE_NAME = mServiceName; + Log.d("MRX", "Registered name : " + mServiceName); + } + + @Override + public void onRegistrationFailed(NsdServiceInfo serviceInfo, + int errorCode) { + // Registration failed! Put debugging code here to determine + // why. + } + + @Override + public void onServiceUnregistered(NsdServiceInfo serviceInfo) { + // Service has been unregistered. This only happens when you + // call + // NsdManager.unregisterService() and pass in this listener. + Log.d("MRX", + "Service Unregistered : " + serviceInfo.getServiceName()); + } + + @Override + public void onUnregistrationFailed(NsdServiceInfo serviceInfo, + int errorCode) { + // Unregistration failed. Put debugging code here to determine + // why. + } + }; + + //////////////////////////////////////////////////////////////////// + + public TextView textView(String message, int color) { + if (null == message || message.trim().isEmpty()) { + message = ""; + } + TextView tv = new TextView(this); + tv.setTextColor(color); + tv.setText(message + " [" + getTime() +"]"); + tv.setTextSize(20); + tv.setPadding(0, 5, 0, 0); + return tv; + } + + public void showMessage(final String message, final int color) { + handler.post(new Runnable() { + @Override + public void run() { + msgList.addView(textView(message, color)); + } + }); + } + + @Override + public void onClick(View view) { + if (view.getId() == R.id.start_server) { + msgList.removeAllViews(); + showMessage("Server Started.", Color.BLACK); + this.serverThread = new Thread(new ServerThread()); + this.serverThread.start(); + return; + } + if (view.getId() == R.id.send_data) { + String msg = edMessage.getText().toString().trim(); + showMessage("Server : " + msg, Color.BLUE); + sendMessage(msg); + } + } + + private void sendMessage(final String message) { + try { + if (null != tempClientSocket) { + new Thread(new Runnable() { + @Override + public void run() { + PrintWriter out = null; + try { + out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(tempClientSocket.getOutputStream())), + true); + } catch (IOException e) { + e.printStackTrace(); + } + out.println(message); + } + }).start(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + class ServerThread implements Runnable { + + public void run() { + Socket socket; + try { + serverSocket = new ServerSocket(SERVER_PORT); +// findViewById(R.id.start_server).setVisibility(View.GONE); + } catch (IOException e) { + e.printStackTrace(); + showMessage("Error Starting Server : " + e.getMessage(), Color.RED); + } + if (null != serverSocket) { + while (!Thread.currentThread().isInterrupted()) { + try { + socket = serverSocket.accept(); + CommunicationThread commThread = new CommunicationThread(socket); + new Thread(commThread).start(); + } catch (IOException e) { + e.printStackTrace(); + showMessage("Error Communicating to Client :" + e.getMessage(), Color.RED); + } + } + } + } + } + + class CommunicationThread implements Runnable { + + private Socket clientSocket; + + private BufferedReader input; + + public CommunicationThread(Socket clientSocket) { + this.clientSocket = clientSocket; + tempClientSocket = clientSocket; + try { + this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); + } catch (IOException e) { + e.printStackTrace(); + showMessage("Error Connecting to Client!!", Color.RED); + } + showMessage("Connected to Client!!", greenColor); + } + + public void run() { + + while (!Thread.currentThread().isInterrupted()) { + try { + String read = input.readLine(); + if (null == read || "Disconnect".contentEquals(read)) { + Thread.interrupted(); + read = "Client Disconnected"; + showMessage("Client : " + read, greenColor); + break; + } + showMessage("Client : " + read, greenColor); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + } + + String getTime() { + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); + return sdf.format(new Date()); + } + + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) + @Override + protected void onDestroy() { + if (mNsdManager != null) { + mNsdManager.unregisterService(mRegistrationListener); + } + super.onDestroy(); + if (null != serverThread) { + sendMessage("Disconnect"); + serverThread.interrupt(); + serverThread = null; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/uz/anor/clientserver/client/ClientActivity.java b/app/src/main/java/uz/anor/clientserver/client/ClientActivity.java new file mode 100644 index 0000000..3e76082 --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/client/ClientActivity.java @@ -0,0 +1,146 @@ +package uz.anor.clientserver.client; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import uz.anor.clientserver.R; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; + +@SuppressLint("SetTextI18n") +public class ClientActivity extends AppCompatActivity { + + Thread Thread1 = null; + + EditText etIP, etPort; + TextView tvMessages; + + EditText etMessage; + Button btnSend; + + String SERVER_IP; + int SERVER_PORT; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_client); + + etIP = findViewById(R.id.etIP); + etPort = findViewById(R.id.etPort); + //test + etIP.setText("192.168.0.101"); + etPort.setText("8080"); + + tvMessages = findViewById(R.id.tvMessages); + etMessage = findViewById(R.id.etMessage); + btnSend = findViewById(R.id.btnSend); + + Button btnConnect = findViewById(R.id.btnConnect); + btnConnect.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + tvMessages.setText(""); + SERVER_IP = etIP.getText().toString().trim(); + SERVER_PORT = Integer.parseInt(etPort.getText().toString().trim()); + Thread1 = new Thread(new Thread1()); + Thread1.start(); + } + }); + + btnSend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String message = etMessage.getText().toString().trim(); + if (!message.isEmpty()) { + new Thread(new Thread3(message)).start(); + } + } + }); + } + + private PrintWriter output; + private BufferedReader input; + + class Thread1 implements Runnable { + + @Override + public void run() { + Socket socket; + try { + socket = new Socket(SERVER_IP, SERVER_PORT); + + output = new PrintWriter(socket.getOutputStream()); + input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + tvMessages.setText("Connected\n"); + } + }); + new Thread(new Thread2()).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + class Thread2 implements Runnable { + @Override + public void run() { + while (true) { + try { + final String message = input.readLine(); + if (message != null) { + runOnUiThread(new Runnable() { + @Override + public void run() { + tvMessages.append("server: " + message + "\n"); + } + }); + } else { + Thread1 = new Thread(new Thread1()); + Thread1.start(); + return; + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + class Thread3 implements Runnable { + private String message; + + Thread3(String message) { + this.message = message; + } + + @Override + public void run() { + output.write(message); + output.flush(); + runOnUiThread(new Runnable() { + @Override + public void run() { + tvMessages.append("client: " + message + "\n"); + etMessage.setText(""); + } + }); + } + } +} + + diff --git a/app/src/main/java/uz/anor/clientserver/server/ServerActivity.java b/app/src/main/java/uz/anor/clientserver/server/ServerActivity.java new file mode 100644 index 0000000..1fbe8be --- /dev/null +++ b/app/src/main/java/uz/anor/clientserver/server/ServerActivity.java @@ -0,0 +1,169 @@ +package uz.anor.clientserver.server; + +import android.annotation.SuppressLint; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; +import uz.anor.clientserver.R; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +@SuppressLint("SetTextI18n") +public class ServerActivity extends AppCompatActivity { + + ServerSocket serverSocket; + Thread Thread1 = null; + + TextView tvIP, tvPort; + TextView tvMessages; + + EditText etMessage; + Button btnSend; + + public static String SERVER_IP = ""; + public static final int SERVER_PORT = 8080; + + String message; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_server); + + tvIP = findViewById(R.id.tvIP); + tvPort = findViewById(R.id.tvPort); + tvMessages = findViewById(R.id.tvMessages); + etMessage = findViewById(R.id.etMessage); + btnSend = findViewById(R.id.btnSend); + + try { + SERVER_IP = getLocalIpAddress(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + + Thread1 = new Thread(new Thread1()); + Thread1.start(); + + btnSend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + message = etMessage.getText().toString().trim(); + if (!message.isEmpty()) { + new Thread(new Thread3(message)).start(); + } + } + }); + } + + private String getLocalIpAddress() throws UnknownHostException { + WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); + assert wifiManager != null; + WifiInfo wifiInfo = wifiManager.getConnectionInfo(); + int ipInt = wifiInfo.getIpAddress(); + return InetAddress.getByAddress( + ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()) + .getHostAddress(); + } + + private PrintWriter output; + private BufferedReader input; + + class Thread1 implements Runnable { + + @Override + public void run() { + Socket socket; + try { + serverSocket = new ServerSocket(SERVER_PORT); + runOnUiThread(new Runnable() { + + @Override + public void run() { + tvMessages.setText("Not connected"); + tvIP.setText("IP: " + SERVER_IP); + tvPort.setText("Port: " + String.valueOf(SERVER_PORT)); + } + }); + try { + socket = serverSocket.accept(); + + output = new PrintWriter(socket.getOutputStream()); + input = new BufferedReader(new InputStreamReader(socket.getInputStream())); + + runOnUiThread(new Runnable() { + @Override + public void run() { + tvMessages.setText("Connected\n"); + } + }); + new Thread(new Thread2()).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private class Thread2 implements Runnable { + @Override + public void run() { + while (true) { + try { + final String message = input.readLine(); + if (message != null) { + runOnUiThread(new Runnable() { + @Override + public void run() { + tvMessages.append("client:" + message + "\n"); + } + }); + } else { + Thread1 = new Thread(new Thread1()); + Thread1.start(); + return; + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + class Thread3 implements Runnable { + private String message; + + Thread3(String message) { + this.message = message; + } + + @Override + public void run() { + output.write(message); + output.flush(); + runOnUiThread(new Runnable() { + @Override + public void run() { + tvMessages.append("server: " + message + "\n"); + etMessage.setText(""); + } + }); + } + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..6348baa --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..a0ad202 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/ac_client.xml b/app/src/main/res/layout/ac_client.xml new file mode 100644 index 0000000..f235b02 --- /dev/null +++ b/app/src/main/res/layout/ac_client.xml @@ -0,0 +1,49 @@ + + + +