📄 chatclient.java
字号:
/////////////////////////////////////////
// Java Programming Course, job-1
// Date: 2008/3/31
/////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class ChatClient implements Runnable
{
static Socket clientSocket = null;
static PrintStream os = null;
static InputStreamReader bReader = null;
static BufferedReader is = null;
static InputStreamReader inputReader = null;
static BufferedReader inputLine = null;
static boolean closed = false;
static boolean getName = false;
static String userName = null;
static boolean busy = false;
public static void main(String[] args)
{
// The default port
int port_number=8000;
String host="localhost";
String userInput;
if (args.length < 2)
{
System.out.println("Usage: java ChatClient [ServerName] [PortNumber] \n"+
"Now using default setting --> host="+host+", port_number="+port_number);
}
else
{
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
// Initialization section:
try
{
inputReader = new InputStreamReader(System.in);
inputLine = new BufferedReader(inputReader);
clientSocket = new Socket(host, port_number);
os = new PrintStream(clientSocket.getOutputStream());
bReader = new InputStreamReader(clientSocket.getInputStream());
is = new BufferedReader(bReader);
}
catch (UnknownHostException e) {
reconnect();
}
catch (IOException e) {
reconnect();
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port port_number
if (clientSocket != null && os != null && is != null)
{
try
{
// Create a thread to read from the server
new Thread(new ChatClient()).start();
while (!closed)
{
userInput = inputLine.readLine();
if (getName)
{
String s = "> ";
if (userInput != null)
{
userName = userInput;
userName = userName.concat(s);
getName = false;
}
else
{
break;
}
}
if (!busy)
os.println(userInput);
}
os.close();
is.close();
clientSocket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
//
// Read string from ChatServer side, and print to ChatClient's console.
//
public void run()
{
String responseLine, outresponse;
boolean ln = false;
// Keep on reading from the socket till we receive the "Bye" from the server,
// once we received that then we want to break.
try
{
while ((responseLine = is.readLine()) != null)
{
if (responseLine.startsWith("Username")) {
System.out.print(responseLine);
getName = true;
}
else {
if (userName != null && responseLine.startsWith(userName)) {
System.out.print(responseLine);
ln = true;
}
else if (responseLine.startsWith("kick ")) {
System.out.println("kick by someone, bye!");
os.println("client end");
os.close();
is.close();
clientSocket.close();
System.exit(0);
}
else if (responseLine.startsWith("[busy]")) {
busy = true;
}
else if (responseLine.startsWith("[nonbusy]")) {
busy = false;
}
else {
if (ln) {
System.out.println();
ln = false;
}
System.out.println(responseLine);
}
}
if (responseLine.indexOf("*** Bye") != -1)
{
os.close();
is.close();
clientSocket.close();
System.exit(0);
}
}
closed = true;
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
//
// If connection failed using this function to do the reconnection.
// Command: "/connect hostname portnumber "
//
public static void reconnect()
{
String response;
System.err.println("**** The server does not exist. Please type different domain and/or port.");
try
{
while ((response = inputLine.readLine()) != null)
{
if (response.indexOf("/connect ") != -1)
{
StringTokenizer st = new StringTokenizer(response);
String _host = null;
String _port = null;
int pn = 0;
_host = st.nextToken();
_host = st.nextToken();
_port = st.nextToken();
if (_host != null && _port != null)
pn=Integer.valueOf(_port).intValue();
else
continue;
try {
clientSocket = new Socket(_host, pn);
os = new PrintStream(clientSocket.getOutputStream());
bReader = new InputStreamReader(clientSocket.getInputStream());
is = new BufferedReader(bReader);
}
catch (UnknownHostException e) {
System.out.println("**** The server does not exist. Please type different domain and/or port.");
continue;
}
catch (ConnectException e) {
System.out.println("**** The server does not exist. Please type different domain and/or port.");
continue;
}
if (clientSocket != null && os != null && is != null)
break;
}
}
}
catch (IOException ee) {
System.err.println("IOException: " + ee);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -