大家给些思路,题目是这样的。
用JAVA SOCKET 搞个服务器端程序,客户端程序。
现在都可以互联,输入数据然后再服务器端显示。老师希望用实验证明。
TCP
o Connection oriented – the same arrival order of packets?
o Byte streaming – no data overwritten?
o Reliable – no lost packet?
UPD
o Connectionless – different arrival order of packets?
o Block transfer – old packets overwritten?
o Non-reliable transfer – lost packets?
大家给小弟些思路,感激不尽。(100分)

解决方案 »

  1.   

    LZ可以用自己的机器作为服务器测试
    改题目有些类似聊天程序,只是没有在通过服务器转发到其他客户端
    下面提供一些代码,Sever端的代码
    import java.io.IOException;
    import java.net.*;
    import java.io.*;
    public class ChatSever { public static void main(String[] args) {

    boolean started = false;

    ServerSocket ss = null;
    Socket s = null;

    DataInputStream dis = null;

    try{
    ss = new ServerSocket(8888);
    }catch(BindException e){
    System.out.println("端口使用中");
    }catch(IOException e){
    e.printStackTrace();
    }
    try{
    started = true;
    while(started){
    boolean connected = false;
    s = ss.accept();
    connected = true;
    System.out.println("a client connected");
    while(connected){
    dis = new DataInputStream(s.getInputStream());
    String str = dis.readUTF();
    System.out.println(str);
    }
    }

    }catch(EOFException e){
    System.out.println("client close");
    }catch(IOException e){
    e.printStackTrace();
    }finally{
    try {
    s.close();
    dis.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }
    }}
      

  2.   

    Client端的代码如下
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.net.*;
    import java.io.*;public class ChatClient extends Frame{
    DataOutputStream dos = null;

    Socket s =null;

    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();

    public static void main(String []args){
    new ChatClient().launchFrame();
    }

    public void launchFrame(){
    setLocation(400,300);
    setSize(300,300); 
    add(tfTxt,BorderLayout.SOUTH);
    add(taContent,BorderLayout.NORTH);
    pack();
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) {
    disconnect();
    System.exit(0);
    }

    });
    tfTxt.addActionListener(new TFListener());
    setVisible(true);
    connect();


    public void connect(){
    try {
     s = new Socket("127.0.0.1",8888);
     dos  = new DataOutputStream(s.getOutputStream());
    System.out.println("Connected");
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void disconnect(){
    try {
    dos.close();
    s.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    private class TFListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    String str = tfTxt.getText();
    taContent.setText(str);
    tfTxt.setText("");
    try {

    dos.writeUTF(str);
    dos.flush();
    // dos.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }

    }
    }

    }
    测试时,先run Sever端,再run Client端就可以了
      

  3.   

    package chatqq;import java.net.*;
    import java.io.*;
    public class ClientThread extends Thread{//接Server 数据
        private PrintWriter out = null;
        private Socket s = null;
        private BufferedReader in;
        private String str=null;
        private QQClientFrame QQ=null;
        public ClientThread(Socket s,QQClientFrame QQ){
            this.s = s;
            this.QQ=QQ;
        }
        public void run(){
            try {
                    in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    out = new PrintWriter(s.getOutputStream(),true);
                    out.println(str);
                    while(true){
                        str=in.readLine();
                        if(str.equals("bye")){
                            str="client from Server1"+str;
                            out.println(str);
                            QQ.txtInfoShow.append(str+"\n");
                            break;
                        }else{
                            str="client from Server2"+str;
                            out.println(str);
                            QQ.txtInfoShow.append(str+"\n");
                        }
                        this.stop();
                    }
                    in.close();
                    out.close();
                    s.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }    }}
    --------------------------------------
    package qqserver;
    import java.net.*;
    import java.io.*;
    public class oneServerThread extends Thread{//发送数据
        private Socket s;
        private QQServerFrame QQ;
        public oneServerThread(Socket s,QQServerFrame QQ) {
            this.s=s;
            this.QQ=QQ;
        }
        public void run(){
            try{
                String info=QQ.txtUserSend.getText();
                BufferedReader in=new BufferedReader(new InputStreamReader(System.in,info));
                PrintWriter out=new PrintWriter(s.getOutputStream());
                while(true){
                    String str=in.readLine();
                    out.println(str);
                    QQ.txtInfoShow.append(str+"\n");
                    if(str.equals("bye")){
                        break;
                    }
                }
             in.close();
             out.close();
            }catch(Exception se){
                se.printStackTrace();
            }
        }
    }
    --------------------------------
      

  4.   

    CSDN怎么搞的,页面的字体怎么变成这样?
    看不清楚
      

  5.   

    让你用实验对比TCP和UDP的3个区别:
    1、TCP是面向连接的(发包和收包的次序一致):连续从客户端按序号发包,比如从1...N,服务器端收包,如果发现连续的2个包的序号不连续,则打印提示信息。一般来说,TCP的报文次序一定是一致的,而UDP的报文次序有可能(是“有可能”)会不一致。
    2、TCP是字节流传输,而UDP是块传输。--这个不知道怎么试。
    3、TCP是可靠的,而UDP则可能丢包。在传输的过程中拔掉网线,对比以下结果。