-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
80 lines (72 loc) · 1.91 KB
/
Copy pathClient.java
File metadata and controls
80 lines (72 loc) · 1.91 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
import java.net.Socket;
import java.io.InvalidObjectException;
import java.security.KeyPair;
public class Client
{
private String username = null;
private Socket sendSocket = null;
private Socket receiveSocket = null;
private Thread sender = null;
private Thread receiver = null;
private KeyPair kp = null;
Client(String name, String ip, int port, int mode) throws InvalidObjectException
{
username = name;
try
{
sendSocket = new Socket(ip, port);
receiveSocket = new Socket(ip, port);
kp = Cryptography.generateKeyPair();
sender = new Thread(new Sender(sendSocket.getInputStream(),
sendSocket.getOutputStream(), this, kp, mode));
receiver = new Thread(new Receiver(receiveSocket.getInputStream(),
receiveSocket.getOutputStream(), this, kp, mode));
}
catch(Exception e)
{
// e.printStackTrace();
throw new InvalidObjectException("CLIENT_CREATE_ERROR");
}
}
private void start()
{
sender.start();
receiver.start();
}
//To synchronize username between Sender and Receiver objects
public synchronized String getUsername()
{
return username;
}
//To synchronize username between Sender and Receiver objects
public synchronized void setUsername(String s)
{
if(s==null)
System.exit(0);
this.username=s;
}
public static void main(String[] args)
{
try
{
int mode = Integer.parseInt(args[3]);
if(mode>=3||mode<0)
{
System.out.println("Invalid mode");
return;
}
Client c = new Client(args[0], args[1],
Integer.parseInt(args[2]), mode);
c.start();
}
catch (InvalidObjectException e)
{
// e.printStackTrace();
System.out.println("Unable to connect socket");
}
catch(Exception e)
{
System.out.println("Please enter valid parameters");
}
}
}