• 网络编程Practices


    1: Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?

    Server

    public class Server {
        private ServerSocket ss;
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;
    
        public Server() {
            try {
                ss = new ServerSocket(10000);
                while (true) {
                    socket = ss.accept();
                    String RemoteIP = socket.getInetAddress().getHostAddress();
                    String RemotePort = ":" + socket.getLocalPort();
                    System.out.println("A client come in!IP:" + RemoteIP + RemotePort);
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String line = in.readLine();
                    System.out.println("Cleint send is :" + line);
                    out = new PrintWriter(socket.getOutputStream(), true);
                    out.println("Your Message Received!");
                    out.close();
                    in.close();
                    socket.close();
                }
            } catch (IOException e) {
                out.println("wrong");
            }
        }
    View Code

    Client

    public class Client {
        Socket socket;
        BufferedReader in;
        PrintWriter out;
    
        public Client() {
            try {
                System.out.println("Try to Connect to 127.0.0.1:10000");
                socket = new Socket("127.0.0.1", 10000);
                System.out.println("The Server Connected!");
                System.out.println("Please enter some Character:");
                BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(socket.getOutputStream(), true);
                out.println(line.readLine());
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println(in.readLine());
                out.close();
                in.close();
                socket.close();
            } catch (IOException e) {
                out.println("Wrong");
            }
        }
    
        public static void main(String[] args) {
            new Client();
        }
    };
    View Code

    2: 编写一个服务器和一个客户端

    1)服务器可以读取本地文件内容并能将内容发给请求的客户端
    2)再编写一个可以发请求到服务器,并能从服务器段获取文件内容的客户端
        @Test
        public void TcpTestOneClient(){
    
            Socket socket = null;
            InputStream is = null;  // 获取服务端信息
    //        ByteArrayOutputStream bos = null;
            FileOutputStream fos = null;
            try {
                socket = new Socket("127.0.0.1",1001);
                is = socket.getInputStream();
                int len;
                byte [] bytes = new byte[5];
    //            bos = new ByteArrayOutputStream();
                fos = new FileOutputStream("a.TcpOne.jpg");
                while ((len = is.read(bytes)) != -1){
    //                bos.write(bytes,0,len);
                    fos.write(bytes,0,len);
                }
    //            System.out.println(bos.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(fos != null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
    //            if(bos != null){
    //                try {
    //                    bos.close();
    //                } catch (IOException e) {
    //                    e.printStackTrace();
    //                }
    //            }
            }
        }
    
        @Test
        public void TcpTestOneServer(){
            ServerSocket serverSocket = null;
            Socket socket = null;
            OutputStream os = null;
            FileInputStream fis = null;
            try {
                serverSocket = new ServerSocket(1001);
                socket = serverSocket.accept();
                os = socket.getOutputStream();
                int len;
                byte [] bytes = new byte[1024];
                fis = new FileInputStream(new File("a.jpg"));
                while ((len = fis.read(bytes)) != -1){
                    os.write(bytes,0,len);
    //                os.write("老王我给你发了一个图片  你好好欣赏啊".getBytes());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(serverSocket != null){
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(os != null){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    服务端向客户端发送文件

    3: 编写一个简单的浏览器

    1)将第一题的服务器做调整,如果客户端传输字符串: GET HTTP1.0  /index.jsp, 服务器会将指定目录(路径)下的index.jsp文件传输到客户端
    2)客户端可以发请求和把上面的文件保存到本机的制定目录下

    4: 编写一个服务器和一个客户端

    1)服务器上保存了几个用户名和对应的密码;且能验证客户端发送过来的用户名和密码,是否和保存的某个用户名和对应的密码一致.
    2)客户端能连接到服务器,并把用户在键盘上输入的用户名和密码,发送到服务器上

    5:编写一个ATM服务器和一个客户端,可以通过客户端输入帐号,密码,然后利用服务器端验证,且能实现ATM存钱,取钱和修改密码的功能将上面的题目结合JDBC实现模拟银行的功能。(后面讲jdbc)

    网络编程
    1)    用网络编程编写一个服务端
    2)    编写一个客户端,客户端向服务端发送一条信息
    3)    服务端在这条信息前面加上“服务端:”后再返给客户端输出。

    6: 网络聊天功能(50分)

    1)    用网络编程编写一个服务端
    2)    编写一个客户端,客户端向服务端发送一条信息
    3)    服务端在这条信息前面加上“服务端:”后再返给客户端输出。
    网络聊天功能(50分)
    1)    用网络编程编写一个服务端(5分)
    2)    用网络编写一个客户端(5分)
    3)    客户端输入 client,服务端响应 I'SERVER!(40分)
    实现服务器端和客户端的单线通话(50分)
    1)    实例化一个服务器端(10分)
    2)    实例化一个客户端(10分)
    3)    服务器端和客户端相连接(10分)
    4)    一旦服务器端接收到客户端的连接,服务器端就向客户端输出“welcom”(20分)

    TCP/IP通信协议和IO

    1)   实例化一个服务端(10分)

    2)   实例化一个客户端,客户端可以向服务端发送一条信息(10分)

    3)   用IO向D盘下面创建temp.txt文件(10分)

    4)   服务端把接收到客户端的信息写到temp.txt文件中(20分)

    .

  • 相关阅读:
    无限维
    黎曼流形
    why we need virtual key word
    TOJ 4119 Split Equally
    TOJ 4003 Next Permutation
    TOJ 4002 Palindrome Generator
    TOJ 2749 Absent Substrings
    TOJ 2641 Gene
    TOJ 2861 Octal Fractions
    TOJ 4394 Rebuild Road
  • 原文地址:https://www.cnblogs.com/zhaoyunlong/p/13256315.html
Copyright © 2020-2023  润新知