patternjavaMinor
Java multithreaded file server and client - emulate TCP over UDP
Viewed 0 times
fileandtcpudpjavaclientserverovermultithreadedemulate
Problem
My task was similar to my last assignment but this time I had to do it with UDP instead of TCP. This basically means I had to
Multithreading was an interesting problem as I had to simulate TCP's 3-way handshake.
I decided to use encapsulation and break up the file I'm transferring into 512 byte size blocks, except for the last block which is more likely going to be smaller. I then create my message object with the correct
Every segment received is acknowledged, after a 2x second timeout if there is no ACK then the segment is resent. In the event an ACK fails to get through the receiver will get a segment ID that is exactly -1 to the expected ID. In this way I know an ACK has failed and I resend it.
This has been tested locally using the built in fail simulator (2% chance a packet fails) and tested in the wild by asking a friend set the server up in London, England and I downloaded a 14.4mb file to my machine in Galway, Ireland. It took a long time and had 35 failed packets, 19 of which were failed ACKs.
Both the
```
/*
* CT326 - Assignment 12 - c.loughnane1@nuigalway.ie - 09101916
*/
package pkg12;
import java.io.*;
import java.net.*;
public class Server
{
private static int clientID = 1;
private static DatagramSocket serverSocket;
public static void main(String[] args) throws IOException
{
System.out.println("Server started.");
byte[] buffer = new byte[512];
/**
* ASSIGNMENT INSTRUCTION The server should be multi-threaded, and
* have one thread per connection.
*/
serverSocket = new DatagramSocket(8550);
while (true)
{
try
{
DatagramPacke
emulate TCP over UDP.Multithreading was an interesting problem as I had to simulate TCP's 3-way handshake.
I decided to use encapsulation and break up the file I'm transferring into 512 byte size blocks, except for the last block which is more likely going to be smaller. I then create my message object with the correct
segmentID and a bytesToWrite. The bytesToWrite is needed to handle the last block, there is a better way to do this. I just got lazy here. I'll fix that when I have time.Every segment received is acknowledged, after a 2x second timeout if there is no ACK then the segment is resent. In the event an ACK fails to get through the receiver will get a segment ID that is exactly -1 to the expected ID. In this way I know an ACK has failed and I resend it.
This has been tested locally using the built in fail simulator (2% chance a packet fails) and tested in the wild by asking a friend set the server up in London, England and I downloaded a 14.4mb file to my machine in Galway, Ireland. It took a long time and had 35 failed packets, 19 of which were failed ACKs.
Both the
Server and Client use the UDPFileReceiver and UDPFileSender classes.Server```
/*
* CT326 - Assignment 12 - c.loughnane1@nuigalway.ie - 09101916
*/
package pkg12;
import java.io.*;
import java.net.*;
public class Server
{
private static int clientID = 1;
private static DatagramSocket serverSocket;
public static void main(String[] args) throws IOException
{
System.out.println("Server started.");
byte[] buffer = new byte[512];
/**
* ASSIGNMENT INSTRUCTION The server should be multi-threaded, and
* have one thread per connection.
*/
serverSocket = new DatagramSocket(8550);
while (true)
{
try
{
DatagramPacke
Solution
-
You shouldn't do work in the constructor let alone ALL the work. You can move everything except
from
-
There are unused fields, e.g.
should heed compiler warnings.
You shouldn't do work in the constructor let alone ALL the work. You can move everything except
this.socket = socket;from
UDPFileReceiver constructor to a method receive(), like the sendFile method of UDPFileSender.-
There are unused fields, e.g.
buffer from CLIENTConnection. Youshould heed compiler warnings.
Context
StackExchange Code Review Q#23088, answer score: 3
Revisions (0)
No revisions yet.