MAIN函数:Socketsocket=newSocket();LogPrinter.setConnectTime(socket,"66.96.163.96",25);方法:publicstaticvoidsetConnectTime(Socketsocket,Stringname,intport){SocketAddress...
MAIN函数:
Socket socket = new Socket();
LogPrinter.setConnectTime(socket,"66.96.163.96",25);
方法:
public static void setConnectTime(Socket socket, String name, int port) {
SocketAddress address = new InetSocketAddress(name, port);
try {
long start = System.currentTimeMillis();
socket.connect(address);
System.out.println("连接成功-" + socket.getInetAddress() + " : "
+ (System.currentTimeMillis() - start) + "ms");
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
BufferedReader read = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line = null;
String line1 = null;
while ((line = read.readLine()) != null) {
System.out.println(line);
// System.out.println(line1 = input.readLine());
out.println(input.readLine());
}
} catch (BindException e) {
System.out.println("BindException-无法绑定本地IP或端口");
} catch (UnknownHostException e) {
System.out.println("UnknownHostException-无法识别该地址或IP:" + name);
} catch (ConnectException e) {
System.out.println("ConnectException-无法连接指定端口:" + port);
} catch (SocketException e) {
System.out.println("SocketException-连接超时");
} catch (IOException e) {
System.out.println("IOException");
}
finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("关闭Socket失败");
}
}
}
根据对方发过来的提示填写信息 (完全按照书上的操作 但回执信息不同 )
应该怎么正确填写? 求正常运行的方法 只要能发出去就行
展开