【TCP简单聊天实现】

客户端

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ChatClient {  

    public static void main(String[] args) throws Exception, IOException {  
        //创建客户端的Socket服务,指定目的主机和端口号  
        Socket socket=new Socket("202.194.126.159",10000);  
          
        BufferedReader dosReader=new BufferedReader(new InputStreamReader(System.in)); 
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        
        new ChatClientToServerWriter(dosReader, bufferedWriter).start();
        new ChatServerToClinetReader(bufferedReader, bufferedWriter).start();
        
    }  
}  

class ChatClientToServerWriter extends Thread{
	BufferedReader dosReader;
	BufferedWriter bufferedWriter;
	public ChatClientToServerWriter(BufferedReader dosReader,
			BufferedWriter bufferedWriter) {
		super();
		this.dosReader = dosReader;
		this.bufferedWriter = bufferedWriter;
	}
	@Override
	public void run() {
		super.run();
		String line=null;
        try {
        	while(true)
        	{
        		while((line=dosReader.readLine())!=null){  
    				bufferedWriter.write(line);
    				bufferedWriter.newLine();
    				bufferedWriter.flush();
    			}
        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class ChatServerToClinetReader extends Thread{
	BufferedReader bufferedReader;
	BufferedWriter bufferedWriter;
	
	public ChatServerToClinetReader(BufferedReader bufferedReader,
			BufferedWriter bufferedWriter) {
		super();
		this.bufferedReader = bufferedReader;
		this.bufferedWriter = bufferedWriter;
	}

	@Override
	public void run() {
		super.run();
		String line=null;
        try {
        	while(true)
        	{
    	        while((line=bufferedReader.readLine())!=null){  
    	        	System.out.println("服务端发来:"+line);
    	        } 
        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


服务器

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {

	public static void main(String[] args) throws Exception {
		//创建服务端的Socket服务,指定端口号
		ServerSocket serverSocket=new ServerSocket(10000);
		
		while(true){
			//通过accept方法获取连接过来的Socket对象
			Socket socket=serverSocket.accept();
			
			BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
			BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
			BufferedReader dosReader=new BufferedReader(new InputStreamReader(System.in)); 
	        
			new ChatClientToServerReader(bufferedReader, bufferedWriter).start();
			new ChatServerToClientWriter(dosReader, bufferedWriter).start();
		}
		
	}
}

class ChatClientToServerReader extends Thread{
	BufferedReader bufferedReader;
	BufferedWriter bufferedWriter;
	
	public ChatClientToServerReader(BufferedReader bufferedReader,
			BufferedWriter bufferedWriter) {
		super();
		this.bufferedReader = bufferedReader;
		this.bufferedWriter = bufferedWriter;
	}

	@Override
	public void run() {
		super.run();
		String line=null;
        try {
        	while(true)
        	{
    	        while((line=bufferedReader.readLine())!=null){  
    	        	System.out.println("客户端发来:"+line);
    	        } 
        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class ChatServerToClientWriter extends Thread{
	BufferedReader dosReader;
	BufferedWriter bufferedWriter;
	public ChatServerToClientWriter(BufferedReader dosReader,
			BufferedWriter bufferedWriter) {
		super();
		this.dosReader = dosReader;
		this.bufferedWriter = bufferedWriter;
	}
	@Override
	public void run() {
		super.run();
		String line=null;
        try {
        	while(true)
        	{
        		while((line=dosReader.readLine())!=null){  
    				bufferedWriter.write(line);
    				bufferedWriter.newLine();
    				bufferedWriter.flush();
    			}
        	}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值