- 论坛徽章:
- 0
|
分享小弟就读英国大学-软件工程的JAVA功课,让大家一起讨论一下:
小弟已经写了差不多了,不知对不对,请大家给出宝贵的建议哦!
这功课的内容要求如下:
This coursework is based on the Java labs regarding client server communication model. You have to implement two clients each with only one thread of execution and a server with two threads of execution.
Implement a flow control protocol for TCP connections according to the following description for a fixed number of data packets (say 4 from each client):
Each client sends a packet to the server and waits until it receives an acknowledgements (ACK) or timeout occurs. Upon receiving an ACK, the client sends the next packet from the remaining packets. If timeout expires or negative acknowledgement (NACK) is received for a packet, the client retransmits that packet.
The server sends ACK upon successful receipt of a packet or a NACK for a missing packet. The server sorts out the packets if received out of order before delivering them.
Design a packet with two fields, a header (containing sequence numbers) and a data part containing a string.
Develop Client and Server programs to implement this flow control protocol for TCP connections (described above).
Write a method in Client program that randomly causes delay in sending packets or miss out a packet.
Test your program with the following scenario:
1. When you generate the packets swap the numbers in two of the headers so
that they apparently arrive out of sequence.
2. Miss out a packet when sending (but keep it on the send side) to test the
algorithm.
小弟的解答如下:(不完全,请大家积极讨论与修改意见)
-------------------------
Packet.java
-----------------------
import java.io.*;
public class Packet implements Serializable
{
private int serialNo;
private String data;
public Packet(int serialNo, String data)
{
this.serialNo = serialNo;
this.data = data;
}
public String getData()
{
return data;
}
public int getSerialNo()
{
return serialNo;
}
}
------------------------------------------------------
TCPMultiThreadPacketServer.java
-------------------------------------------
import java.io.*;
import java.net.*;
public class TCPMultiThreadPacketServer
{
private static ServerSocket welcomeSocket;
private static final int PORT = 1234;
private static int clientNo =0;
public static void main(String argv[]) throws Exception
{
System.out.println("Opening port...\n" ;
try
{
welcomeSocket = new ServerSocket(PORT);
}
catch (IOException e)
{}
do
{
Socket client = welcomeSocket.accept();
System.out.println("\nNew client accepted.\n" ;
//Create a thread to handle communication with
//this client and pass the constructor for this
//thread a reference to the relevant socket...
TCPMultiThreadPacketServer.ClientHandler handler =
new TCPMultiThreadPacketServer().new ClientHandler(client,clientNo);
handler.start(); //this method calls run.
clientNo++;
}while (true);
}
class ClientHandler extends Thread
{
private Socket client;
private ObjectOutputStream outToClient;
private ObjectInputStream inFromClient;
public int clientNo;
//public boolean stopping;
public ClientHandler(Socket socket, int clientNos)
{
//Set up reference to associated socket...
client = socket;
clientNo= clientNos;
try
{
outToClient = new ObjectOutputStream(client.getOutputStream());
inFromClient = new ObjectInputStream(client.getInputStream());
}
catch(IOException e)
{}
}
public void run()
{
try
{
Packet packet;
Thread mythread = Thread.currentThread();
while(( packet = (Packet)inFromClient.readObject())!=null)
{
int x = packet.getSerialNo();
Integer myx = new Integer(x);
String str = myx.toString();
System.out.println("Recieving From Client Packet's serialNo#" + str +" \n and packet's Data is " + packet.getData());
outToClient.writeObject(str);
if(packet.getData().equals("CLOSE" )
{
if (mythread.activeCount() == 2 && (clientNo ==0 || clientNo >;0))
System.exit(0);
else
break;
}
}
client.close();
}
catch(IOException e)
{}
catch(ClassNotFoundException e)
{}
}
}
}
--------------------------------------------------------------
TCPPacketClientThread1.java
-----------------------------------------
import java.io.*;
import java.net.*;
class TCPPacketClientThread1
{
public static void main(String argv[]) throws Exception
{
int i=1;
String str="";
String sentence;
Socket clientSocket = new Socket("dcsun2",1234);
ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
try
{
do
{
System.out.print("Enter the data packet: " ;
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
str =(String)br.readLine();
Packet myPacket = new Packet(i,str);
outToServer.writeObject(myPacket);
sentence = (String)inFromServer.readObject();
System.out.println("FROM SERVER acket SerialNo#" + sentence+" is recieved" ;
i++;
}while(!str.equals("CLOSE" );
}
catch(IOException e)
{}
clientSocket.close();
}
}
-----------------------------------------
TCPPacketClientThread2.java
-----------------------------------------
import java.io.*;
import java.net.*;
class TCPPacketClientThread2
{
public static void main(String argv[]) throws Exception
{
int i=1;
String str="";
String sentence;
Socket clientSocket = new Socket("dcsun2",1234);
ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
try
{
do
{
System.out.print("Enter the data packet: " ;
BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
str =(String)br.readLine();
Packet myPacket = new Packet(i,str);
outToServer.writeObject(myPacket);
sentence = (String)inFromServer.readObject();
System.out.println("FROM SERVER acket SerialNo#" + sentence+" is recieved" ;
i++;
}while(!str.equals("CLOSE" );
}
catch(IOException e)
{}
clientSocket.close();
}
}
-------------------------------------------------------------------
请大大多多指教,哪里需要改或要写多几个,请执教! |
|