简体   繁体   English

Java关闭TCP / IP连接

[英]Java close TCP/IP connection

I need a java program to download a series of file from a web server and I need to be able to close and then reopen the tcp/ip connection between each file download. 我需要一个Java程序从Web服务器下载一系列文件,并且需要能够关闭然后重新打开每个文件下载之间的tcp / ip连接。

Not sure how to do this. 不确定如何执行此操作。

Here is a low-level way of retrieving any information from not only socket 80 (http) but generically to retrieve mail, do a 'telnet' etc: 这是一种不仅可以从套接字80(http)检索信息的低级方式,而且通常可以检索邮件,执行“ telnet”等操作:

socket = new Socket();
socket.connect(new InetSocketAddress(host, port));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

now to do a call to retrieve a page, send the get command, set the content-type, and send two '\\n'. 现在,要进行调用以检索页面,发送get命令,设置内容类型并发送两个'\\ n'。

There is a higher-level solution: Send HTTP GET request with header 有一个更高级别的解决方案: 发送带有标头的HTTP GET请求

您可以为要发送给它的IP和端口创建一个Socket ,然后使用FileInputStream将文件读入字节数组,并通过DataOutPutStream发送该字节数组,然后完成后,只需刷新dataOutPutStream并关闭Socket并调用该功能再次发送下一个文件。

use the logic: Please post something that you have tried. 使用逻辑:请发布您尝试过的内容。

 do{
      connection.open;
      while(!eof){
      fetchfiles();
      }
     connection.close;
     }while(no of files yet to download);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM