ECHO TCP Server Client

本文介绍了一个使用 Java 实现的简单 TCP Echo 服务器和客户端程序。服务器监听特定端口并反射客户端发送的消息。客户端连接到服务器,并通过对话框输入消息,服务器将消息回显给客户端,直到客户端发送特定结束指令。
摘要由CSDN通过智能技术生成
//server
package com.test.javaSe02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import com.ctl.util.PropertiesUtil;

public class ECHOServer {
	public static void main(String[] args) {
		ServerSocket server = null;
		int port = Integer.parseInt(PropertiesUtil.loadProperties(
				"E:\\JavaWeb\\A\\src\\com\\test\\javaSe02\\tcp.properties")
				.getProperty("port"));
		try {
			server = new ServerSocket(port);
		} catch (IOException e) {
			e.printStackTrace();
		}

		PrintStream out = null;
		BufferedReader br = null;
		while (true) {
			Socket client = null;
			try {
				client = server.accept();
				br = new BufferedReader(new InputStreamReader(
						client.getInputStream()));
				out = new PrintStream(client.getOutputStream());
				boolean flag = true;
				while (flag) {
					String str = br.readLine();
					if (str.equals("") || null == str)
						flag = false;
					else {
						if ("bye".equals(str.toLowerCase()))
							flag = false;
						else
							out.println("server echo:" + str);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
//client
package com.test.javaSe02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

import com.ctl.util.PropertiesUtil;

public class ECHOClient {
	public static void main(String[] args) {
		Properties pro = PropertiesUtil
				.loadProperties("E:\\JavaWeb\\A\\src\\com\\test\\javaSe02\\tcp.properties");
		Socket client = null;
		try {
			client = new Socket(pro.getProperty("ip"), Integer.parseInt(pro
					.getProperty("port")));
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			PrintStream out = null;
			boolean flag = true;
			while (flag) {
				out = new PrintStream(client.getOutputStream());
				String str = javax.swing.JOptionPane.showInputDialog("message");
				//System.out.println(str);
				out.println(str);
				if (str.equalsIgnoreCase("bye"))
					flag = false;
				else {
					System.out.println(new BufferedReader(
							new InputStreamReader(client.getInputStream())).readLine());
				}
			}
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
//PropertiesUtil
package com.ctl.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @deprecated 该类可以用来创建properties资源文件
 * @author Administrator
 * @see  www.ctl.com.cn
 * @category SDGHDHSDFG
 * @serial  dfgds
 */
public class PropertiesUtil {
	/**
	 * 
	 * @param propertityPath
	 *            资源文件要存放的路径例如H:/mysql.properties
	 * @param map
	 *            将要写入的资源放入该map中 HashMap<String, String>
	 * @param comment
	 *            对在资源文件的描述
	 */
	public static void createPropertityFile(String propertityPath, String comment, Map<String, String> map) {
		File file = new File(propertityPath);
		new File(file.getParent()).mkdirs();
		Properties pro = new Properties();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			pro.setProperty(entry.getKey(), entry.getValue());
		}
		try {
			pro.store(new FileOutputStream(propertityPath), comment);
		} catch (Exception e) {
			System.err.println("存储properties出错");
		}
	}
	/**
	 * @param map
	 *            将要写入的资源放入该map中 HashMap<String, String>
	 * @param comment
	 *            对在资源文件的描述

	 * @param propertityPath
	 *            资源文件要存放的路径例如H:/mysql.xml  
  	 */
	public static void createPropertityXMLFile(String propertityPath, String comment, Map<String, String> map) {
		File file = new File(propertityPath);
		new File(file.getParent()).mkdirs();
		Properties pro = new Properties();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			pro.setProperty(entry.getKey(), entry.getValue());
		}
		try {
			pro.storeToXML(new FileOutputStream(propertityPath), comment,"utf-8");
		} catch (Exception e) {
			System.err.println("存储properties出错");
		}
	}
	/**
	 * 
	 * @param filePath "H:/mysql.properties"
	 * @return  Properties的实例对象
	 */
	public static Properties loadProperties(String filePath){
		Properties pro=new Properties();
		try {
			pro.load(new FileInputStream(new File(filePath)));
		} catch (FileNotFoundException e) {
			System.err.println("文件未找到");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pro;
	}/**
	 * 
	 * @param filePath "H:/mysql.xml"
	 * @return  Properties的实例对象
	 */
	public static Properties loadXMLProperties(String filePath){
		Properties pro=new Properties();
		try {
			pro.loadFromXML(new FileInputStream(new File(filePath)));
		} catch (FileNotFoundException e) {
			System.err.println("文件未找到");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pro;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("a", "value1");
		map.put("b", "value2");
		map.put("c", "value3");
		String path="H:" + File.separator + "a//a/"+File.separator;
		createPropertityFile(path+"mysql.properties", "mysql数据库资源配置",
				map);
		createPropertityXMLFile(path+"mysql.xml", "mysql数据库资源配置",
				map);
		Properties pro=loadProperties(path+"mysql.properties");
		System.out.println(pro);
		pro=loadXMLProperties(path+"mysql.xml");
		System.out.println(pro);
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值