Advertisement
anas_harby

Untitled

Jul 8th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. private void sendTCP(byte[] data) {
  2.         threadTCP = new Thread("Send TCP") {
  3.             public void run() {
  4.                 Socket socketSendTCP = null;
  5.                 try {
  6.                     socketSendTCP = new Socket(address, port);
  7.                 } catch (UnknownHostException e) {
  8.                     e.printStackTrace();
  9.                 } catch (IOException e) {
  10.                     e.printStackTrace();
  11.                 }
  12.                 OutputStream os = null;
  13.                 try {
  14.                     os = socketSendTCP.getOutputStream();
  15.                 } catch (IOException e) {
  16.                     e.printStackTrace();
  17.                 }
  18.                 OutputStreamWriter osw = new OutputStreamWriter(os);
  19.                 BufferedWriter bw = new BufferedWriter(osw);
  20.                 DataOutputStream dos = new DataOutputStream(os);
  21.                 try {
  22.                     bw.close();
  23.                 } catch (IOException e1) {
  24.                     e1.printStackTrace();
  25.                 }
  26.                 try {
  27.                     dos.writeInt(data.length);
  28.                 } catch (IOException e) {
  29.                     e.printStackTrace();
  30.                 }
  31.                 if (data.length > 0) {
  32.                     try {
  33.                         dos.write(data, 0, data.length);
  34.                     } catch (IOException e) {
  35.                         e.printStackTrace();
  36.                     }
  37.                 }
  38.                 try {
  39.                     socketSendTCP.close();
  40.                 } catch (IOException e) {
  41.                     e.printStackTrace();
  42.                 }
  43.             }
  44.         };
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement