socket编程java中的数据包丢失
我正在尝试将文件从客户端发送到服务器。下面是我尝试过的代码。但有时,在传输过程中会出现数据包丢失。我不确定我错在哪里 服务器端代码:socket编程java中的数据包丢失,java,sockets,Java,Sockets,我正在尝试将文件从客户端发送到服务器。下面是我尝试过的代码。但有时,在传输过程中会出现数据包丢失。我不确定我错在哪里 服务器端代码: public static void ReadAndWrite(byte[] aByte, Socket clientSocket, InputStream inputStream, String fileOutput) throws FileNotFoundException, IOExceptio
public static void ReadAndWrite(byte[] aByte, Socket clientSocket,
InputStream inputStream, String fileOutput)
throws FileNotFoundException, IOException {
int bytesRead;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try
{
fileOutputStream = new FileOutputStream( fileOutput );
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(aByte, 0, aByte.length);
System.out.println("The length is "+bytesRead);
int count = 0;
do {
count++;
byteArrayOutputStream.write(aByte);
bytesRead = inputStream.read(aByte);
} while (bytesRead != -1);
System.out.println("The count is "+count);
System.out.println("The length is "+byteArrayOutputStream.size());
bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
bufferedOutputStream.flush();
bufferedOutputStream.close();
clientSocket.close();
}
catch(Exception ex)
{
Logger.writeLog(ex,Listen.class.getName(), LogType.EXCEPTION);
throw ex;
}
public void readByteArrayAndWriteToClientSocket(
Socket connectionSocket, BufferedOutputStream outToClient, String fileToSend ) throws Exception
{
try{
if (outToClient != null)
{
File myFile = new File(fileToSend);
System.out.println(myFile.length());
byte[] byteArray = new byte[(int) myFile.length()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myFile);
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try {
bufferedInputStream.read(byteArray, 0, byteArray.length);
outToClient.write(byteArray, 0, byteArray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
return;
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
}
}catch (Exception e) {
Logger.writeLog(e, getClass().getName(), LogType.EXCEPTION);
throw e;
}
}
客户端代码:
public static void ReadAndWrite(byte[] aByte, Socket clientSocket,
InputStream inputStream, String fileOutput)
throws FileNotFoundException, IOException {
int bytesRead;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try
{
fileOutputStream = new FileOutputStream( fileOutput );
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bytesRead = inputStream.read(aByte, 0, aByte.length);
System.out.println("The length is "+bytesRead);
int count = 0;
do {
count++;
byteArrayOutputStream.write(aByte);
bytesRead = inputStream.read(aByte);
} while (bytesRead != -1);
System.out.println("The count is "+count);
System.out.println("The length is "+byteArrayOutputStream.size());
bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
bufferedOutputStream.flush();
bufferedOutputStream.close();
clientSocket.close();
}
catch(Exception ex)
{
Logger.writeLog(ex,Listen.class.getName(), LogType.EXCEPTION);
throw ex;
}
public void readByteArrayAndWriteToClientSocket(
Socket connectionSocket, BufferedOutputStream outToClient, String fileToSend ) throws Exception
{
try{
if (outToClient != null)
{
File myFile = new File(fileToSend);
System.out.println(myFile.length());
byte[] byteArray = new byte[(int) myFile.length()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myFile);
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try {
bufferedInputStream.read(byteArray, 0, byteArray.length);
outToClient.write(byteArray, 0, byteArray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
return;
} catch (IOException ex) {
Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
throw ex;
}
}
}catch (Exception e) {
Logger.writeLog(e, getClass().getName(), LogType.EXCEPTION);
throw e;
}
}
sockets read方法将在其获得您请求的所有字节时返回,或者在其停止从网络接收数据时返回 由于在任何实际网络中传输都经常中断,因此您需要不断发出读取调用,直到获得所需的字节数为止 您需要这样的代码:
char [] buffer = new char[1024];
int expect = 1000;
int sofar = 0;
int chars_read;
try
{
while((chars_read = from_server.read(buffer[sofar])) != -1)
{
sofar = sofar + chars_read;
if (sofar >= expected) break;
}
}
catch(IOException e)
{
to_user.println(e);
}
sockets read方法将在其获得您请求的所有字节时返回,或者在其停止从网络接收数据时返回 由于在任何实际网络中传输都经常中断,因此您需要不断发出读取调用,直到获得所需的字节数为止 您需要这样的代码:
char [] buffer = new char[1024];
int expect = 1000;
int sofar = 0;
int chars_read;
try
{
while((chars_read = from_server.read(buffer[sofar])) != -1)
{
sofar = sofar + chars_read;
if (sofar >= expected) break;
}
}
catch(IOException e)
{
to_user.println(e);
}
没有“数据包丢失”,只是代码中有bug Java中复制流的规范方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
如果您事先知道字节数,并且发送方必须在传输后保持连接打开,则它将变为:
while (total < expected && (count = in.read(buffer, 0, expected-total > buffer.length ? buffer.length : (int)(expected-total))) > 0)
{
out.write(buffer, 0, count);
total += count;
}
while(totalbuffer.length?buffer.length:(int)(expected total))>0)
{
out.write(缓冲区,0,计数);
总数+=计数;
}
忘记所有的
字节数组输入/输出流
和额外的副本。只需从文件读取并发送到套接字,或从套接字读取并写入文件。没有“数据包丢失”,只是代码中有错误
Java中复制流的规范方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
如果您事先知道字节数,并且发送方必须在传输后保持连接打开,则它将变为:
while (total < expected && (count = in.read(buffer, 0, expected-total > buffer.length ? buffer.length : (int)(expected-total))) > 0)
{
out.write(buffer, 0, count);
total += count;
}
while(totalbuffer.length?buffer.length:(int)(expected total))>0)
{
out.write(缓冲区,0,计数);
总数+=计数;
}
忘记所有的
字节数组输入/输出流
和额外的副本。只需从文件中读取并发送到套接字,或从套接字中读取并写入文件。代码看起来正常。如果你区分这些文件,它们有什么不同?你说的“数据包丢失”是什么意思?部分数据没有收到?没有收到任何数据?或者用Wireshark之类的东西检查数据包,会发现数据包被丢弃?偶尔的数据包丢失在任何有损的线路上都是不可避免的;TCP的设计考虑到了这一点,并内置了恢复机制,以确保交付(如果可能)并保证顺序和完整性。您不应在发送文件后立即关闭套接字。让它等待几秒钟后再终止。这将确保服务器实际接收文件。@extremecorders:这绝对不是Java套接字的工作方式。如果是的话,你怎么知道要等多久才能关闭?我正在尝试传输一个3500 KB的ZIP文件。但传输的文件只有3450 KB或3495 KB。我很少得到完整的文件。代码看起来还可以。如果你区分这些文件,它们有什么不同?你说的“数据包丢失”是什么意思?部分数据没有收到?没有收到任何数据?或者用Wireshark之类的东西检查数据包,会发现数据包被丢弃?偶尔的数据包丢失在任何有损的线路上都是不可避免的;TCP的设计考虑到了这一点,并内置了恢复机制,以确保交付(如果可能)并保证顺序和完整性。您不应在发送文件后立即关闭套接字。让它等待几秒钟后再终止。这将确保服务器实际接收文件。@extremecorders:这绝对不是Java套接字的工作方式。如果是的话,你怎么知道要等多久才能关闭?我正在尝试传输一个3500 KB的ZIP文件。但传输的文件只有3450 KB或3495 KB。我很少得到完整的文件。这里的关键是不要忽略read
的返回值read(buf,off,len)
不保证读取len
字节;它可以读得更少。无论哪种情况,它都返回实际读取的字节数。这适用于从文件读取和从套接字读取。Guava的行为方式与OP期望的read
工作方式相同,尽管ByTestStreams.toByteArray()
更简单。如何将其与现有代码相适应。我不明白。@James:我用你的代码替换了我的代码。仍然面对着同样的问题issue@All字体但是每次我做对了,当我第二次做的时候。只有第一次出现数据包丢失。我想知道如何…这甚至都没有编译。没有read(char)
方法。缓冲区应该是字节[]
,而不是字符[]。
这里的关键是不要忽略读取的返回值read(buf,off,len)
不保证读取len
字节;它可以读得更少。无论哪种情况,它都返回实际读取的字节数。这适用于从文件读取和从套接字读取。Guava的行为方式与OP期望的read
工作方式相同,尽管ByTestStreams.toByteArray()
更简单。如何将其与现有代码相适应。我不明白。@James:我用你的代码替换了我的代码。仍然面对着同样的问题issue@All字体但是每次我做对了,当我第二次做的时候。只有第一次出现数据包丢失。我想知道如何…这甚至都没有编译。没有read(char)
方法。缓冲区应该是字节[]
,而不是字符[]。