|
| 1 | +package com.example.picturetopc; |
| 2 | + |
| 3 | +import android.graphics.Bitmap; |
| 4 | +import android.widget.Button; |
| 5 | +import android.widget.EditText; |
| 6 | +import android.widget.ProgressBar; |
| 7 | + |
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.OutputStream; |
| 12 | +import java.net.DatagramPacket; |
| 13 | +import java.net.InetAddress; |
| 14 | +import java.net.MulticastSocket; |
| 15 | +import java.net.ServerSocket; |
| 16 | +import java.net.Socket; |
| 17 | +import java.net.SocketException; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.LinkedList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +class ConnHanlder extends Thread { |
| 25 | + MulticastSocket socket; |
| 26 | + |
| 27 | + InetAddress ip; |
| 28 | + int port; |
| 29 | + |
| 30 | + EditText code; |
| 31 | + |
| 32 | + |
| 33 | + public ConnHanlder(String _ip, int _port, EditText _code) throws IOException { |
| 34 | + socket = new MulticastSocket(); |
| 35 | + ip = InetAddress.getByName(_ip); |
| 36 | + socket.joinGroup(ip); |
| 37 | + socket.setTimeToLive(20); |
| 38 | + |
| 39 | + |
| 40 | + port = _port; |
| 41 | + |
| 42 | + code = _code; |
| 43 | + } |
| 44 | + |
| 45 | + public void run() { |
| 46 | + socket.connect(ip, port); |
| 47 | + |
| 48 | + while (true) { |
| 49 | + try { |
| 50 | + sleep(1000); |
| 51 | + } catch (InterruptedException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + |
| 55 | + String msg = null; |
| 56 | + |
| 57 | + |
| 58 | + msg = "{\"code\": \"" + code.getText().toString() + "\", \"ip\": \"" + "0" + "\", \"port\":" + 42069 + "}"; |
| 59 | + |
| 60 | + |
| 61 | + DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(), msg.getBytes().length, this.ip, this.port); |
| 62 | + |
| 63 | + |
| 64 | + try { |
| 65 | + socket.send(msgPacket); |
| 66 | + } catch (IOException e) { |
| 67 | + run(); |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class ConnListener extends Thread |
| 75 | +{ |
| 76 | + ServerSocket socket; |
| 77 | + ProgressBar progressBar; |
| 78 | + List<Connection> connections; |
| 79 | + |
| 80 | + Button button; |
| 81 | + |
| 82 | + public ConnListener(ProgressBar _progressBar, Button _button) throws IOException{ |
| 83 | + socket = new ServerSocket(42069); |
| 84 | + progressBar = _progressBar; |
| 85 | + |
| 86 | + connections = new ArrayList<>(); |
| 87 | + |
| 88 | + button = _button; |
| 89 | + } |
| 90 | + |
| 91 | + public void run(){ |
| 92 | + while (socket != null){ |
| 93 | + try { |
| 94 | + connections.add(new Connection(socket.accept(), this)); |
| 95 | + button.setText("Connected: " + connections.size()); |
| 96 | + } catch (IOException e) { |
| 97 | + run(); |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void Disconnect(Connection connection) { |
| 104 | + connections.remove(connection); |
| 105 | + button.setText("Connected: " + connections.size()); |
| 106 | + } |
| 107 | + |
| 108 | + public void SendAll(Bitmap pic) { |
| 109 | + for (Connection conn: |
| 110 | + connections) { |
| 111 | + conn.Send(pic); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void DisconnectAll() { |
| 116 | + for (Connection conn: |
| 117 | + connections) { |
| 118 | + conn.Disconnect(); |
| 119 | + } |
| 120 | + try { |
| 121 | + socket.close(); |
| 122 | + } catch (Exception e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + socket = null; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +class ConnSender extends Thread { |
| 130 | + Connection Listener; |
| 131 | + |
| 132 | + List<byte[]> Sendable; |
| 133 | + |
| 134 | + public ConnSender(Connection listener){ |
| 135 | + Sendable = new LinkedList<>(); |
| 136 | + Listener = listener; |
| 137 | + } |
| 138 | + |
| 139 | + public void run() { |
| 140 | + while (!Listener.Stop){ |
| 141 | + SendAll(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + private void SendAll() { |
| 147 | + if (Sendable.isEmpty()) { |
| 148 | + try { |
| 149 | + sleep(100); |
| 150 | + } catch (InterruptedException e) { |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + int size = 1024; |
| 157 | + byte[] send = Sendable.remove(0); |
| 158 | + byte[] sdata = new byte[size]; |
| 159 | + |
| 160 | + Listener.ConnListener.progressBar.setMax(send.length); |
| 161 | + try { |
| 162 | + for (int i = 0; i < send.length; i += size) { |
| 163 | + Listener.ConnListener.progressBar.setProgress(i); |
| 164 | + byte[] data = Arrays.copyOfRange(send, i, i+size); |
| 165 | + Listener.Output.write(data); |
| 166 | + } |
| 167 | + } catch (IOException e) { |
| 168 | + Listener.Disconnect(); |
| 169 | + return; |
| 170 | + } |
| 171 | + } |
| 172 | + public void KeepAlive(){ |
| 173 | + final byte[] biteses = "-1".getBytes(StandardCharsets.UTF_8); |
| 174 | + Sendable.add(biteses); |
| 175 | + } |
| 176 | + |
| 177 | + public void Send(Bitmap bmp){ |
| 178 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 179 | + |
| 180 | + bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); |
| 181 | + byte[] b = baos.toByteArray(); |
| 182 | + |
| 183 | + Sendable.add((String.valueOf(b.length)).getBytes(StandardCharsets.UTF_8)); |
| 184 | + Sendable.add(b); |
| 185 | + |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +class ConnTimeout extends Thread { |
| 191 | + ConnSender Sender; |
| 192 | + |
| 193 | + public ConnTimeout(ConnSender sender){ |
| 194 | + Sender = sender; |
| 195 | + } |
| 196 | + |
| 197 | + public void run() { |
| 198 | + while (!Sender.Listener.Stop){ |
| 199 | + SendAll(); |
| 200 | + try { |
| 201 | + sleep(1000); |
| 202 | + } catch (InterruptedException e) { |
| 203 | + e.printStackTrace(); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + private void SendAll() { |
| 210 | + Sender.KeepAlive(); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +class Connection { |
| 215 | + java.net.Socket Socket; |
| 216 | + InputStream Input; |
| 217 | + OutputStream Output; |
| 218 | + |
| 219 | + //ConnReader Reader; |
| 220 | + ConnSender Sender; |
| 221 | + |
| 222 | + ConnTimeout Timeout; |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + ConnListener ConnListener; |
| 227 | + boolean Stop; |
| 228 | + |
| 229 | + public Connection(Socket socket, ConnListener connListener) throws SocketException { |
| 230 | + Socket = socket; |
| 231 | + socket.setSoTimeout(1*1000); |
| 232 | + ConnListener = connListener; |
| 233 | + |
| 234 | + Stop = false; |
| 235 | + |
| 236 | + try { |
| 237 | + Input = Socket.getInputStream(); |
| 238 | + Output = Socket.getOutputStream(); |
| 239 | + |
| 240 | + } catch (IOException e) { |
| 241 | + return; |
| 242 | + } |
| 243 | + |
| 244 | + //Reader = new ConnReader(this); |
| 245 | + Sender = new ConnSender(this); |
| 246 | + |
| 247 | + Timeout = new ConnTimeout(Sender); |
| 248 | + |
| 249 | + //Reader.start(); |
| 250 | + Sender.start(); |
| 251 | + |
| 252 | + Timeout.start(); |
| 253 | + } |
| 254 | + |
| 255 | + public void Send(Bitmap bmp){ |
| 256 | + Sender.Send(bmp); |
| 257 | + } |
| 258 | + |
| 259 | + public void Disconnect() { |
| 260 | + try { |
| 261 | + Socket.close();} |
| 262 | + catch (IOException e) |
| 263 | + {} |
| 264 | + Stop = true; |
| 265 | + |
| 266 | + //Sender.join(); |
| 267 | + //Reader.join(); |
| 268 | + |
| 269 | + ConnListener.Disconnect(this); |
| 270 | + } |
| 271 | +} |
0 commit comments