zoukankan      html  css  js  c++  java
  • Tom猫2.0-Socket+Thread

    • Client
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    public class TestSockClient {
        public static void main(String[] args) {
            DataInputStream dis = null;
            DataOutputStream dos = null;
            Socket socket = null;
            Scanner scan = null;
            String  sendStr;
            String receiveStr;
            try {
                socket = new Socket("localhost", 8888);
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
                scan = new Scanner(System.in);
                do {
                    System.out.print("我:");
                    sendStr = scan.nextLine();
                    dos.writeUTF(sendStr);
                    if ((receiveStr = dis.readUTF()) != null) {
                        System.out.println(receiveStr);
                    }
                } while (!sendStr.equals("88"));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    dis.close();
                    dos.close();
                    socket.close();
                    scan.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • Server
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestSockServer {
        public static void main(String[] args) {
            ServerSocket server = null;
            Socket socket = null;
            List<ClientService> cslist = new ArrayList<>();
            try{
                 //开门营业
                server = new ServerSocket(8888);
                System.out.println("服务器已开启");
                while(true){
                    //顾客来
                    socket = server.accept();
                    //给分配服务员
                    ClientService cs = new TestSockServer().new ClientService(socket);
                    //记录分配情况
                    cslist.add(cs);
                     //开始服务
                    cs.start();
                }
            } catch(IOException e){
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try{
                    socket.close();
                    server.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
         class ClientService extends Thread{
            Socket socket;
            public ClientService(Socket socket) {
                this.socket = socket;
            }
            @Override
            public void run() {
                DataOutputStream dos = null;
                DataInputStream dis = null;
                try{
                    dos = new DataOutputStream(socket.getOutputStream());
                    dis = new DataInputStream(socket.getInputStream());
                    String receiveStr ;
                    while((receiveStr = dis.readUTF())!= null) {
                        System.out.println(socket.getPort()+"客户端发送:"+receiveStr);
                        dos.writeUTF(socket.getPort()+":"+receiveStr);
                    }
                }catch(EOFException e) {
                    System.out.println(socket.getPort()+"客户端结束服务");
                }catch(IOException e){
                    e.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                finally{
                    try{
                        dos.close();
                        dis.close();
                        socket.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 运行结果


    ljm要加油
  • 相关阅读:
    ElasticSearch Index API && Mapping
    jQuery静态方法type使用和源码分析
    jQuery原型属性和方法总结
    jQuery原型方法first,last,eq,slice源码分析
    jQuery原型方法.pushStack源码分析
    解决浏览器窗口变小后右侧出现空白背景颜色或者图片不能全屏填充的方法
    js方法实现rgb颜色转换成16进制格式的代码的方法
    一个解决表单中的文字和文本区域(textarea)上对齐的方法
    jquery原型方法map的使用和源码分析
    jQuery原型方法each使用和源码分析
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14296506.html
Copyright © 2011-2022 走看看