`
xusaomaiss
  • 浏览: 608690 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

使用java写tcp服务端

阅读更多

因为要和另一家公司合作,就需要一下测试的服务器端,很简单,发一下

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.text.DecimalFormat;

/**
 * 
 */
public class SeverTest extends Thread {

	private Socket client;
	private static String split = "\t";
	private static String mac = "00000000";

	public SeverTest(Socket c) {
		this.client = c;
		try {
			client.setSoTimeout(1000 * 30);
		} catch (SocketException e) {
			e.printStackTrace();

		}
	}

	/**
	 * 线程方法
	 */
	public void run() {
		BufferedReader in = null;
		PrintWriter out = null;
		String strfromboss = null;
		String re = "";
		try {
			in = new BufferedReader(new InputStreamReader(client
					.getInputStream()));
			out = new PrintWriter(client.getOutputStream());
			strfromboss = in.readLine();
			strfromboss = strfromboss.trim();
			// 接收信息机数据
			System.out.println("[Client IP:" + client.getInetAddress() + "\n"
					+ "from client:" + strfromboss);
			// 拆分数据包
			String remap[] = strfromboss.split("\t");

			// 申请交易密码
			if (remap[1].equals("0100")) {

				re = type1(remap);

			}
	
			// 发送到信息机数据
			System.out.println("[Client IP:" + client.getInetAddress() + "\n"
					+ "to client:" + re);

			out.println(re);
			out.flush();

		} catch (Exception e) {
			System.out.println("错误" + e.toString());

		} finally {
			try {

				if (out != null)
					out.close();
				if (in != null)
					in.close();
				if (client != null)
					client.close();
			} catch (Exception ex) {

				System.out.println("错误" + ex.toString());

			}

		}
	}

	private static String type1(String[] arg) {

		return "helo world";
	}

	

	private static String makeln(int ln) {
		String strln = "";
		DecimalFormat df = new DecimalFormat("0000");
		strln = String.valueOf(df.format(ln));
		return strln;
	}

	/**
	 * 函数入口
	 * 
	 * @param args
	 * @throws Exception
	 * @throws IOException
	 */
	public static void main(String[] args) throws Exception {

		ServerSocket server;

		server = new ServerSocket(9977);

		server.setReuseAddress(true);

		while (true) {
			System.out.println("wait for a client...");
			SeverTest ms = new SeverTest(server.accept());
			ms.start();
		}

	}

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics