免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1548 | 回复: 4
打印 上一主题 下一主题

分享小弟就读英国大学-软件工程的JAVA功课 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-09 08:44 |只看该作者 |倒序浏览
分享小弟就读英国大学-软件工程的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 SERVERacket 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 SERVERacket SerialNo#" + sentence+" is recieved";
                              i++;                  
                    }while(!str.equals("CLOSE");      
                }
                  catch(IOException e)
                  {}
                 
        clientSocket.close();   
    }
}
-------------------------------------------------------------------

请大大多多指教,哪里需要改或要写多几个,请执教!

论坛徽章:
0
2 [报告]
发表于 2004-12-09 14:05 |只看该作者

分享小弟就读英国大学-软件工程的JAVA功课

在Server段应该解决的问题是返回ACK和测试收到所有packet的完整性。
在Client段应该解决的问题是怎样处理一个Server的反馈和等待是否超时的问题。
在测试程序的时候,还要在Server加一个随机的延时来模拟客户端等待超时的情况,而在Client段要模拟发送packet丢失,让Server返回packet miss 的情况,我觉得这些是程序代码应该加进去的。

论坛徽章:
0
3 [报告]
发表于 2004-12-09 23:23 |只看该作者

分享小弟就读英国大学-软件工程的JAVA功课

怎样加,在哪加呢? 加些啥呢? 请指教!

论坛徽章:
0
4 [报告]
发表于 2004-12-12 09:06 |只看该作者

分享小弟就读英国大学-软件工程的JAVA功课

忧闷? 没人指教啊?

论坛徽章:
0
5 [报告]
发表于 2004-12-13 13:52 |只看该作者

分享小弟就读英国大学-软件工程的JAVA功课

对于这个coureswork我觉得楼主最好是自己完成,老师的目的并不是考住谁,而是要学生明白如何运用TCP来发送包,而且lab6的答案也已经给得相当清楚。虽然今天就要交作业了,不过因该还有时间。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP