• java socket 客户端和服务端通信


    1.采用阻塞式readUTF();长连接

    2.java socket的3个主要方面如下

      1)accept 阻塞,直到接受到一个连接,并返回一个客户端对象实例

      2)getInputStream()

      3)getOutputStream()

    客户端代码:

    public class ClientSocket {
        
        public static final String IP="10.100.63.18";
        public static final int PORT=667;
        
        public static void main(String[] args){
            
            System.out.println("客户端启动...");
            
            while(true){
                Socket socket=null;
                
                try{
                    socket=new Socket(IP,PORT);
            //        socket.setSoTimeout(2000);
                            
                    DataOutputStream out=new DataOutputStream(socket.getOutputStream());
                    String send="Hello";
                    out.writeUTF(send);//客户端发送到服务端
            //        Thread.sleep(2000);
                    DataInputStream in=new DataInputStream(socket.getInputStream());
                    System.out.println("客户端接收到:"+in.readUTF());
    //            
                    out.close();
                    in.close();
                }catch(Exception e){
                    System.out.println("客户端异常");
                }finally{
                    if(socket!=null){
                        
                        try{
                            socket.close();
                        }catch(IOException e){}
                    }
                }
            }
        }
    }

    服务端代码:

    public class Server {
    
        public static int PORT=667;
        
        public static void main(String[] args){
            
            System.out.println("服务器启动...");
            Server server=new Server();
            server.init();
        
        }
        
        private void init() {
            // TODO Auto-generated method stub
            ServerSocket sock = null;
            try{
                sock=new ServerSocket(PORT);
                while(true){
                    
                    Socket Client=sock.accept();
                
                    new HandlerThread(Client);
                    
                }
            }catch(Exception e){
                
            }finally{
                if(sock!=null){
                try {
                    sock.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
            }
        }
    
        private class HandlerThread implements Runnable{
            private Socket sc;
            public HandlerThread(Socket client){
            
                sc=client;
    //            try {
    //                sc.setSoTimeout(1000);
    //            } catch (SocketException e) {
    //                // TODO Auto-generated catch block
    //                e.printStackTrace();
    //            }
                new Thread(this).start();
            }
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try{
                    
                    DataInputStream input=new DataInputStream(sc.getInputStream());
                    System.out.println("处理客户端数据:"+input.readUTF());
                    DataOutputStream output=new DataOutputStream(sc.getOutputStream());
                
                    String s="Hello,I have received your message";
                    output.writeUTF(s);
                    output.close();
                    input.close();
                    
                }catch(EOFException e){
                    System.out.println("服务器EOFException异常");
                }catch(IOException e){
                    System.out.println("服务器EOFException异常");
                }finally{
                    if(sc!=null){
                        try{
                            sc.close();
                        }catch(Exception e){
                            sc=null;
                            
                        }
                    }
                }
            }} 
    }

     运行结果:(先运行server,在运行client)

    处理客户端数据:Hello
    处理客户端数据:Hello
    
    
    客户端接收到:Hello,I have received your message
    客户端接收到:Hello,I have received your message
  • 相关阅读:
    实线矢量元素提取
    matlab写txt文件
    matlab之boundary()函数
    matlab之flipud()函数
    matlab unique()函数
    KD-tree
    matlab之细胞数组
    matlab的代码注释
    matlab中的try...catch...end
    (转)MySQL 加锁处理分析
  • 原文地址:https://www.cnblogs.com/zhengtu2015/p/4882876.html
Copyright © 2020-2023  润新知