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.springframework.stereotype.Component;

@Component
public class ServerClient {

	public static boolean needSend = false;

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

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

	public void start() {
		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());
				// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				// System.out.println("Client:" + in.readLine());
				line = in.readLine();
				System.out.println(line);
				// line = br.readLine();
				if (line != null && line.equals("result")) {
					writer.println("result:end");
					writer.flush();
					needSend = true;
					System.out.println("收到result");
				} else if (line != null) {
					writer.println("无效" + line);
					writer.flush();
					System.out.println("继续接收");
				}
				line = in.readLine();
				// if (line != null) {
				// System.out.println("接收到:" + line);
				// try {
				// Thread.sleep(1000 * 60);
				// } catch (InterruptedException e) {
				// e.printStackTrace();
				// }
				// }
				if (line != null && line.equals("asdasd")) {
					break;
				}
				writer.close();
				in.close();
				// socket.close();
			}
			server.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
