首页 > 代码库 > 用socket模拟实现全双工通信

用socket模拟实现全双工通信

//源码下载 http://download.csdn.net/detail/qq_24054661/9802994
package com.tsocket.test;

import java.io.*;
import java.net.*;

public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket socket = new Socket("169.254.87.76",8888);
		
		new Thread(new Send(socket)).start();
		new Thread(new Receive(socket)).start();
		/*String info = "客户端要连接!";
		BufferedWriter br = new BufferedWriter(
				new OutputStreamWriter(
						socket.getOutputStream()));
		br.write(info);
		br.newLine();
		br.flush();*/
	}
}
package com.tsocket.test;

import java.io.*;
import java.net.*;

public class Server {
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(8888);
		Socket socket = server.accept();
		new Thread(new Send(socket)).start();
		new Thread(new Receive(socket)).start();
		//new Server().start();
		/*
		Socket socket = server.accept();
		DataInputStream dis = new DataInputStream(socket.getInputStream());
		DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
		while(true){	
			String info = dis.readUTF();
			System.out.println("服务器-->"+info);
			dos.writeUTF("服务器:--->"+info);
		}*/
		
	}
	public void start() throws IOException
	{
		ServerSocket server = new ServerSocket(8888);
		while(true)
		{
			Socket socket = server.accept();
			MyChannel channel = new MyChannel(socket);
			new Thread(channel).start();
		}
	}
	private class MyChannel implements Runnable
	{
		private BufferedReader br ;
		private DataInputStream dis;
		private DataOutputStream dos;
		private boolean isRunning = true;
		private String Msg = null;
		@Override
		public void run() {
			while(isRunning)
			{
				Receive();
				Send();
			}
		}
		private String getMsg()
		{
			try {
				return br.readLine();
			} catch (IOException e) {
				return null;
			}
		}
		public MyChannel (Socket socket)
		{
			br = new BufferedReader(new InputStreamReader(System.in));
			try {
				dis = new DataInputStream(socket.getInputStream());
				dos = new DataOutputStream(socket.getOutputStream());
			} catch (IOException e) {
				CloseUtil.closeAll(dis,dos);
				 isRunning = false;
			}	
		}
		private String Receive()
		{
			try{
				Msg = dis.readUTF();
				
			}catch(Exception e)
			{
				CloseUtil.closeAll(dis,dos);
				isRunning = false;
			}
			return Msg;
		}
		private void Send()
		{
			String Msg=null;
			try {
				Msg = br.readLine();
			} catch (IOException e1) {
				//e1.printStackTrace();
			}
			if(Msg==null||Msg.equals("")){
				return;
			}
			try{
				dos.writeUTF(Msg);
				dos.flush();
			}catch(Exception e){
				CloseUtil.closeAll(dis,dos);
				isRunning = false;
			}
		}
	}

}





package com.tsocket.test;

import java.io.*;
import java.net.*;

public class Receive implements Runnable {
	private boolean isRunning = true;
	private DataInputStream dis;
	public Receive(Socket s)
	{
		try {
			dis = new DataInputStream(s.getInputStream());			
		} catch (IOException e) {
			isRunning = false;
			CloseUtil.closeAll(dis);			
		}
	}
	public String receive()
	{
		String Msg="";
		try {
			Msg=dis.readUTF();
		} catch (IOException e) {
			isRunning = false;
			CloseUtil.closeAll(dis);	
		}
		return Msg;
	}
	@Override
	public void run() {
		while(isRunning)
		{
			System.out.println(receive());
		}
	}

}

 

package com.tsocket.test;

import java.io.Closeable;

public class CloseUtil {
public static void closeAll(Closeable...io)
{
	for(Closeable temp :io)
	{
		try{
			if(null!=temp)
			{
				temp.close();
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
		
}
}

 

package com.tsocket.test;

import java.io.*;
import java.net.*;

public class Send implements Runnable {

	private BufferedReader br ;
	private DataOutputStream dos;
	private boolean isRunning = true;
	
	public Send(Socket socket)
	{
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			dos = new DataOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			isRunning = false;
			CloseUtil.closeAll(dos,br);
		}	
	}
	private String getMsg()
	{
		try {
			return br.readLine();
		} catch (IOException e) {
			return null;
		}
	}
	public void send()
	{
		String Msg = getMsg();
		if(Msg!=null&&!Msg.equals(""))
		{
			try {
				dos.writeUTF(Msg);
				dos.flush();
			} catch (IOException e) {
				isRunning = false;
				CloseUtil.closeAll(dos,br);
			}
		}

	}
	@Override
	public void run() {
		
		while(isRunning)
		{
			send();
		}

	}
	
	
	
	
	

}

 

用socket模拟实现全双工通信