UDP组播文件名到对等体,使用TCP在对等体中发送/接收找到的文件

时间:2017-09-20 10:35:55

标签: java sockets p2p file-sharing

我在p2p做大学任务。我需要完成的是创建和加入组播组,并使用UDP从 peer1向其他对等方发送文件名请求,并在其他对等方查找文件,并从对等方发送/接收文件(比如peer2) )使用TCP进行peer1并使每个对等客户端/服务器尝试使用以下代码但是它说地址已经在使用和IO异常,请帮帮我

主要方法对等

<requiredProperty key="artifactId" />

类发送udp请求并下载文件

    import java.net.*;
import java.io.*;

public class Peer {
    public static void main (String args[])
    {
        // we need to give following argument
        //1. destination multicast group and port (e.g. 228.5.6.7 8888)
        //2. the peer's ID (e.g. PPP1)
        MulticastSocket ms = null;
        try {
            //joining the multicastgroup
            InetAddress group = InetAddress.getByName(args[0]);
            int port = Integer.parseInt(args[1]);
            String peerId = new String(args[2]);
            ms =  new MulticastSocket(port);
            ms.joinGroup(group);

            //now utilizing multitheread services for receive and send
            new PeerService2(ms, peerId);
            new PeerService1(ms, group, port, peerId);
        } catch (SocketException e){System.out.println("Socket: " + e.getMessage());
    }catch (IOException e){System.out.println("IO: " + e.getMessage());}
    }

}

用于处理接收udp请求和发送文件的类

import java.io.*;
import java.net.*;
import java.util.*;
import javax.rmi.CORBA.Util;



public class PeerService1 extends Thread {

    MulticastSocket ms = null;
    InetAddress group = null;
    int port = 8888;
    String peerId;
    public BufferedReader stdin;

    public PeerService1(MulticastSocket ms, InetAddress group, int port, String peerId) {
        this.ms = ms;
        this.group = group;
        this.port = port;
        this.peerId = peerId; 

        this.start();
    }

    public void run()
    {
         String filename = null;
         stdin = new BufferedReader(new InputStreamReader(System.in));
         while (true) {  
             System.err.print("Enter the File Name:");
             try {
                 filename = stdin.readLine();
                 filename = new String(filename);
             byte [] f = filename.getBytes();
             DatagramPacket messageOut = new DatagramPacket(f, f.length, group, port);
             ms.send(messageOut);
             download(group, port, filename);

             } catch (SocketException ex) {System.out.println("Socket:" + ex.getMessage());
             }
             catch (IOException ex) {
                 System.out.println("IO:" + ex.getMessage());
             }    
        }
    }  
    public void download (InetAddress group, int port, String filename) throws IOException
    {
        Socket socket = new Socket(""+group+"", port);
        DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
        dOut.writeUTF(filename);
        InputStream in = socket.getInputStream();
        System.out.println(in);

        String dwnfile = filename + "-download";

        OutputStream out = new FileOutputStream(dwnfile);
        System.out.println("The file of" + filename + "was found. Downloaded and saved as" + dwnfile);
        dOut.close();
        out.close();
        in.close();
        socket.close();



    }
}

1 个答案:

答案 0 :(得分:0)

以防万一你仍然没有找到任何解决方案。 您想在从对等方请求文件后创建ServerSocket。如果任何对等方找到文件,则peer将从数据报包中获取套接字地址以连接到服务器套接字。
伪码:
PeerService1:
 1.请求文件的组播消息
 2.请求创建ServerSocket并等待任何客户端连接回发送文件。 (如果没有客户端返回文件,请不要忘记在服务器套接字上添加超时。)

PeerService2:
 1.接收多播数据包并获取文件名  2.在文件系统中查找文件  3.如果找到文件,则从数据报包中获取请求对等套接字地址并连接回发送文件。你应该在这里创建一个Socket
提示: - 别忘了关闭溪流和插座。你不希望同伴在套接字上等待 我所写的内容可能没什么错。我赶时间。 :/ 评论是否有任何不明确的地方。如果需要,我会发布代码示例。