-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
executable file
·64 lines (58 loc) · 2.05 KB
/
Client.java
File metadata and controls
executable file
·64 lines (58 loc) · 2.05 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
/* vim: set tw=80 ts=4 sts=4 sw=4 et: */
import java.net.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
* Creates a client instance for communicating with a compatable Server
* instance.
*
* @author Steven C. Nay
* @since 05 Feb 2007
*/
public class Client {
private static Comm comm;
private static ClientGui gui;
/**
* Runs the client
* @param args Pass in connection parameters as follows:
* - Host: IP address or hostname of server instance
* - Port: Port number to connect on
* - Username: Screen name for the server. May be rejected if invalid
* or already in use
*/
public static void main(String[] args) {
System.out.println(Application.NAME);
Scanner console = new Scanner(System.in);
//Get initialization settings from the command-line arguments
String host = args[0];
int port = Integer.parseInt(args[1]);
String you = args[2];
try {
//Create the ClientComm object
comm = new ClientComm(host, port, you);
//Create the GUI
gui = new ClientGui(comm);
gui.setVisibility(true);
//Set up the loop to read input from the comm object
while (true) {
try {
//Get the next line; will block until a line comes
String text = comm.nextLine();
gui.putMessage(text);
} catch (Exception e) {
//The connection has died
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"The connection with the server was lost");
System.exit(1);
}
}
} catch (Exception e) {
//If the connection fails, we can't go on.
JOptionPane.showMessageDialog(null,
"Error sending message: Connection could not be opened");
System.exit(1);
}
}
}