-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommunications.java
More file actions
169 lines (141 loc) · 4.26 KB
/
Communications.java
File metadata and controls
169 lines (141 loc) · 4.26 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.net.*;
import java.io.*;
public class Communications implements Runnable{
//houses all the methods for createing a server, and sending messages to others
private int communport;
private int secretport;
private Cryptogravisor crypt;
private Thread mservthread;
private Thread kservthread;
// private SocketServer msgServer;
//private SocketServer keyServer;
private boolean stopflag;
public Communications(int commport,int keyport, Cryptogravisor c) {
stopflag=false;
//initializes variables, calls server methodse
crypt=c;
communport=commport;
secretport=keyport;
mservthread=new Thread(this);
mservthread.start();
kservthread=new Thread(this);
kservthread.start();
System.out.println("ayylmao");
}
private void messageServer() { //fix
//THROW EXCEPTIONS
//initializes server on ports
System.out.println("messtart");
try {
ServerSocket msgServer=new ServerSocket(communport);
while (!stopflag) {
Socket cn= msgServer.accept();
//PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
InputStream istream= cn.getInputStream();
DataInputStream dat=new DataInputStream(istream);
char bu;
String message="";
try { //keep going until EOF
while (true) {
bu=dat.readChar();
System.out.println(bu);
message+=bu;
}
}
catch (EOFException e) {
System.out.println("end of file");//temp
}
//call method in crypt
System.out.println("Fe"+message);
crypt.handleMessage(message,cn.getInetAddress().getHostAddress());
//close streams
dat.close();istream.close();cn.close();
}
//cleanup
msgServer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void keyServer() {
//initializes keyserver on port
System.out.println("keyserver started");
try {
ServerSocket keyServer=new ServerSocket(secretport);
while (!stopflag) {
Socket cn= keyServer.accept();
//PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
InputStream istream= cn.getInputStream();
DataInputStream dat=new DataInputStream(istream);
byte buf[]=new byte[32]; //arb value, fix if not
byte mod[]=new byte[32];
int ind=0;
try { //keep going until EOF
//scans input and records it in arrays
while (ind<64) {
if (ind<32) {
buf[ind]=dat.readByte();
}
else {
mod[ind-32]=dat.readByte();
}
ind++;
}
}
catch (EOFException e) {
System.out.println("end of file");//temp
}
//call method in crypt
if (ind<=32) {//if the user not the one initiating
crypt.handleKey(buf,cn.getInetAddress().getHostAddress());
}
else {
crypt.handleKeyAndMod(buf,mod,cn.getInetAddress().getHostAddress());
//close streams
dat.close();istream.close();cn.close();
}
}
keyServer.close();
}catch (Exception e) {e.printStackTrace();}
}
public void send(String address, String message) throws Exception{
//send message to specified ip
Socket s = new Socket(address,communport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.writeChars(message);
s.close();
}
public void sendKey(String address,byte[] key) throws Exception {
//send encryption key via seperate port for cryptography purposes
Socket s = new Socket(address,secretport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.write(key,0,key.length);
s.close();
}
public void sendKeyAndMod(String address, byte key[],byte mod[]) throws Exception{
//sends this if the user is the one starting the conversation
//shares modulus for purposes of encoding
Socket s = new Socket(address,secretport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.write(key,0,key.length);
out.write(mod,0,mod.length);
s.close();
}
public void kill() {
//kill server
stopflag=true;
}
public void run() {
//initalizes servers on threads
if (Thread.currentThread()==mservthread) {//may not work
messageServer();
}
else if (Thread.currentThread()==kservthread) {
keyServer();
}
}
// public static void main(String args[] ){
//new Communications(6000,6001,new Cryptogravisor());
// }
}