-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
101 lines (90 loc) · 3.61 KB
/
ChatClient.java
File metadata and controls
101 lines (90 loc) · 3.61 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
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
/**
* Client:
* - Allow users to connect to server.
* - Accept input text from user and pass it to server
* - Receive and display dialog from server
* - Send a message when user disconnects.
*
* start the ChatClient programs on the client hosts.
* USAGE: java ChatClient serverHost serverPort user
* optional command line arguments:
* [serverHost]: the IP or host name of the server to be connected to, "localhost" by default.
* [serverPort]: the port number the server is listening to, 8000 by default.
* [user]: the user using the chatting client, "anonymous_user" by default.
*
*NOTE: The client will fail to connect if the server is not turned on or wrong server host and port number are given.
*
* @author: Yu Jin
*/
public class ChatClient{
public static final String DEFAULT_SERVER = "localhost";
public static final String DEFAULT_USER = "anonymous_client";
private static ChatFrame frame;
private static String user;
private static Socket serverSocket;
public static void main(String[] args) throws InterruptedException, IOException {
final String server = (args.length > 0) ? args[0] : DEFAULT_SERVER;
final int port = (args.length > 1) ? Integer.parseInt(args[1]) : ChatServer.DEFAULT_PORT;
user = (args.length > 2) ? args[2] : DEFAULT_USER;
connect2Server(server, port);
EventQueue.invokeLater(new Runnable() {
public void run() {
ChatClient.frame = new ChatFrame(user, server, port);
ChatClient.frame.setTitle("Chatting Client");
ChatClient.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChatClient.frame.setVisible(true);
}
});
startChatting();
}
/**
* Start a thread to:
* 1. connect to the specified server host on the specified port,
* 2. and then keep processing incoming message from the server.
* @param server the host name or IP of the server to be connected.
* @param port the port number on which the server is listening.
*/
public static void connect2Server(String server, int port) throws IOException{
serverSocket = new Socket();
serverSocket.connect(new InetSocketAddress(server, port), 10000 );
PrintWriter output = new PrintWriter(serverSocket.getOutputStream(), true);
output.println(user);
}
private static void startChatting(){
try{
Scanner incomingMsg = new Scanner(serverSocket.getInputStream());
while (incomingMsg.hasNextLine()){
String msg = incomingMsg.nextLine();
if(ChatClient.frame != null)
ChatClient.frame.showMessage(msg);
}
}
catch(IOException e){
}
finally{
try{
if(serverSocket != null)
serverSocket.close();
serverSocket = null;
}
catch(IOException e){
}
}
}
public static void sendMsg(String msg) {
if(serverSocket != null){
try{
PrintWriter output = new PrintWriter(serverSocket.getOutputStream(), true);
output.println(msg);
}
catch(IOException e){
System.err.println("Failed to send message! Please be sure the server is still alive.");
}
}
}
}