如何在Java中使用RUDP创建简单的服务器客户端应用程序?

时间:2014-06-01 18:54:52

标签: java sockets udp udpclient rudp

我正在开发一个简单的应用程序,使用UDP在两台机器之间传输文件,但结果是有损且不可靠,所以在搜索Internet时我发现这个项目名为Simple Reliable UDP here,但是没有任何文档或任何示例代码。所以,如果有任何人可以帮助我使用这些代码,我将不胜感激,因为我是Java的新手。我开始编写简单的服务器客户端应用程序,但我得到的地址已绑定异常。为了明确我只想使用UDP连接,这就是为什么我要尝试实现ReliableServerSocketReliableSocket

  package stackoverflow;

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import net.rudp.ReliableServerSocket;
  import net.rudp.ReliableSocket;

  /**
  *
  * @author Nika
  */
  public class udpServer implements Runnable{
  ReliableServerSocket rss;
  ///ocket rs;
  ReliableSocket rs;
  public udpServer() throws IOException {
  rss= new ReliableServerSocket(9876);
  }
  public void run(){
  while (true){
    try {
        rs=(ReliableSocket)rss.accept();
        System.out.println("Connection Accepted");
        System.out.println(""+rs.getInetAddress());
        BufferedReader  inReader = new BufferedReader (new InputStreamReader (rs.getInputStream()));
    //BufferedWriter  outReader = new BufferedWriter (new OutputStreamWriter (rs.getOutputStream()));

        String str= ""+inReader.readLine();

        if(str.contains("UPLOAD")){
            System.out.println("Client wants to upload file");
        }else if(str.contains("D1")){
            System.out.println("Client wants to download file");
       }



    } catch (IOException ex) {
        Logger.getLogger(udpServer.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  }

      public static void main(String args[]) throws Exception  
   {
      System.out.println("UDP Server Executed");
     Thread t= new Thread( new udpServer());
             t.start();
   }


 }

客户端代码

      package stackoverflow;

      import java.io.BufferedWriter;
      import java.io.IOException;
      import java.io.OutputStreamWriter;
      import net.rudp.ReliableSocket;

      /**
      *
      * @author Nika
      */
     public class UdpFileClient {

     BufferedWriter outReader;
     ReliableSocket server;

    public UdpFileClient(boolean b1, boolean b2) throws IOException {

    if (b1) {
        server = new ReliableSocket("127.0.0.1", 9876);

        outReader = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
        outReader.write("D1");
        System.out.println("Download Req Sent From Client");
        server.close();
        outReader.flush();
        outReader.close();
    }

    if (b2) {
        server = new ReliableSocket("127.0.0.1", 9876);
        outReader = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
        outReader.write("UPLOAD");
        System.out.println("Upload Req Sent From Client");
        server.close();
        outReader.flush();
        outReader.close();
    }

}

public static void main(String args[]) throws Exception {
    System.out.println("UDP CLient Executed");
    new UdpFileClient(true, true);
}

}

我已经知道我可以使用TCP / IP,但项目要求使用UDP。如果使用UDP以无损方式发送文件的任何其他方式也会有所帮助。

提前致谢!!

1 个答案:

答案 0 :(得分:3)

我试过RUDP并发现我没有打印输出,我知道这是一个愚蠢的错误。

UDP客户端       包UDPClient;

  import java.io.IOException;
  import java.io.InputStream;
  import java.net.InetSocketAddress;
  import net.rudp.ReliableSocket;

  /**
  *
  * @author Nika
  */
  public class UDPtestc {
  ReliableSocket server;
   public UDPtestc() throws IOException {
     server = new ReliableSocket();
            server.connect(new InetSocketAddress("127.0.0.1", 9876));
    byte[] buffer = new byte[1024];
    int count,progress=0;
    InputStream in = server.getInputStream();
    while((count=in.read(buffer)) >0){
    progress+=count;
    System.out.println(""+progress);
    }
    server.close();            
    }

    public static void main(String[] args) throws IOException {
    new UDPtestc();
    }

    }

<强> UDPserver

  package UDPServer;

  import java.io.FileInputStream; 
  import java.io.IOException;
  import java.io.OutputStream;
  import java.io.RandomAccessFile;
  import java.net.Socket;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import net.rudp.ReliableServerSocket;
  import net.rudp.ReliableSocket;

  /**
  *
  * @author Nika
  */
  public class UDPtests implements Runnable {

  ReliableServerSocket rss;
  ReliableSocket rs;
  String file;

  FileInputStream bin;

  public UDPtests() throws IOException {
    rss = new ReliableServerSocket(9876);
    Thread serverthread = new Thread(this);
    serverthread.start();

   }

   public void run() {
    while (true) {
        try {
            rs =  (ReliableSocket)rss.accept();
            System.out.println("Connection Accepted");
            System.out.println("" + rs.getRemoteSocketAddress());

              file = "";
        Long size=0L;
             file += "10MB.txt";
                size+=10*1024*1024;

        RandomAccessFile r1= new RandomAccessFile(file,"rw");
        r1.setLength(size);
        byte[] sendData = new byte[1024];

        OutputStream os = rs.getOutputStream();
        //FileOutputStream wr = new FileOutputStream(new File(file));
        bin= new FileInputStream(file);
        int bytesReceived = 0;
        int progress = 0;
        while ((bytesReceived = bin.read(sendData)) > 0) {
            /* Write to the file */
            os.write(sendData, 0, bytesReceived);
            progress += bytesReceived;
            System.out.println(""+progress);
        }



        } catch (IOException ex) {
            Logger.getLogger(udpServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

public static void main(String[] args) throws IOException {
    new UDPtests();
}

}

如果有可能,我很快就会在RUDP上发布其他内容。