【问题标题】:Sending file to unknown client address through socket?通过套接字将文件发送到未知的客户端地址?
【发布时间】:2014-07-22 00:03:23
【问题描述】:

基本上我想要做的是让客户端(具有未知 IP)通过套接字连接到服务器,并让服务器每隔 x 秒将 BufferedImage 发送回客户端。

我了解如何使用已知的客户端 IP 来完成此操作,而不是未知的客户端 IP。一个简单的例子会很棒,谢谢。

已知 IP 的示例:

BufferedImage scr = getImage();
Socket sock = new Socket(ip, 123456); //unknown IP
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(scr);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();

另外,如果有人能告诉我我可以在哪里循环以继续发送文件,那就太棒了。

【问题讨论】:

  • 必须知道服务器 IP 才能连接。客户端连接到已知的服务器IP后,就可以将信息传递给客户端了。
  • 1.如果您不知道 IP,则无法寻址数据包。 2. 为什么客户端连接服务器后不知道IP?
  • 服务器IP已知。正在连接的客户端不是。
  • @user3613821 我认为你搞错了整个服务器客户端的工作方式。在客户端尝试连接之前,服务器不知道客户端。客户端知道它需要连接到哪里,而服务器正在监听连接。
  • 你能说明你将如何使用已知的客户地址吗?也许只需要做一个小改动。

标签: java sockets fileoutputstream


【解决方案1】:

我创建了一些示例服务器客户端连接。基本上,您只需定义您的服务器以使用您的本地主机,然后根据您的网络配置进行端口转发或打开服务器端口。网上有很多关于这个的教程。在您的客户端上,您需要知道您的外部或内部 IP 地址,具体取决于您的连接位置。

此示例仅使用您的本地主机并从您的硬盘驱动器发送文件,但我专门编写了它以添加任何 InputStreamOutputStream 以便您可以将其调整为读取或写入图像。大多数服务器您只需将您的 IP 地址绑定到 127.0.0.1。当连接到本地网络之外的服务器时,您需要找到您的外部 IP 地址。您可以在 whatsmyip.org 等网站上找到它。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class SendFileExample {
    public static void main(String [] args) throws IOException {
        System.out.print("Type 's' for server or 'c' for client: ");

        char c = (char) System.in.read();
        if(Character.toLowerCase(c) == 's') {
            createServer();
        } else if(Character.toLowerCase(c) == 'c') {
            createClient();
        }
    }

    public static void createServer() throws IOException {
        // create a server to listen on port 12345
        ServerSocket socket = new ServerSocket(12345, 0, InetAddress.getByName("127.0.0.1"));
        System.out.println("Server started on " + socket.getInetAddress().getHostAddress() + ":" + socket.getLocalPort() + ",\nWaiting for client to connect.");
        Socket clientConnection = socket.accept();
        System.out.println("Client accepted from "+clientConnection.getInetAddress().getHostAddress()+", sending file");
        pipeStreams(new FileInputStream(new File("c:\\from.txt")), clientConnection.getOutputStream(), 1024);
        System.out.println("File sent, closing out connection");
        clientConnection.close();
        socket.close();
    }

    public static void createClient() throws IOException {
        System.out.println("Connecting to server.");
        Socket socket = new Socket();
        // connect to an address, this is the server address (which you have to know)
        socket.connect(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 12345));
        // read all bytes from the socket
        System.out.println("Success, retreiving file.");
        pipeStreams(socket.getInputStream(), new FileOutputStream(new File("c:\\to.txt")), 1024);
        System.out.println("Done, file sent. Closing connection");
        socket.close();
    }


    /**
     * writes all bytes from inputStream to outputStream
     * @param source
     * @param out
     * @throws IOException
     */
    public static void pipeStreams(java.io.InputStream source, java.io.OutputStream destination, int bufferSize) throws IOException {

        // 16kb buffer
        byte [] buffer = new byte[bufferSize];
        int read = 0;
        while((read=source.read(buffer)) != -1) {
            destination.write(buffer, 0, read);
        }
        destination.flush();
        destination.close();
        source.close();
    }
}

【讨论】:

    【解决方案2】:

    我认为实现此目的最简洁的方法是让客户端每隔 x 秒连接一次服务器,然后从套接字的流中提取图像。

    如果您希望服务器拥有主动权,请让客户端连接到服务器,然后保持套接字打开以每隔 x 秒发送一次图像。这意味着客户端必须准备好读取图像。这也意味着图像的长度必须在其内容之前发送,因为图像不会在流的末尾终止。

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2014-12-01
      • 2010-09-15
      • 1970-01-01
      • 2019-08-08
      • 2019-04-05
      • 1970-01-01
      • 2019-01-01
      • 2016-03-03
      相关资源
      最近更新 更多