import java.net.*;
import java.io.*;
//服务器端程序
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
byte [] b = new byte[1024];
int i = 0;
while(dis.readByte() != -1) {
b[i] = dis.readByte();
i++ ;
}
FileOutputStream fos = new FileOutputStream("c:/ClientData.txt");
fos.write(b);
fos.flush();
fos.close();
dis.close();
s.close();
}
}
//客户端程序
import java.net.*;
import java.io.*;public class Client
{
public static final String fileName = "c:" + File.separator + "TestClient.java"; public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",6666);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(fileName);
byte[] b = new byte[fis.available()];
int len = fis.read(b); //读取数据到数组b中,返回读到的字节数len
dos.write(b, 0, len-1);
dos.flush();
fis.close();
dos.close();
}
}/*单独运行Server.java和Client.java都不会报错,说明socket编程是没有问题的,但当运行Server.java后再运行Client.java抛出如下错误:
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at Server.main(Server.java:15)
*/我知道这是读取文件取到末尾的错误,但不知道怎么回事,Server.java:15(b[i] = dis.readByte();
)怎么会错呢?已经判断了文件结尾的啊(while(dis.readByte() != -1))!希望达人不吝赐教~
b[i] = dis.readByte();
i++ ;
}你while条件里读的那个Byte就不要了?
你在那里判断没到末尾,怎么能确定下一个Byte没到末尾呢?
而且你最起码套一个Buffer流吧,而且可以用read(byte[] b)这个方法读啊。你这样写效率太低。
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
FileOutputStream fos = new FileOutputStream("c:/ClientData.txt");
byte [] b = new byte[1024];
int i = 0;
while((i = dis.read(b)) != -1) {
fos.write(b);
}
// fos.write(b);
fos.flush();
fos.close();
dis.close();
s.close();
import java.io.DataInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;public class Toyqsshr {
boolean started = false;
ServerSocket ss = null; public static void main(String[] args) {
new Toyqsshr().start();
} public void start() {
try {
ss = new ServerSocket(50000);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} try {
while (started) {
Socket s = ss.accept();
Save2File r = new Save2File(s);
System.out.println(s.getInetAddress().toString() + " connected!"); new Thread(r).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (!ss.isClosed()) {
ss.close();
ss = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 保存到文件
*/
class Save2File implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private PrintWriter pw = null;
byte[] buffer = new byte[2000];
String fileName = "c:/clientData.txt"; public Save2File(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true/* 写入文件末尾 */)), true/* 自动刷新 */);
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void run() {
try {
while (true) {
int num = dis.read(buffer);
System.out.println("读取了" + num + "个字节");
for (int i = 0; i < num; i++) {
pw.printf("0x%02X ", buffer[i]);
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
dis = null;
}
if (pw != null) {
pw.close();
pw = null;
}
if (!s.isClosed()) {
s.close();
s = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (byte)(ch);
}
这是dis.readByte()调用的内部源码,楼主程序如果读到了末尾就肯定会抛出异常了
2.判断流的结尾是用 read()!=-1而不是readByte()!=-1
readByte()返回 -128~127的byte值,如果到达流的结尾则会抛出 EOFException
read()如果有内容时返回0~255,等效于readByte()&0xFF
无内容可读时返回 -1