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);
}
}

Best services for writing your paper according to Trustpilot

Premium Partner
From $18.00 per page
4,8 / 5
4,80
Writers Experience
4,80
Delivery
4,90
Support
4,70
Price
Recommended Service
From $13.90 per page
4,6 / 5
4,70
Writers Experience
4,70
Delivery
4,60
Support
4,60
Price
From $20.00 per page
4,5 / 5
4,80
Writers Experience
4,50
Delivery
4,40
Support
4,10
Price
* All Partners were chosen among 50+ writing services by our Customer Satisfaction Team

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

Post Author: admin