2016-08-23-java-基于socket和多线程的聊天器通讯

服务器端:

package chatingRoomThread;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServerThread {
	public static void main(String[] args) {
		try {
			ServerSocket s = new ServerSocket(8888);
			Socket socket = s.accept();
			
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			
			Thread t1 = new Thread(new MyServerReader(dis));
			Thread t2 = new Thread(new MyServerWriter(dos));
		
			t1.start();
			t2.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class MyServerReader implements Runnable{
	private DataInputStream dis;
	
	public MyServerReader(DataInputStream dis) {
		this.dis = dis;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String info;
		
			try {
				while(true){
					info = dis.readUTF();
					System.out.println("客户端说:"+info);
					if(info.equals("bye")){
						System.out.println("客户端下线,程序退出");
						System.exit(0);
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
}
class MyServerWriter implements Runnable{
	private DataOutputStream dos;
	
	public MyServerWriter(DataOutputStream dos) {
		this.dos = dos;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String info;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
			try {
				while(true){
					info = br.readLine();
					dos.writeUTF(info);
					if(info.equals("bye")){
						System.out.println("服务器下线,程序退出");
						System.exit(0);
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
}

客户端:

package chatingRoomThread;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestClientThread {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Socket s;
		try {
			s = new Socket("localhost", 8888);
			
			InputStream is = s.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			
			OutputStream os = s.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			
			Thread t1 = new Thread(new MyClientWriter(dos));
			Thread t2 = new Thread(new MyClientReader(dis));
			
			t1.start();
			t2.start();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class MyClientReader implements Runnable{
	private DataInputStream dis;
	
	public MyClientReader(DataInputStream dis) {
		this.dis = dis;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String info;
		try {
			while(true){
				info = dis.readUTF();
				System.out.println("服务器说:"+info);
				if(info.equals("byr")){
					System.out.println("服务器端下线");
					System.exit(0);
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class MyClientWriter implements Runnable{
	private DataOutputStream dos;
	
	public MyClientWriter(DataOutputStream dos) {
		super();
		this.dos = dos;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String info;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		while(true){
			try {
				info = br.readLine();
				dos.writeUTF(info);
				if(info.equals("byr")){
					System.out.println("客户端下线");
					System.exit(0);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

基本流程为:客户端写-》服务器读-》服务器写-》客户端读-》客户端写 其中每个端口写的次数是不限的,所以读的次数也是不限的,这就实现了多线程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值