package sample;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class tcp_server {

	private static final int BUFSIZE = 1024; // Mobt@TCY

	public static void main(String[] args) throws IOException {
		// TODO ꂽ\bhEX^u

		int servPort = 5000;
		// T[o\Pbg̍쐬
		ServerSocket servSock = new ServerSocket(servPort);

		int recvMsgSize; // MbZ[WTCY
		byte[] receiveBuf = new byte[BUFSIZE]; // Mobt@

		// NCAg̐ڑ҂󂯂郋[v
		while (true) {
			Socket clntSock = servSock.accept(); // NCAg̐ڑ擾
			SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
			System.out.println("ڑF" + clientAddress);

			InputStream in = clntSock.getInputStream();
			OutputStream out = clntSock.getOutputStream();

			while ((recvMsgSize = in.read(receiveBuf)) != -1) {
				out.write(receiveBuf, 0, recvMsgSize);
				// System.out.println(receiveBuf);
				String st_data = "";

				// int ch;
				byte test_bytes;
				byte test_bytes02;
				//String st_data02 = "";

				for (int i = 0; i < receiveBuf.length; i++) {
					test_bytes = receiveBuf[i];
					test_bytes02 = receiveBuf[i+1];
					
					// int ch = Integer.toHexString(test_bytes);
					st_data = st_data + Integer.toHexString(test_bytes) + Integer.toHexString(test_bytes02) + " ";
				}

				// System.out.print(Integer.toHexString(ch) + " ");
				// st_data = st_data + Integer.toHexString(test_bytes) + " ";

				String xx = new String(receiveBuf, "UTF-8");

				System.out.println(xx.substring(71, 79));
				System.out.println(st_data.substring(20,21) + st_data.substring(22,23) + st_data.substring(24,25) + st_data.substring(31,32) + st_data.substring(33,34) + st_data.substring(35,36));
			}
			clntSock.close();
		}
		// Bs\R[h
	}
}
