码迷,mamicode.com
首页 > 编程语言 > 详细

java--socket实现一个客户端像另一个客户端发送信息

时间:2014-10-15 13:53:10      阅读:563      评论:0      收藏:0      [点我收藏+]

标签:socket

首先建立接收方socket服务端

package com.haier.socket.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.haier.socket.PolicyServer;

public class TVServer implements Runnable {
	private Logger logger = Logger.getLogger(this.getClass().getName());

	private Socket client;

	public TVServer(Socket client) {
		this.client = client;
	}

	public void run() {
		logger.info("TV:client对象:" + client);
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			String flag = "aa";
			logger.info("TV:flag :" + flag);

			// 获取TV端SOCKET连接,将数据传给TV端
			PolicyServer.tvSocketMap.put(flag, client);
		} catch (Exception e) {
			logger.info("error in the close the socket!");
			e.printStackTrace();
		} finally {

		}
	}
}

再创建发送socket服务端

package com.haier.socket.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.haier.bean.PolicyBean;
import com.haier.json.PolicyJson;
import com.haier.socket.PolicyServer;

public class WEBServer implements Runnable {
	private Logger logger = Logger.getLogger(this.getClass().getName()); 
	
	private Socket WebSocket;

	public WEBServer(Socket client) {
		this.WebSocket = client;
	}

	public void run() {
		logger.info("web:client对象:"+ WebSocket);
		Socket tvSocket = null;
		try {
			// web端
			BufferedReader webIn = new BufferedReader(new InputStreamReader(
					WebSocket.getInputStream()));
			
			while(true){
	 			String webStr = webIn.readLine();
	 			if("bye".equals(webStr)){
	 				break;
	 			}
				PrintWriter webOut = new PrintWriter(WebSocket.getOutputStream());
				webOut.println("收到信息...");
				webOut.flush();
				
//				PolicyBean policyBean = PolicyJson.getMacJson(webStr);
				String key = "aa";
				logger.info("key: " + key + ",web:" + webStr);
				
				// 获取TV端SOCKET连接,将数据传给TV端
				tvSocket = PolicyServer.tvSocketMap.get(key);
				PrintWriter tvOut = new PrintWriter(tvSocket.getOutputStream());
				tvOut.println(webStr);
				tvOut.flush();
			}
			
			tvSocket.close();
			WebSocket.close();
		} catch (Exception e) {
			logger.info("error in the close the socket!");
			e.printStackTrace();
		} finally {

		}
	}
}

创建两个测试客户端,一个接受、一个发送client

package com.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientWEB {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		Socket client = new Socket("localhost", 9001);

		BufferedReader in = new BufferedReader(new InputStreamReader(
				client.getInputStream()));
		PrintWriter out = new PrintWriter(client.getOutputStream());

		BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			String str = wt.readLine();
			// String str = "jonathan";
			out.println(str);
			out.flush();
			System.out.println("client2:" + in.readLine());
		}
	}

}

package com.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ClientTV {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{

		Socket client = new Socket("localhost", 9000);

		BufferedReader in = new BufferedReader(new InputStreamReader(
				client.getInputStream()));
		PrintWriter out = new PrintWriter(client.getOutputStream());

		while (true) {
			String str = in.readLine();
			System.out.println("client1:" + str);
			// String str = "jonathan";
		}
	}

}


java--socket实现一个客户端像另一个客户端发送信息

标签:socket

原文地址:http://blog.csdn.net/gaopeng0071/article/details/40108267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!