java聊天室二(客户端)

本文介绍了一个使用 Java Socket 实现的简单客户端程序,该程序能够同时发送和接收数据。通过创建两个独立的线程,分别负责发送和接收消息,实现了与服务器之间的双向通信。
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();
        }
        
    }

}

 

转载于:https://www.cnblogs.com/helloMyworld0001/p/5973001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值