如何(无需每次重新连接和关闭套接字)在Android或java中从套接字接收许多文件
目前,我的服务器程序能够从客户端套接字接收文件,并能够将接收到的文件保存在服务器机器中。 但是我需要在每次不关闭和打开套接字连接的情况下从客户端套接字接收到服务器套接字的许多文件 我已经写了代码,这是工作良好。但在本文中,我将在每次迭代中关闭和打开服务器和客户端套接字连接。但我需要做到这一点,而不是每次都连接和断开两个插座 请引导我的长者如何(无需每次重新连接和关闭套接字)在Android或java中从套接字接收许多文件,java,sockets,Java,Sockets,目前,我的服务器程序能够从客户端套接字接收文件,并能够将接收到的文件保存在服务器机器中。 但是我需要在每次不关闭和打开套接字连接的情况下从客户端套接字接收到服务器套接字的许多文件 我已经写了代码,这是工作良好。但在本文中,我将在每次迭代中关闭和打开服务器和客户端套接字连接。但我需要做到这一点,而不是每次都连接和断开两个插座 请引导我的长者 我的服务器代码: int img_count=1; int bytesRead; int current =
我的服务器代码:
int img_count=1;
int bytesRead;
int current = 0;
byte [] mybytearray = new byte [100000];
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket sock=null;
// create socket
ServerSocket servsock = new ServerSocket(6668);
System.out.println("Waiting... for client req");
int i=0;
for ( i=0; i<9; i++)
{
sock = servsock.accept(); // Waiting for Client
String fname = "Image000"+(img_count++)+".JPG";
String fpath = "C:/RX_images/"+fname; // Image saving path
File myFile = new File (fpath);
is = sock.getInputStream();
fos = new FileOutputStream(myFile);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
fos.flush();
fos.close();
bos.close();
is.close();
sock.close();
} // End of for loop
servsock.close();
System.out.println("Received : "+ (i++)+ " Images");
int img_count=1;
int字节读取;
int电流=0;
字节[]mybytearray=新字节[100000];
InputStream=null;
FileOutputStream=null;
BufferedOutputStream bos=null;
套接字sock=null;
//创建套接字
ServerSocket servsock=新的ServerSocket(6668);
System.out.println(“等待客户请求”);
int i=0;
对于(i=0;i=0)电流+=字节读取;
}while(bytesRead>-1);
写入(mybytearray,0,当前);
bos.flush();
fos.flush();
fos.close();
bos.close();
is.close();
sock.close();
}//for循环结束
servsock.close();
System.out.println(“接收:”+(i++)+“图像”);
我的客户代码:
int i=0;
int img_count=1;
FileInputStream fis=null;
BufferedInputStream bis=null;
OutputStream os=null;
Socket client=null;
System.out.println("Sending...");
for ( i=0; i<9; i++)
{
client = new Socket("192.168.1.54",6668);
String fname = "Image000"+(img_count++)+".JPG";
String fpath = "C:/Tx_Images/"+fname; // Image path
File myFile = new File (fpath);
byte [] mybytearray = new byte [(int)myFile.length()];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
os = client.getOutputStream();
os.write(mybytearray,0,mybytearray.length);
bis.close();
fis.close();
os.flush();
os.close();
client.close();
Thread.sleep(2000);
} // End of for loop
System.out.println("\n Sent : "+(i++)+" Images");
inti=0;
int img_计数=1;
FileInputStream fis=null;
BufferedInputStream bis=null;
OutputStream os=null;
socketclient=null;
System.out.println(“发送…”);
对于(i=0;i由于套接字只是一个字节流,为了处理多个文件,您必须构造某种简单协议。换句话说,发送方必须发送区分一个文件中的字节和另一个文件中的字节的字节。因为您发送的是二进制数据,所以没有字节序列您可以发送到“mark”的s开始和/或/或结束-例如,如果在结尾发送4个零字节,则可能是数据,因此接收者无法确定它是标记还是数据。立即想到两种处理方法-将文件拆分为最多N个字节的节,然后一次发送一个节。您必须有一个by的计数每个节中的字节数,因为至少有一个节的字节数与所有其他节的字节数不同。或者,y可以计算文件中的字节数,并从给出该计数的字节开始,以便接收方知道预期的字节数。在给出计数的同时,还可以提供名称和类型等信息这个问题实际上取决于您是否需要客户端保持连接打开。通常,您只需要保持服务器端侦听,客户端每次需要发送文件时都可以重新连接
使用ExecutorService
保持服务器端运行,并使用单独的线程处理多个连接。然后让客户端连接并发送需要发送和断开连接的内容。有关快速示例,请参阅此问题:
另外,在这个例子中,看看他们是如何关闭资源(最终)并停止服务器的。这与您的问题无关,但您也希望使I/O和错误处理更加健壮
如果确实需要服务器和客户端保持连接并发送多个文件(或任何数据)然后,你需要像Rbook所说的那样实现某种协议,你需要更深入地研究网络并拥有心跳信号等。即使你这样做,客户端仍然需要足够智能,以便在套接字关闭时尝试重新连接,等等。只需制作简单的协议,如:
File Name\r\n
File Size\r\n
File Data\r\n
File Name\r\n
File Size\r\n
File Data\r\n
....
我希望您能理解这一点。您可以先发送文件信息,然后服务器将解析此文件信息,并使您的服务器读取文件信息中指定的字节数。这将使您能够看到文件结束标记以及何时开始创建新文件。但您必须先知道文件大小
这不适用于长度未知的数据流
让您的服务器读取要指定的字节数,这样服务器就可以知道何时结束文件写入并开始新文件,或者在套接字关闭之前文件是否已完全接收…最简单的协议是在实际数据之前发送每个文件的长度。使用DataInputStream和DataOutputStreamUse可以轻松完成此操作HTTP,它处理文件大小、内容类型等。嗨,artbristol,谢谢回复。你能编辑并给出我的上述代码吗..使用HTTP。对不起,Meraman,我试过了。但我没有得到输出,你能编辑并给出我的上述代码吗?嗨,Meraman,谢谢回复。你能编辑并给出我的上述代码吗..至少10个文件吗