-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientWrite.java
More file actions
79 lines (61 loc) · 1.89 KB
/
ClientWrite.java
File metadata and controls
79 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
cryptochat Android app uses a symmetric key for
encryption/decryption and is designed to be
completely anonymous.
Copyright (c) 2015 GB Tony Cabrera
NOTE: You must provide your own encrypt/decrypt algorithms
*/
package com.example.app1114;
import android.util.Log;
import java.io.*;
import java.net.*;
public class ClientWrite implements Runnable {
private static final String APP_NAME = MainActivity.APP_NAME;
private String SERVER_IP = MainActivity.SERVER_IP;
private static final int PORT = MainActivity.PORT;
private String FROM = MainActivity.FROM;
private String TO = MainActivity.TO;
private byte[] bytes;
private String header = "";
private byte[] header_bytes;
public ClientWrite(byte[] b){
this.bytes = b;
}
public void run() {
Log.d(APP_NAME, "Trying " + SERVER_IP + ":" + PORT);
try{
Socket s = new Socket(SERVER_IP, PORT);
Log.d(APP_NAME, "Connected.");
Log.d(APP_NAME, "From: " + FROM);
Log.d(APP_NAME, "To: " + TO);
header = FROM + ":" + TO + "#!";
OutputStream os = s.getOutputStream();
try{
header_bytes = header.getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
}
short h_l = (short)header_bytes.length;
short b_l = (short)bytes.length;
short l = (short)(h_l + b_l);
byte[] len = new byte[2];
len[0] = (byte)(l & 0xff);
len[1] = (byte)((l >> 8) & 0xff);
Log.d(APP_NAME, "Sending bytes: " + l);
os.write(len[0]);
os.write(len[1]);
// SEND HEADER
os.write(header_bytes);
os.write(bytes);
os.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String reply = in.readLine();
Log.d(APP_NAME, "Server reply: " + reply);
Log.d(APP_NAME, "Message sent.");
s.close();
} catch (UnknownHostException e) {
String st = Log.getStackTraceString(e);
} catch (IOException e) {
String st = Log.getStackTraceString(e);
}
}
}