11package com .example .picturetopc ;
22
33import android .graphics .Bitmap ;
4+ import android .os .Debug ;
5+ import android .util .Log ;
46import android .widget .Button ;
57import android .widget .EditText ;
68import android .widget .ProgressBar ;
9+ import android .widget .TextView ;
10+
11+ import org .w3c .dom .Text ;
712
813import java .io .ByteArrayOutputStream ;
14+ import java .io .Console ;
915import java .io .IOException ;
1016import java .io .InputStream ;
1117import java .io .OutputStream ;
2026import java .util .Arrays ;
2127import java .util .LinkedList ;
2228import java .util .List ;
29+ import java .util .stream .Stream ;
2330
24- class ConnHanlder extends Thread {
31+ class ConnHandler extends Thread {
2532 MulticastSocket socket ;
2633
2734 InetAddress ip ;
2835 int port ;
2936
37+ EditText name ;
3038 EditText code ;
3139
3240
33- public ConnHanlder (String _ip , int _port , EditText _code ) throws IOException {
41+ public ConnHandler (String _ip , int _port , EditText _name , EditText _code ) throws IOException {
3442 socket = new MulticastSocket ();
3543 ip = InetAddress .getByName (_ip );
3644 socket .joinGroup (ip );
@@ -39,6 +47,7 @@ public ConnHanlder(String _ip, int _port, EditText _code) throws IOException {
3947
4048 port = _port ;
4149
50+ name = _name ;
4251 code = _code ;
4352 }
4453
@@ -55,7 +64,7 @@ public void run() {
5564 String msg = null ;
5665
5766
58- msg = "{\" code \" : \" " + code .getText ().toString () + "\" , \" ip \" : \" " + "0" + "\" , \" port\" :" + 42069 + "}" ;
67+ msg = "{\" name \" : \" " + name .getText ().toString () + "\" , \" code \" : \" " + code . getText (). toString () + "\" , \" port\" :" + 42069 + "}" ;
5968
6069
6170 DatagramPacket msgPacket = new DatagramPacket (msg .getBytes (), msg .getBytes ().length , this .ip , this .port );
@@ -77,22 +86,21 @@ class ConnListener extends Thread
7786 ProgressBar progressBar ;
7887 List <Connection > connections ;
7988
80- Button button ;
89+ TextView connText ;
8190
82- public ConnListener (ProgressBar _progressBar , Button _button ) throws IOException {
91+ public ConnListener (ProgressBar _progressBar , TextView conntext ) throws IOException {
8392 socket = new ServerSocket (42069 );
8493 progressBar = _progressBar ;
8594
8695 connections = new ArrayList <>();
8796
88- button = _button ;
97+ connText = conntext ;
8998 }
9099
91100 public void run (){
92101 while (socket != null ){
93102 try {
94103 connections .add (new Connection (socket .accept (), this ));
95- button .setText ("Connected: " + connections .size ());
96104 } catch (IOException e ) {
97105 run ();
98106 return ;
@@ -102,7 +110,18 @@ public void run(){
102110
103111 public void Disconnect (Connection connection ) {
104112 connections .remove (connection );
105- button .setText ("Connected: " + connections .size ());
113+ ChangeConnections ();
114+ }
115+
116+ public void ChangeConnections (){
117+ String [] text = new String [connections .size ()];
118+
119+ for (int i = 0 ; i <text .length ; i ++){
120+ text [i ] = ("• " + connections .get (i ).Name );
121+ }
122+
123+ connText .setText (String .join ("\n " , text ));
124+
106125 }
107126
108127 public void SendAll (Bitmap pic ) {
@@ -125,10 +144,32 @@ public void DisconnectAll() {
125144 socket = null ;
126145 }
127146}
147+ class ConnReader extends Thread {
148+ Connection Listener ;
149+
150+ public ConnReader (Connection listener ){
151+ Listener = listener ;
152+ }
153+
154+ public void run () {
155+ while (true ){
156+ //Get name
157+ byte [] bytes = new byte [1024 ];
158+ try {
159+ Listener .Input .read (bytes );
160+ } catch (IOException e ) {
161+ break ;
162+ }
163+ if (bytes [0 ] == 0 ) continue ;
164+ Listener .Name = new String (bytes , StandardCharsets .UTF_8 ).trim ();
165+ break ;
166+ }
167+ Listener .ConnListener .ChangeConnections ();
168+ }
169+ }
128170
129171class ConnSender extends Thread {
130172 Connection Listener ;
131-
132173 List <byte []> Sendable ;
133174
134175 public ConnSender (Connection listener ){
@@ -148,14 +189,13 @@ private void SendAll() {
148189 try {
149190 sleep (100 );
150191 } catch (InterruptedException e ) {
151- e . printStackTrace () ;
192+ return ;
152193 }
153194 return ;
154195 }
155196
156197 int size = 1024 ;
157198 byte [] send = Sendable .remove (0 );
158- byte [] sdata = new byte [size ];
159199
160200 Listener .ConnListener .progressBar .setMax (send .length );
161201 try {
@@ -166,7 +206,6 @@ private void SendAll() {
166206 }
167207 } catch (IOException e ) {
168208 Listener .Disconnect ();
169- return ;
170209 }
171210 }
172211 public void KeepAlive (){
@@ -216,19 +255,21 @@ class Connection {
216255 InputStream Input ;
217256 OutputStream Output ;
218257
219- // ConnReader Reader;
258+ ConnReader Reader ;
220259 ConnSender Sender ;
221260
222261 ConnTimeout Timeout ;
223262
263+ public String Name ;
264+
224265
225266
226267 ConnListener ConnListener ;
227268 boolean Stop ;
228269
229270 public Connection (Socket socket , ConnListener connListener ) throws SocketException {
230271 Socket = socket ;
231- socket .setSoTimeout (1 * 1000 );
272+ socket .setSoTimeout (1000 );
232273 ConnListener = connListener ;
233274
234275 Stop = false ;
@@ -241,12 +282,13 @@ public Connection(Socket socket, ConnListener connListener) throws SocketExcepti
241282 return ;
242283 }
243284
244- //Reader = new ConnReader(this);
285+
286+ Reader = new ConnReader (this );
245287 Sender = new ConnSender (this );
246288
247289 Timeout = new ConnTimeout (Sender );
248290
249- // Reader.start();
291+ Reader .start ();
250292 Sender .start ();
251293
252294 Timeout .start ();
@@ -259,7 +301,7 @@ public void Send(Bitmap bmp){
259301 public void Disconnect () {
260302 try {
261303 Socket .close ();}
262- catch (IOException e )
304+ catch (IOException ignored )
263305 {}
264306 Stop = true ;
265307
0 commit comments