我正在创建一个TCP文件发送者。 TCP服务器运行得很好,但是客户端获得了EOFException
。如何解决该异常?我还授予了运行时权限,并将权限写入manifest.xml。
这是服务器代码:
ServerSocket ss = new ServerSocket(8899);
System.out.println("Searching...");
Socket soc = ss.accept();
System.out.println("Connected");
DataOutputStream dos = new DataOutputStream(soc.getOutputStream());
String filepath = "C:\\Users\\Nitesh Rathi\\Downloads\\Garena-v2.0.exe";
file = new File(filepath);
System.out.println("File Selected : " + file.getName());
fis = new FileInputStream(file);
fn = file.getName();
fl = (int) file.length();
b = new byte[fl];
System.out.println("Sending file...");
dos.writeUTF(fn);
dos.writeInt(fl);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(b, 0, b.length);
bis.close();
dos.write(b, 0, b.length);
dos.flush();
dos.close();
soc.close();
ss.close();
System.out.println("file send");
这是我的客户代码:
try
{
Socket soc = new Socket("192.168.1.6", 8899);
str = "Status : connected";
DataInputStream dis = new DataInputStream(soc.getInputStream());
String fn = dis.readUTF();
int fl = dis.readInt();
File file = new File(Environment.getExternalStorageDirectory() + "/" + fn);
if (!file.exists())
file.createNewFile();
str = "Status : File Receiving...";
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[fl];
dis.readFully(b);
dis.close();
fos.write(b);
fos.flush();
fos.close();
soc.close();
str = "Status : File Received!";
}
catch (Exception e)
{
err = e.toString();
}