import java.io.*;
import java.net.*;
public class ASCIIClient{
public static void main(String[] args){
int i = 1;
try{
Socket s = new Socket("127.0.0.1",9545);
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.writeInt(i);
while( i < 128){
i++;
//i = (i + 1) % 128;
String s1 = in.readUTF();
out.writeInt(i);
System.out.println("Client receives : " + s1);
}
in.close();
out.close();
s.close();
}catch(IOException e){}
}
}
------解决思路----------------------
String s1 = in.readUTF();这句如果读不到或者读不全会抛出EOFException,继承自IOException。
这样就直接跳到catch中了,把close()语句放到finally中试试。别的没看出什么错误。