应该是红色部分有问题,好像是不让初始化Socket,这是什么原因呢
package socket;import java.io.*;
import java.net.*;
public class RemoteFileClient{
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
protected String hostIp;
protected int hostPort;
//构造方法
public RemoteFileClient(String hostIp,int hostPort)
{
this.hostIp=hostIp;this.hostPort=hostPort;
}
//向服务器请求文件的内容
public String getFile(String fileNameToGet)
{
StringBuffer fileLines=new StringBuffer();
try{
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line=null;
while((line=socketReader.readLine())!=null)
fileLines.append(line+"\n");
}
catch(IOException e){
System.out.println("Errorreadingfromfile:"+fileNameToGet);
}
return fileLines.toString();
}
//连接到远程服务器
public void setUpConnection(){
try{
Socket client=new Socket(hostIp,hostPort);
socketReader=new BufferedReader(new InputStreamReader(client.getInputStream()));
socketWriter=new PrintWriter(client.getOutputStream());
}
catch(UnknownHostException e){
System.out.println("Error1settingupsocketconnection:unknownhostat"+hostIp+":"+hostPort);
}
catch(IOException e){
System.out.println("Error2settingupsocketconnection:"+e);
}
}
//断开远程服务器
public void tearDownConnection(){
try{
socketWriter.close();
socketReader.close();
}
catch(IOException e){
System.out.println("Errortearingdownsocketconnection:"+e);
}
}
public static void main(String args[]){
RemoteFileClient remoteFileClient=new RemoteFileClient("127.0.0.1",8090);
remoteFileClient.setUpConnection();

StringBuffer fileContents=new StringBuffer();

fileContents.append(remoteFileClient.getFile("c:\\e.txt"));
//remoteFileClient.tearDownConnection();
System.out.println(fileContents);}
}

解决方案 »

  1.   

    Error2settingupsocketconnection:java.net.ConnectException: Connection refused: connect
    Exception in thread "main" java.lang.NullPointerException
    at socket.RemoteFileClient.getFile(RemoteFileClient.java:20)
    at socket.RemoteFileClient.main(RemoteFileClient.java:64)
      

  2.   

    错误指示:
    java.lang.NullPointerException
    at RemoteFileClient.getFile(RemoteFileClient.java:20)
    at RemoteFileClient.main(RemoteFileClient.java:61)
    Error2settingupsocketconnection:java.net.ConnectException: Connection refused: connect楼主误导人了啊 两个错误
    1.
    RemoteFileClient.java:20 -> socketWriter.println(fileNameToGet); 
    这里证明你的端口是打开了的 socketWriter 这个东西出错了...
    注释掉getFile(String fileNameToGet)
    里面的内容就发现这个错误没了..
    原因来至于22.
    Error2settingupsocketconnection:java.net.ConnectException: Connection refused: connect
    这是因为你的端口没打开而已
    new RemoteFileClient("127.0.0.1",8090); 
    这个地址不能随便用的 必须要有服务器你才能链接
    所以要测试要写服务器先
      

  3.   

    拷贝你的代码,没有报出你展示的错误,connection refused,那就是联结被拒绝,看看你的端口是否打开,127.0.0.1是否能被解析。
      

  4.   

    getFile(String fileNameToGet) 确定文件存在
    new RemoteFileClient("127.0.0.1",8090); 确定远程服务存在
      

  5.   

    我现在等于把我本地的电脑作为服务器不行吗?  我换了个端口8821    提示出现Mission Failed。
      

  6.   


    lz....
    TCP/IP 是一个点对点的协议...
    服务器必须在你指定的8821端口监听
    你的client才能连接的..你可以把自己电脑作为服务器。。前提是你要运行服务器的软件啊