
这个程序在eclipse中为什么执行不了?
EchoServer.javaimportjava.io.*;importjava.net.*;classEchoServerThreadextendsThread{pr...
EchoServer.java
import java.io.*;
import java.net.*;
class EchoServerThread extends Thread{
private Socket connection;
public EchoServerThread(Socket c){
connection=c;
start();
}
public void run(){
try{
DataInputStream in=new DataInputStream(connection.getInputStream());
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
System.out.println("has opened I/O byte stream on the connection to client");
String line=new String("");
while(!line.equalsIgnoreCase("QUIT")){
line=in.readUTF();
System.out.println("Echoing:"+line);
if(line.toUpperCase().equals("SHUTDOWN")){
EchoServer.running=false;
line="QUIT";
}
out.writeUTF(line);
}
in.close();out.close();
connection.close();
System.out.println("Connection close.");
}
catch(IOException ioe){
System.out.println("Connection closed unexpectedly.");
}
}
}
public class EchoServer {
static boolean running=true;
public static void main(String[] args){
try{
ServerSocket server=new ServerSocket(Integer.parseInt(args[0]));
System.out.println("Server started on"+server.getLocalPort());
while(running){
Socket connection=server.accept();
System.out.println("New connection moved to thread.");
EchoServerThread handler=new EchoServerThread(connection);
}
server.close();
System.out.println("server has closed!");
}
catch(IOException ioe){
System.err.println("Error:"+ioe);
}
}
}
EchoClient.java
import java.net.*;
import java.io.*;
public class EchoClient {
public static void main(String[] args){
try{
Socket connection=new Socket(args[0],8000);
System.out.println("Connection established");
DataInputStream in=new DataInputStream(connection.getInputStream());
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
System.out.println("has opened the I/O byte stream on the connection to server");
String line=new String("");
while(!line.toUpperCase().equals("QUIT")){
System.out.print("Enter string:");
line=readString();
System.out.println("Sending string to server……");
out.writeUTF(line);
System.out.println("Waiting for server response……");
line=in.readUTF();
System.out.println("Received:"+line);
}
in.close();out.close();
connection.close();
System.out.println("connection closed!");
}
catch(UnknownHostException uhe){
System.err.println("Unknown host:"+args[0]);
}
catch(IOException ioe){
System.err.println("IOException:"+ioe);
}
}
public static String readString(){
String str=new String();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
str=in.readLine();
}catch(IOException e){
System.out.println("Console.readString:Unknow error……");
System.exit(-1);
}
return str;
}
} 展开
import java.io.*;
import java.net.*;
class EchoServerThread extends Thread{
private Socket connection;
public EchoServerThread(Socket c){
connection=c;
start();
}
public void run(){
try{
DataInputStream in=new DataInputStream(connection.getInputStream());
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
System.out.println("has opened I/O byte stream on the connection to client");
String line=new String("");
while(!line.equalsIgnoreCase("QUIT")){
line=in.readUTF();
System.out.println("Echoing:"+line);
if(line.toUpperCase().equals("SHUTDOWN")){
EchoServer.running=false;
line="QUIT";
}
out.writeUTF(line);
}
in.close();out.close();
connection.close();
System.out.println("Connection close.");
}
catch(IOException ioe){
System.out.println("Connection closed unexpectedly.");
}
}
}
public class EchoServer {
static boolean running=true;
public static void main(String[] args){
try{
ServerSocket server=new ServerSocket(Integer.parseInt(args[0]));
System.out.println("Server started on"+server.getLocalPort());
while(running){
Socket connection=server.accept();
System.out.println("New connection moved to thread.");
EchoServerThread handler=new EchoServerThread(connection);
}
server.close();
System.out.println("server has closed!");
}
catch(IOException ioe){
System.err.println("Error:"+ioe);
}
}
}
EchoClient.java
import java.net.*;
import java.io.*;
public class EchoClient {
public static void main(String[] args){
try{
Socket connection=new Socket(args[0],8000);
System.out.println("Connection established");
DataInputStream in=new DataInputStream(connection.getInputStream());
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
System.out.println("has opened the I/O byte stream on the connection to server");
String line=new String("");
while(!line.toUpperCase().equals("QUIT")){
System.out.print("Enter string:");
line=readString();
System.out.println("Sending string to server……");
out.writeUTF(line);
System.out.println("Waiting for server response……");
line=in.readUTF();
System.out.println("Received:"+line);
}
in.close();out.close();
connection.close();
System.out.println("connection closed!");
}
catch(UnknownHostException uhe){
System.err.println("Unknown host:"+args[0]);
}
catch(IOException ioe){
System.err.println("IOException:"+ioe);
}
}
public static String readString(){
String str=new String();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
str=in.readLine();
}catch(IOException e){
System.out.println("Console.readString:Unknow error……");
System.exit(-1);
}
return str;
}
} 展开
2个回答
展开全部
追问
运行时有异常。
EchoClient.java
追答
你把代码改一下,
Socket connection=new Socket(args[0],8000);
ServerSocket server=new ServerSocket(Integer.parseInt(args[0]));
改成 Socket connection=new Socket("127.0.0.1",8000);
ServerSocket server=new ServerSocket(8000);
试试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询