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();
        }
        
    }

}

 

posted @ 2016-10-18 13:49  mmmmgxt  阅读(229)  评论(0编辑  收藏  举报