zoukankan      html  css  js  c++  java
  • java聊天室二(客户端)

    package com.gu.socket.pro4;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    /**
     * 客户端可以发送数据+接受数据
     * 
     * @author 谷
     *
     */
    public class client {
        public static void main(String[] args) throws UnknownHostException, IOException {
            Socket soc=new Socket("localhost",9999);
            
            //写出数据
            new Thread(new send(soc)).start();;
            
            //接受数据
            new Thread(new receive(soc)).start();
            
            
        }
    
    }
    
    package com.gu.socket.pro4;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    
    public class receive implements Runnable {
        DataInputStream in; 
        private boolean isRunning=true;
        @Override
        public void run() {
            while(isRunning){
                System.out.println(receive());
            }
            
        }
        public receive (Socket soc){
            try {
                in= new DataInputStream(soc.getInputStream());
            } catch (IOException e) {
                isRunning=false;
                closeUtil.closeAll(in);
                
            }
        }
        
        public String receive(){
            String mess=null;
            try {
                mess=in.readUTF();
                
            } catch (IOException e) {
                isRunning=false;
                closeUtil.closeAll(in);
            
            }
            return mess;
        }
    
    }
    
    package com.gu.socket.pro4;
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    /**
     * 发送数据线程
     * @author 谷
     *
     */
    public class send implements Runnable{
        private BufferedReader console;
        private DataOutputStream out;
        private boolean isRunning=true;
        public send(){
            console=new BufferedReader(new InputStreamReader(System.in));
        }
        public send(Socket soc){
            this();
            try {
                out=new DataOutputStream(soc.getOutputStream());
            } catch (IOException e) {
                isRunning=false;
                closeUtil.closeAll(console,out);
                
            }
        }
        
        //接受控制台写入数据
        private String getMes(){
        
            try {
                return console.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "";
            
        }
        
        //发送数据
        public void send(){
            String message=getMes();
            if(message!=null&&!message.equals("")){
                try {
                    out.writeUTF(message);
                } catch (IOException e) {
                    isRunning=false;
                    closeUtil.closeAll(console,out);
                }
            }
        }
        
        @Override
        public void run() {
            while(isRunning){
                send();
            }
            
        }
    
    }
  • 相关阅读:
    ckplayer-超酷网页视频播放器的使用
    我的第一篇博文(Winfrom下WebBrowser控件的使用)
    estore商城案例(四、五)------添加购物车&购物车显示/生成订单&在线支付
    estore商城案例(三)------Filter过滤器:自动登录&权限管理
    estore商城案例(二)------登录&添加商品&商品列表(下)
    estore商城案例(二)------登录&添加商品&商品列表(上)
    myeclipse2014在线安装aptana
    estore商城案例(一)------用户注册&邮件激活(下)
    estore商城案例(一)------用户注册&邮件激活(上)
    使用指南
  • 原文地址:https://www.cnblogs.com/helloMyworld0001/p/5973001.html
Copyright © 2011-2022 走看看