也发送文件的Java聊天程序
我做了一个基本的聊天程序,它也发送,它的不完整。一切运行良好,除了当我发送一个文件,我的程序收到它没有任何问题,但它提供了一个套接字关闭错误。也发送文件的Java聊天程序
CLIENT
import java.net.*;
import java.io.*;
public class chatClnt {
public static int PORT = 6666;
public static String IP_ADDR = "192.168.15.1";
public static String F = "FILE MODE";
public static void main(String[] ar) {
try {
InetAddress ipAddress = InetAddress.getByName(IP_ADDR);
System.out.println("\nconnecting......");
Socket socket = new Socket(ipAddress, PORT);
System.out.println("\nconnected");
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
String fname = null;
while (true) {
System.out.println("\nYou(Plain Text):>");
line = keyboard.readLine();
out.writeUTF(line);
out.flush();
if (line.equals(F)) {
//Sending File
OutputStream fout = socket.getOutputStream();
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
System.out.println("Enter the path: ");
fname = keyboard.readLine();
File myFile = new File(fname);
byte[] mybytearray = new byte[(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
fout = socket.getOutputStream();
System.out.println("Sending " + fname + "(" + mybytearray.length + " bytes)");
fout.write(mybytearray,0,mybytearray.length);
fout.flush();
System.out.println("Done.");
} finally {
if (bis != null) bis.close();
if (fout != null) fout.close();
}
line = in.readUTF(); // wait for the server to send a line of text.
System.out.println("\nSender(Plain Text):> " + line);
System.out.println();
}
}
} catch(Exception x) {
x.printStackTrace();
}
}
}
服务器
import java.net.*;
import java.io.*;
public class chatSrv {
public static int PORT = 6666;
public static String F = "FILE MODE";
public final static String FILE_TO_RECEIVE = "received.txt";
public final static int FILE_SIZE = 999999999;
public static void main(String[] ar) {
try {
ServerSocket ss = new ServerSocket(PORT);
System.out.println("\nconnecting...");
Socket socket = ss.accept();
System.out.println("\nconnected");
System.out.println();
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
int bytesRead;
int current = 0;
while (true) {
line = in.readUTF();
//Receiving File
if (line.equals(F)) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
byte [] mybytearray = new byte [FILE_SIZE];
fos = new FileOutputStream(FILE_TO_RECEIVE);
bos = new BufferedOutputStream(fos);
bytesRead = sin.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead = sin.read(mybytearray, current, (mybytearray.length-current));
if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("File " + FILE_TO_RECEIVE+ " downloaded (" + current + " bytes read)");
} finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
}
System.out.println("\nSender(Plain Text):> " + line);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nYou(Plain Text):>");
line = keyboard.readLine();
out.writeUTF(line);
out.flush();
System.out.println();
}
}
} catch(Exception x) {
x.printStackTrace();
}
}
}
当您关闭套接字的输出流,该关闭套接字为好。 documentation说:
关闭返回的
OutputStream
将关闭相关的套接字。
所以你不能关闭流。但是,如果你不关闭它,你将永远不会在接收端获得-1
。
因此,基本上,您不能使用从磁盘读取文件时使用的相同读取过程。您需要设计一个协议,使您可以在保持流打开的同时读取文件。可能的策略:
- 为此打开第二个套接字。例如,在可用端口上打开一个套接字,然后向服务器发送一条消息,告知它应该使用此端口号连接到另一个套接字。然后你可以通过另一个套接字发送整个文件并关闭它。或者让服务器打开第二个套接字并告诉您要连接的端口号。这是ftp协议使用的策略。
- 预先以字节为单位发送文件的长度,在接收端读取多个字节。这是HTTP使用的策略,例如,当它发送
Content-length:
标题时。
请记住,文件可能会很长,所以如果你决定发送的长度,使用long
而非int
。
请仔细请帮助我更精确一点点 –
如果我不关闭'OutputStream'程序不会前进,我应该怎么做 –
@SiddharthMazumdar正如我所说的,你不能只是决定不关闭它 - 因为你永远不会在另一端得到-1,它只会等待更多的输入。相反,您应该根据其中一种策略重新考虑您的协议。 – RealSkeptic
你可以请发布实际的错误?也是次要的编码样式注释,当您导入时尝试导入特定的包而不是'import java.net。*'。这是一个很好的习惯。 – wiredniko
remove fout.close();您正在永久运行while循环,并在收到一次hte文件后关闭,这意味着在单个文件之后流被关闭,因此它将无法再从该流中读取数据 –
关于删除' f.out.close()'会挂起程序,因为文件永远不会关闭 –