Created
October 25, 2015 20:13
-
-
Save anonymous/ad52b60bb88d73dde2c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package experiment2; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.net.*; // we use Sockets | |
import java.nio.file.Paths; | |
public class FileTransferClientUDPjlibcnds { | |
public static void main(String args[]) throws Exception{ | |
// Arguments: Server name & port & filename to transfer | |
String srvName = args[0]; // server Name | |
int srvPort = Integer.parseInt(args[1]); // server UDP port | |
String filename = Paths.get(args[2]).toAbsolutePath().toString(); | |
// Open special datagramm socket from jlibcnds library, do not change this | |
javax.net.DatagramSocket dtgSock; | |
dtgSock = new javax.net.DatagramSocket(); | |
InetSocketAddress srvSockAddr = new InetSocketAddress(srvName, srvPort); | |
dtgSock.connect(srvSockAddr); | |
byte[] buf = new byte[8]; | |
java.io.FileInputStream fr; | |
try { | |
fr = new java.io.FileInputStream(new File(filename)); | |
int len; // number of bytes written from the file | |
while ((len=fr.read(buf,0,buf.length))!=-1){ | |
DatagramPacket packet = new DatagramPacket(buf, len); | |
dtgSock.send(packet); | |
System.out.print("*"); | |
Thread.sleep(100); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
finally { | |
DatagramPacket packet = new DatagramPacket(buf, 0); // Send an empty packet to the server to indicate end of file | |
dtgSock.send(packet); | |
dtgSock.close(); // Close the Socket | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment