Site Loader
Get a Quote
Rock Street, San Francisco

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;

class UDPClient {
//Max packet size set to 1024 bytes
public static int udpPacketSize = 1024;
//main client function
public static void main(String args) throws Exception {
try {
//creates a file input stream to take in the image
FileInputStream FIS = new FileInputStream(“ex3.BMP”);
FileChannel FC = FIS.getChannel();
ByteBuffer BB = ByteBuffer.allocate((int) FC.size());
//reads the bytes and places them in the buffer
FC.read(BB);
FC.close();
//closes the file input stream
FIS.close();
BB.rewind();
rdt_send(BB);
//catches an exception and prints it to the user
} catch (IOException e) {
System.out.println(“Error:” + e);
}
}
//function for sending packets to the server
public static void udt_send(byte sendData) {
try {
DatagramSocket cSocket = new DatagramSocket();
InetAddress IP = InetAddress.getByName(“localhost”);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IP, 5000);
cSocket.send(sendPacket);
cSocket.close();
} catch (IOException e) {
System.out.println(“Error:” + e);
}
}
//breaks up the packets and sends them to the server using udt_send
public static void rdt_send(ByteBuffer data) {
try {
byte dataPacket = new byteudpPacketSize;
while(true) {
data.get(dataPacket);
byte packet = Make_Packet(dataPacket);
udt_send(packet);
}
} catch (BufferUnderflowException e) {
byte dataPacketEnd = new bytedata.remaining();
data.get(dataPacketEnd);
byte packetLast = Make_Packet(dataPacketEnd);
udt_send(packetLast);
}
}

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

public static byte Make_Packet (byte data) {
byte packet = new bytedata.length;
packet = data;
return packet;
}
}

Post Author: admin

x

Hi!
I'm Victoria

Would you like to get a custom essay? How about receiving a customized one?

Check it out
x

Hi!
I'm Jeremy!

Would you like to get a custom essay? How about receiving a customized one?

Check it out