java socket编程在两台机器之间发送和接收文件
我正在尝试使用套接字编程在两台机器之间进行通信 我基本上需要的是两台机器都应该能够发送和接收文件。我在下面粘贴的代码没有显示任何错误,但服务器端程序似乎正在无限期运行,也就是说,它没有终止。它卡在标有注释的行上,卡在这里 在这段代码中,最初,服务器发送名为“file.txt”的文件,客户端接收并保存名为“copy.txt”的文件。稍后,客户端将发送一个名为“file2.txt”的文件,服务器将接收并以“copy2.txt”的名称保存该文件 有人能告诉我错误并提出一些改进建议吗java socket编程在两台机器之间发送和接收文件,java,sockets,Java,Sockets,我正在尝试使用套接字编程在两台机器之间进行通信 我基本上需要的是两台机器都应该能够发送和接收文件。我在下面粘贴的代码没有显示任何错误,但服务器端程序似乎正在无限期运行,也就是说,它没有终止。它卡在标有注释的行上,卡在这里 在这段代码中,最初,服务器发送名为“file.txt”的文件,客户端接收并保存名为“copy.txt”的文件。稍后,客户端将发送一个名为“file2.txt”的文件,服务器将接收并以“copy2.txt”的名称保存该文件 有人能告诉我错误并提出一些改进建议吗 //server
//server side code
import java.net.*;
import java.io.*;
public class server
{
public static void main (String [] args ) throws IOException
{
//sending file started
ServerSocket serverSocket = new ServerSocket(16167);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
File transferFile = new File ("/Users/abhishek/desktop/file.txt");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
System.out.println("File transfer complete");
//socket.close();
//sending comleted
//receiving file started
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
byte [] bytearray1 = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy2.txt");
//fos.flush();
BufferedOutputStream bos = new BufferedOutputStream(fos);
//bos.flush();
System.out.println("not moving ahead!!!");//program stucked here
bytesRead = is.read(bytearray1,0,bytearray1.length);
currentTot = bytesRead;
System.out.println("current"+currentTot);
do
{
bytesRead = is.read(bytearray1, currentTot, (bytearray1.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
System.out.println("current"+currentTot);
} while(bytesRead > -1);
System.out.println("outside current"+currentTot);
bos.write(bytearray1, 0 , currentTot);
bos.flush();
//receiving complete
System.out.println("Receving file completed");
socket.close();
}
}
//client side code
import java.net.*;
import java.io.*;
public class client
{
public static void main (String [] args ) throws IOException
{
int filesize=1022386;
int bytesRead=0;
int currentTot = 0;
Socket socket = new Socket("localhost",16167);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do
{
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
System.out.println("current"+currentTot);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
System.out.println("receiving first file completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//sending file
System.out.println("sending second file started!");
File transferFile = new File ("/Users/abhishek/desktop/file2.txt");
byte [] bytearray2 = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray2,0,bytearray2.length);
OutputStream os = socket.getOutputStream();
os.flush();
os.write(bytearray2,0,bytearray2.length);
os.flush();
System.out.println("sending second file completed!");
//sending complete
socket.close();
}
}
我敢打赌
bytesRead=is.read(bytearray1,0,bytearray1.length)代码>
是你真正陷入困境的地方。如果您被困在这里,通常的问题是通信的另一端没有发送任何数据,没有可读取的内容,并且您的线程被困在等待它发送
在你的客户方面,你打电话
bos.close()代码>
在发送第一条消息之后。这将导致套接字也关闭,这将在服务器端抛出IOException,因为您没有捕获IOException,所以服务器程序将退出
你有多少插座经验?如果您刚刚开始使用套接字,您可能希望查看我围绕此编写的扩展,从和开始 是否执行System.out.println(“不前进!!!”//程序在此死机
行是否实际执行?如果是这样,那么问题是InputStream.read()函数是blobking函数;他们将停止执行程序(“块”),直到他们能够完成
从:
从输入流读取多达len个字节的数据到一个字节数组中。尝试读取多达len字节的数据,但可以读取较小的数据。实际读取的字节数作为整数返回。
此方法会一直阻止,直到输入数据可用、检测到文件结尾或引发异常为止。
由于您没有收到异常,这意味着当您调用.read()时,没有可读取的数据,并且您的程序坐在那里等待数据读取(数据永远不会到达)。您应该首先检查您的客户端程序是否确实发送了数据。这感觉应该放在评论中。@StackOverflowException我已经更新了这个,以回答所问的问题,并包括这个建议。是的,这就是我遇到的问题。我是第一次做socket编程,所以你能告诉我任何可以避免这种情况的方法吗。另外,请阅读我对@Nekojimi reply的评论,以便更好地理解我的问题。@StackOverflowException刚刚更新了我的答案。你的问题在于客户端调用bos.close()@user3245573:他指的是你。是的,这一行正在执行,我确信它停留在read(),因为没有可用的数据。但是,我正在从客户端发送数据(您可以检查客户端程序)。但是,我认为客户端没有完成读取第一个文件的数据(创建了一个名为“copy.txt”的文件,但其中没有数据)。因此,由于客户端没有完成第一个文件的读取部分,所以它无法为第二个文件发送数据。那么,我怎样才能避免这种情况呢?