Hi there,
I have this coursework to do and I don't know why my code is not working. Could you help, please? Many thanks.

Task 1
You are to develop a UDP server, and Client, capable of transferring a file (worth up to 60% of total available marks for this piece of coursework).
1. A server and client are to be developed in Java. The main requirement is what might be described as the implementation of a very simple stop- and-wait protocol. This protocol implies that at any stage during the server’s data transmission to a client, the server will send the next packet only after the last packet has been acknowledged.
2. On receiving a request from a client, the server is to send the first line of a file and then wait for an acknowledgement. When receipt of this has been acknowledged, the next line will be sent. This is repeated until the whole file has been sent. As it stands, the system is not very useful, and not very efficient
3. You should explain each line of code you think is important
4. You should state how you intend to test the code for correct functionality
5. You should provide screen-captures of the working programs
6. you should suggest a method of transmitting the data other than on a line-by-line basis
The server should display a client’s IP and Port number when a request is received and should indicate when each packet has been sent; the client should make a copy of the file that has been received in a file named “pg1012.txt”. The client should terminate gracefully when all data has been received.
In writing your report, you should consider various scenarios, such as: what should happen if an acknowledgement is not received by the server or if packets become corrupted, given that we are interested in reliable communications. You needn’t implement proposed ‘solutions’ to such potential; you are not being asked to model such scenarios, but you should show awareness of the use of retransmission timers, cumulative acknowledgements and sequence numbers (from the lectures on TCP). By addressing all such questions a reliable protocols based on UDP implement could be implemented!

SERVER

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


class Responder implements Runnable {

    DatagramSocket socket = null;
    DatagramPacket packet = null;

public Responder(DatagramSocket socket, DatagramPacket packet)
    {
        this.socket = socket;
        this.packet = packet;
    }
public void run()
	{
        byte[] data = new byte[66508]; // code not shown
        String message = new String("!");
       try
       	{
       	BufferedReader bufferedfilecopy = new BufferedReader(new InputStreamReader(new FileInputStream ("gutenberg.txt")));
       String line;
      while ((line = bufferedfilecopy.readLine()) != null)
      	 {
        DatagramPacket response = new DatagramPacket(line.getBytes(), line.length(),packet.getAddress() , packet.getPort());
       System.out.print(".");
    socket.send(response);
     	 }

    	 } catch (Exception e){;};
    }
}
class Server implements Runnable
 {
   			 public void run()
   	{
    byte buffer[] = new byte[66508];
    DatagramPacket getstartpacket = new DatagramPacket(buffer, buffer.length);
    try
     {
     DatagramSocket socket = new DatagramSocket(87);
    	    while (true)
    	{
           socket.receive(getstartpacket);
           (new Responder(socket, getstartpacket)).run();
        }
   	 }
    	catch (Exception e) {;};
	}
}
class  NewServer
 	{

		static public void main (String args[]) throws IOException

		{
		Runnable ns = new Server();
		ns.run();
		}
	}

CLIENT

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

class Fileclient
{
  public static void main(String args[]) throws IOException
{
    DatagramSocket socket = new DatagramSocket(1005);
    socket.setSoTimeout(2000);
    boolean received = false;

      String message = new String("!");
      DatagramPacket packet = new DatagramPacket( message.getBytes(), message.length(),
        InetAddress.getByName("127.0.0.1"), 87); //this socket is 'wired in'; specify on the cmd line?

      socket.send(packet);

      System.out.println(" Main Packet sent!");

       // THE CODE ABOVE EXTRACT SENT INFORMATION FORM ANOTHER SERVER....


      byte buffer[] = new byte[66508];
      boolean receiving = true;
      while (receiving) 
   {  // sure about this??
      try
      		 {
		      		DatagramPacket getbitoffile =  new DatagramPacket(buffer, buffer.length);
		        socket.receive(getbitoffile);

		        System.out.println(new String(getbitoffile.getData(),0, getbitoffile.getLength()));
		        System.out.print("!");

      	 	 }

      	 	 // the codes ontop will only display the object that was sent if it was receive and true....

      catch (SocketTimeoutException e) 
      	{
          receiving = false;
          System.out.println("Error. Taking Too Long...");//find out how to use setSOTimeout!!
        }

    } // end while true
	 // the codes ontop would send an error message when nothing was received
}
}

I have this coursework to do and I don't know why my code is not working

What is not working , post in brief. Are you getting an exception somewhere?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.