import java.lang.System;
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;public class PortTalkApp {
public static void main(String args[]) {
PortTalk portTalk = new PortTalk(args);
portTalk.displayDestinationParameters();
portTalk.displayLocalParameters();
portTalk.chat();
portTalk.shutdown();
}
}
class PortTalk {
Socket connection;
DataOutputStream outStream;
DataInputStream inStream;
public PortTalk(String args[]) { //这里是什么意思啊,每次执行的时候
if (args.length != 2) error("Usage: PortTalkApp 主机端口");//他只会执行到error,后面的不执行了
String destination = args[0];
int port = 0;
try {
port = Integer.valueOf(args[1]).intValue();
} catch (NumberFormatException ex) {
error("非法端口号");
}
try {
connection = new Socket(destination, port);
} catch (UnknownHostException ex1) {
error("找不到主机");
} catch (IOException ex1) {
error("创建Socket时IO异常");
} try {
inStream = new DataInputStream(
connection.getInputStream());
outStream = new DataOutputStream(
connection.getOutputStream());
} catch (IOException ex1) {
error("IO error getting streams");
}
System.out.println(
"连接到" + destination + " 端口号是:" + port + ".");
} public void displayDestinationParameters() {
InetAddress destAddress = connection.getInetAddress();
String name = destAddress.getHostName();
byte ipAddress[] = destAddress.getAddress();
int port = connection.getPort();
displayParameters("目标", name, ipAddress, port);
} public void displayLocalParameters() {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
error("获得本地信息错误");
} String name = localAddress.getHostName();
byte ipAddress[] = localAddress.getAddress();
int port = connection.getLocalPort();
displayParameters("本地", name, ipAddress, port);
} public void displayParameters(
String s, String name, byte ipAddress[], int port) {
System.out.println(s + " 主机是 " + name + ".");
System.out.print(s + " IP 地址是 ");
for (int i = 0; i < ipAddress.length; ++i)
System.out.print((ipAddress[i] + 256) % 256 + ".");
System.out.println();
System.out.println(s + " 端口号是 " + port + ".");
} public void chat() {
DataInputStream keyboardInput = new DataInputStream(System.in);
boolean finished = false;
do {
try {
System.out.print("Send,receive,or quit(S/R/Q): ");
System.out.flush();
String line = keyboardInput.readLine();//已经被删除的方法,能用什么代替啊,我用过Reader类
if (line.length() > 0) { //进行修改,但还是报错
line = line.toUpperCase();
switch (line.charAt(0)) {
case 'S':
String sendLine = keyboardInput.readLine();
outStream.writeBytes(sendLine);
outStream.write(13);
outStream.write(10);
outStream.flush();
break;
case 'R':
int inByte;
System.out.print("* * *");
while ((inByte = inStream.read()) != '\n')
System.out.write(inByte);
System.out.println();
break;
case 'Q':
finished = true;
break;
default:
break;
}
}
} catch (IOException ex) {
error("Error reading from keyboard or socket");
} } while (!finished);
} public void shutdown() {
try {
connection.close();
} catch (IOException ex) {
error("关闭Socket时IO错误");
}
} public void error(String s) {
System.out.println(s);
System.exit(1);
}
}
怎样用上面的代码转换成一个文字游戏啊,求!!!!
import java.net.Socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;public class PortTalkApp {
public static void main(String args[]) {
PortTalk portTalk = new PortTalk(args);
portTalk.displayDestinationParameters();
portTalk.displayLocalParameters();
portTalk.chat();
portTalk.shutdown();
}
}
class PortTalk {
Socket connection;
DataOutputStream outStream;
DataInputStream inStream;
public PortTalk(String args[]) { //这里是什么意思啊,每次执行的时候
if (args.length != 2) error("Usage: PortTalkApp 主机端口");//他只会执行到error,后面的不执行了
String destination = args[0];
int port = 0;
try {
port = Integer.valueOf(args[1]).intValue();
} catch (NumberFormatException ex) {
error("非法端口号");
}
try {
connection = new Socket(destination, port);
} catch (UnknownHostException ex1) {
error("找不到主机");
} catch (IOException ex1) {
error("创建Socket时IO异常");
} try {
inStream = new DataInputStream(
connection.getInputStream());
outStream = new DataOutputStream(
connection.getOutputStream());
} catch (IOException ex1) {
error("IO error getting streams");
}
System.out.println(
"连接到" + destination + " 端口号是:" + port + ".");
} public void displayDestinationParameters() {
InetAddress destAddress = connection.getInetAddress();
String name = destAddress.getHostName();
byte ipAddress[] = destAddress.getAddress();
int port = connection.getPort();
displayParameters("目标", name, ipAddress, port);
} public void displayLocalParameters() {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
error("获得本地信息错误");
} String name = localAddress.getHostName();
byte ipAddress[] = localAddress.getAddress();
int port = connection.getLocalPort();
displayParameters("本地", name, ipAddress, port);
} public void displayParameters(
String s, String name, byte ipAddress[], int port) {
System.out.println(s + " 主机是 " + name + ".");
System.out.print(s + " IP 地址是 ");
for (int i = 0; i < ipAddress.length; ++i)
System.out.print((ipAddress[i] + 256) % 256 + ".");
System.out.println();
System.out.println(s + " 端口号是 " + port + ".");
} public void chat() {
DataInputStream keyboardInput = new DataInputStream(System.in);
boolean finished = false;
do {
try {
System.out.print("Send,receive,or quit(S/R/Q): ");
System.out.flush();
String line = keyboardInput.readLine();//已经被删除的方法,能用什么代替啊,我用过Reader类
if (line.length() > 0) { //进行修改,但还是报错
line = line.toUpperCase();
switch (line.charAt(0)) {
case 'S':
String sendLine = keyboardInput.readLine();
outStream.writeBytes(sendLine);
outStream.write(13);
outStream.write(10);
outStream.flush();
break;
case 'R':
int inByte;
System.out.print("* * *");
while ((inByte = inStream.read()) != '\n')
System.out.write(inByte);
System.out.println();
break;
case 'Q':
finished = true;
break;
default:
break;
}
}
} catch (IOException ex) {
error("Error reading from keyboard or socket");
} } while (!finished);
} public void shutdown() {
try {
connection.close();
} catch (IOException ex) {
error("关闭Socket时IO错误");
}
} public void error(String s) {
System.out.println(s);
System.exit(1);
}
}
怎样用上面的代码转换成一个文字游戏啊,求!!!!
//因为你的args.length 不等于2阿~所以,每次都调用这个方法
public void error(String s) {
System.out.println(s);
System.exit(1); //退出进程!
}
并且退出这个进程!
1 运行时需要2个参数,你没有给!
2 键盘读取参考这个 http://www.java2000.net/viewthread.jsp?tid=164#T997
答:用BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
if (args.length != 2) error("Usage: PortTalkApp 主机端口");//他只会执行到error,后面的不执行了
答:这种其实就类似于main中的String args[]一样,使用接受运行参数的
在c/c++下是int main(int argc, char** argv)其实是一样的,argc是指的参数个数,对应于java中的args.length