UDP Socket Programming - File transfer

// Socket Programing – Datagram Sockets
// Client Server Communication
// File Transfer

// Server Program

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


public class UDPFileServer
{
     public static void main(String as[]) throws IOException
     {
          DatagramSocket ds;
          DatagramPacket dp_send;
          DatagramPacket dp_rec;
         
          final int buf_size = 512;
          final int port = 1500;
         
          byte msg_rec[] = new byte[buf_size];
          byte msg_send[] = new byte[buf_size];
         
          try
          {
              ds = new DatagramSocket(port);
             
              dp_rec = new DatagramPacket(msg_rec, buf_size);
              ds.receive(dp_rec);
                  
              String filename = new String(dp_rec.getData()).trim();
                            
              InetAddress DA = dp_rec.getAddress();
              int destport = dp_rec.getPort();
                 
              System.out.println("Requested file : "+filename);  
             
              FileInputStream fin = new FileInputStream(filename);
              int i = 0;
              while(true)
              {
                   int ch = fin.read();
                   if(ch==-1)
                        break;
                   msg_send[i]=(byte)ch;
                   i++;
                  
              }
             
              fin.close();
             
            dp_send = new DatagramPacket(msg_send, msg_send.length, DA, destport);
              ds.send(dp_send);
             
          }
          catch(Exception e)
          {
              System.out.println("Exception " + e);
          }
     }
}




// Socket Programing – Datagram Sockets
// Client Server Communication
// File Transfer

// Client Program

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

public class UDPFileClient
{
     public static void main(String as[]) throws IOException
     {
          DatagramSocket ds;
          DatagramPacket dp_send;
          DatagramPacket dp_rec;
         
          final int port = 1500;
          final int buf_size = 512;
         
          Scanner s = new Scanner(System.in);
         
          byte[] msg_send = new byte[buf_size];    
          byte[] msg_rec = new byte[buf_size];
             
          try
          {
              ds = new DatagramSocket();
              System.out.print("Enter file name : ");
              String data = s.nextLine();
             
              msg_send = data.getBytes();
              InetAddress DA = InetAddress.getByName("localhost");
             
              dp_send = new DatagramPacket(msg_send, msg_send.length, DA, port);
              ds.send(dp_send);
             
              dp_rec = new DatagramPacket(msg_rec, buf_size);
              ds.receive(dp_rec);
                  
              String filedata = new String(dp_rec.getData()).trim();
              System.out.println(filedata);
         
          }
          catch(Exception e)
          {
              System.out.println("Exception " + e);
          }
     }
}
          

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu