package com.zhiwei.readWechat;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

/**
 * 服务端启动
 * 
 * @author admin
 *
 */
@Component
public class ServerClient extends Thread {

	private final static Log log = LogFactory.getLog(ServerClient.class);

	public ServerClient() {
		this.start();
	}

	public static void main(String[] args) {
		new ServerClient();
	}

	@Override
	public void run() {
		ServerSocket server = null;
		try {
			server = new ServerSocket(22222);
			System.out.println("服务启动");
			while (true) {
				Socket socket = server.accept();
				String line = "";
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				PrintWriter writer = new PrintWriter(socket.getOutputStream());
				line = in.readLine();
				if (line != null && line.equals("result")) {
					writer.println("result:end");
					writer.flush();
					WechatReadAndWrite.need();
					log.info("收到result" + WechatReadAndWrite.needsend);
				} else if (line != null) {
					writer.println("无效" + line);
					writer.flush();
					log.info("继续接收");
				}
				line = in.readLine();
				if (line != null && line.equals("asdasd")) {
					break;
				}
				writer.close();
				in.close();
				// socket.close();
			}
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
