日期:2014-05-20  浏览次数:20790 次

【新手求助】《Head First Java》简单Socket通信示例程序运行报错
《Head First Java》第15章介绍Socket通信时的一个示例程序DailyAdvice,包括客户端DailyAdviceClient,服务器DailyAdviceServer。编译通过,开两个命令行窗口,先运行DailyAdviceServer等待客户端的Socket请求,然后在另一个命令行窗口运行客户端DailyAdviceClient,报错:

D:\Code\Head First Java\chap15\DailyAdvice>java DailyAdviceClient
java.net.SocketException: Connection reset
  at java.net.SocketInputStream.read(SocketInputStream.java:168)
  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
  at java.io.InputStreamReader.read(InputStreamReader.java:167)
  at java.io.BufferedReader.fill(BufferedReader.java:136)
  at java.io.BufferedReader.readLine(BufferedReader.java:299)
  at java.io.BufferedReader.readLine(BufferedReader.java:362)
  at DailyAdviceClient.go(DailyAdviceClient.java:15)
  at DailyAdviceClient.main(DailyAdviceClient.java:7)

同时,服务器端报错:
D:\Code\Head First Java\chap15\DailyAdvice>java DailyAdviceServer
java.net.SocketException: select failed
  at java.net.PlainSocketImpl.socketAccept(Native Method)
  at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390)
  at java.net.ServerSocket.implAccept(ServerSocket.java:453)
  at java.net.ServerSocket.accept(ServerSocket.java:421)
  at DailyAdviceServer.go(DailyAdviceServer.java:11)
  at DailyAdviceServer.main(DailyAdviceServer.java:30)

奇怪的是,程序不做任何修改,在别人的机子上编译运行均正常。本机环境:Win7 64位,java version 1.6.0_21,防火墙已关闭

程序源码如下:

Server: DailyAdviceServer.java
Java code

import java.io.*;
import java.net.*;

public class DailyAdviceServer {
    String[] adviceList = {"Take smaller bites", "Go for the tight jeans. No they do NOT make you look fat.", "One word: inappropriate", "Just for today, be honest. Tell your boss what you *really* think", "you might want to rethink that haircut."};
    
    public void go() {
        try {
            ServerSocket serverSock = new ServerSocket(4242);
            while (true) {
                Socket sock = serverSock.accept();
                PrintWriter writer = new PrintWriter(sock.getOutputStream());
                String advice = getAdvice();
                writer.println(advice);
                writer.close();
                System.out.println(advice);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private String getAdvice() {
        int random = (int) (Math.random() * adviceList.length);
        return adviceList[random];
    }
    
    public static void main(String[] args) {
        DailyAdviceServer server = new DailyAdviceServer();
        server.go();
    }
}



Client: DailyAdviceClient.java
Java code

import java.io.*;
import java.net.*;

public class DailyAdviceClient {
    public static void main(String[] args) {
        DailyAdviceClient client = new DailyAdviceClient();
        client.go();
    }
    
    public void go() {
        try {
            Socket s = new Socket("127.0.0.1", 4242);
            InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
            BufferedReader reader = new BufferedReader(streamReader);
            String advice = reader.readLine();
            System.out.println("Today you should: " + advice);
            reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}