📄 client.java
字号:
// Client.java
package keti2;
import java.io.*;
import java.net.*;
public class Client
extends Thread {
public static final int Default_Port = 7456;
// 定义出错例程
public static final void usage() {
System.out.println("Usage: Java Client []");
System.exit(0);
}
public static void main(String args[]) {
int port = Default_Port;
Socket s = null;
// 解析端口参数
if ( (args.length != 1) && (args.length != 2)) usage();
if (args.length == 1)
port = Default_Port;
else {
try {
port = Integer.parseInt(args[1]);
}
catch (NumberFormatException e) {
usage();
}
}
try {
// 产生一个Socket ,通过指定的端口与主机通信。
s = new Socket(args[0], port);
// 产生用于发出和接收的文本字符流
DataInputStream sin = new DataInputStream(s.getInputStream());
PrintStream sout = new PrintStream(s.getOutputStream());
// 从控制台读入字符流
DataInputStream in = new DataInputStream(System.in);
// 返回连接的地址和端口
System.out.println("Connected to" + s.getInetAddress() + ":" + s.getPort());
String line;
for (; ; ) {
// 显示提示符
System.out.print(" >");
System.out.flush();
// 读入控制台输入的一行字符
line = in.readLine();
if (line == null)break;
// 将接收的文本行送至服务器
sout.println(line);
// 从服务器接收一行字符
line = sin.readLine();
// Check if connection is closed(i.e. for EOF)
if (line == null) {
System.out.println("Connection closed by server.");
break;
}
// 在控制台上显示接收的字符
System.out.println(line);
} // End of for loop
} // End of try
catch (IOException e) {
System.err.println(e);
}
// Always be sure to close the socket
finally {
try {
if (s != null) s.close();
}
catch (IOException e2) {
;
}
}
} // End of main
public static void StartClient(String serverName, int portNumber, String Msg) {
int port = Default_Port;
Socket s = null;
// 解析端口参数
if (portNumber != 0)
port = portNumber;
// Frame1.showLogTAText("port: ");
try {
// 产生一个Socket ,通过指定的端口与主机通信。
s = new Socket(serverName, port);
// Frame1.showLogTAText("socket");
// 产生用于发出和接收的文本字符流
// DataInputStream sin = new DataInputStream(s.getInputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(s.
getInputStream()));
PrintStream sout = new PrintStream(s.getOutputStream());
// 从控制台读入字符流
// DataInputStream in = new DataInputStream(System.in);
// 返回连接的地址和端口
System.out.println("Connected to" + s.getInetAddress() + ":" + s.getPort());
Frame1.showLogTAText("Connected to" + s.getInetAddress() + ":" +
s.getPort());
String line;
// for (; ; ) {
// 显示提示符
// System.out.print(" >");
// System.out.flush();
// 读入控制台输入的一行字符
// line = in.readLine();
// if (line == null)break;
// 将接收的文本行送至服务器
sout.println(Msg);
Frame1.showLogTAText("send ok");
/*
// 从服务器接收一行字符
line = sin.readLine();
// Check if connection is closed(i.e. for EOF)
if (line == null) {
System.out.println("Connection closed by server.");
Frame1.showLogTAText("Connection closed by server.");
// break;
}
// 在控制台上显示接收的字符
System.out.println(line);
}
*/
// } // End of for loop
} // End of try
catch (IOException e) {
System.err.println(e);
Frame1.showLogTAText("client socket err-" + e);
}
// Always be sure to close the socket
finally {
try {
if (s != null) s.close();
}
catch (IOException e2) {
;
}
}
} // End of Send
public static void StartClient(String serverName, int portNumber, char cOrder) {
int port = Default_Port;
Socket s = null;
char cArray[] = {
cOrder};
String Msg = new String(cArray);
// 解析端口参数
if (portNumber != 0)
port = portNumber;
try {
// 产生一个Socket ,通过指定的端口与主机通信。
s = new Socket(serverName, port);
// 产生用于发出和接收的文本字符流
DataInputStream sin = new DataInputStream(s.getInputStream());
PrintStream sout = new PrintStream(s.getOutputStream());
// 返回连接的地址和端口
System.out.println("Connected to" + s.getInetAddress() + ":" + s.getPort());
String line;
// 将接收的文本行送至服务器
sout.println(Msg);
// 从服务器接收一行字符
line = sin.readLine();
// Check if connection is closed(i.e. for EOF)
if (line == null) {
System.out.println("Connection closed by server.");
}
// 在控制台上显示接收的字符
System.out.println(line);
} // End of try
catch (IOException e) {
System.err.println(e);
}
// Always be sure to close the socket
finally {
try {
if (s != null) s.close();
}
catch (IOException e2) {
;
}
}
} // End of Send
} // End of Client
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -