在同一个端口号下可以互动聊天室

接收端

package Tcp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
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 a implements  Runnable {
	Socket	s;
	public a(Socket s){
			this.s=s;
	}
	public void run() {
		byte[] bys=new byte[1024];
		String line;
		boolean a=true;
		BufferedReader	br=null;BufferedWriter	bw=null;BufferedReader	br1=null;
		 try {
			 	br= new BufferedReader(new InputStreamReader(this.s.getInputStream()));
			bw  = new BufferedWriter(new OutputStreamWriter(this.s.getOutputStream()));
			 br1= new BufferedReader(new InputStreamReader(System.in));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
			try {
				if("1".equals(Thread.currentThread().getName())){
					while(a){
				line=br.readLine();
				if(line==null){
					break;
				}
				System.out.println(line);
					}
				
				}else{
					while(a){
					line=br1.readLine();
					if(line==null||line==" "){
						break;
					}
					bw.write("wu :"+line);
					bw.newLine();
					bw.flush();
					}
				}
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				
			}finally {
				try {
					if(br1!=null)
						br1.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
	}

	public static void main(String[] args) throws IOException {
		ServerSocket	ss = new ServerSocket(10086);
		Socket   s	 = ss.accept();
		a a=new a(s);
		Thread thread = new Thread(a,"1");
		thread.start();
		Thread thread1 = new Thread(a,"2");
		thread1.start();
	System.out.println();
	}

	}

发送端

package TCP;

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;
import java.net.UnknownHostException;

//tcp发送端
public class ClientDemo implements  Runnable {
	static Socket s ;
	 BufferedWriter bw ;
	 BufferedReader br1;
	 BufferedReader br ;
	public ClientDemo(Socket s) {
		
		try {
			this.s = s;
			bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
			br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
			br= new BufferedReader(new InputStreamReader(System.in));
			   
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void run() {
		String line;
			boolean a=true;
				try {
					if("1".equals(Thread.currentThread().getName())){
					while(a){
				line=this.br.readLine();
				if(line==null||line==" "){
					break;
				}
					
				this.bw.write("li :"+line);
				this.bw.newLine();
				this.bw.flush();
					}
				}else{
					while(a){
					line=this.br1.readLine();
					if(line==null){
						break;
					}
			System.out.println(line);
					}
					}
			
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(this.br!=null)
					this.br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(this.s!=null)
					this.s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		}
		

	public static void main(String[] args) throws Exception {
		Socket s = new Socket("192.168.1.132", 10086);
		ClientDemo clientDemo = new ClientDemo(s);
		Thread thread = new Thread(clientDemo,"1");
		thread.start();
		Thread thread1 = new Thread(clientDemo,"2");
		thread1.start();
		
	
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值