본문 바로가기
개발 업무(코딩)-개발자였을때 썼던..

c-java(클라) tr전문 테스트용 샘플

by 주용사 2023. 1. 9.
728x90
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 * 
	 * @throws IOException
	 * @throws UnknownHostException
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) throws UnknownHostException, IOException {

		logger.info("Welcome home! The client locale is {}.", locale);

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate);
		model.addAttribute("test", "hello");
		/////////////////////////접속 정보/////////////////////////////////////
for(int i = 0 ; i < 1; i++)
{
		String IP = "111.111.111.112";
		
		int port = 61000; // A는 60000 C는 61000
		////////////////////////////////////////////////////////////////////
		ApplicationContext app = new ClassPathXmlApplicationContext("application.xml"); 
		TcpInterface TCP_DATA_HEAD = (TcpInterface)app.getBean("tcp");
		
		// TR 마다 인풋 값 정해서 넣어준다. 클라이언트는 input에 무엇을 넣을지 안다.
		String sLength = "00096";
		String sLineFlag = "C";
		String sResponseCode = "";
		String sTrCode = "5005";
		String sNextGb = "0";
		String sReqsGb = "0";
		String sWinId = "";
		String sPcIdx = "";
		String sPcIPAddr = "111.111.111.111";
		String sNextPage = "0";
		String sUserId = "";
		String sFiller = "";
		
		TCP_DATA_HEAD.setTcp(sLength, 
				sLineFlag, 
				sResponseCode,
				sTrCode, 
				sNextGb, 
				sReqsGb, 
				sWinId,
				sPcIdx,
				sPcIPAddr,
				sNextPage,
				sUserId, 
				sFiller);
		System.out.println("["+TCP_DATA_HEAD.getTcp()+"]");
		System.out.println("["+TCP_DATA_HEAD.getTcp().length()+"]");
		////////////////////////////////////////////////////////////////////
		
		
		//////////////////socket 연결/////////////
		Socket socket = new Socket(IP, port);
		// socket.
		 socket.setSoTimeout(5000); // 5초
		// 링거타임 설정 0, 소켓을 닫을 때 아직 전송되지 않은 패킷이 있다면 이를 버리고 소켓을 즉시 닫음
		socket.setSoLinger(true, 0);
		
		OutputStream os = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;

		InputStream is = null;
		InputStreamReader isr = null;

		try {
			os = socket.getOutputStream();
			osw = new OutputStreamWriter(os);
			bw = new BufferedWriter(osw);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		String body;
		String FinalData;

		String index = "     ";
		String filler = "                                             ";
		
		///////////////////인풋 만들어서 세팅/////////////////////////////
		
		
		String uid = "1000000140      ";
		

		body = uid ;
				
		System.out.println("body length = " + body.length());
		//String count = "001";
		//body = index + filler + count;
		
		FinalData = TCP_DATA_HEAD.getTcp() + body;
		model.addAttribute("data", "{" + FinalData + "}");
		model.addAttribute("length", FinalData.length());

		// head+index[5]+filler[45]+count[3]
		try {
			bw.write(FinalData, 0, FinalData.length()); // newLine()하면 안된다.
			bw.flush();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//System.out.println("11111111111111");
		BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
		
		// head[80] + index[5] + filler[45] + count[5] + sub[16]*alpha = 기본 151
		int read = 0;
		//TCP_DATA_HEAD의 길이값은 고정
		byte[] len = new byte[5];
		read = bis.read(len, 0, 5);
		//System.out.println("2222222222222");
		int tcp_slength = Integer.parseInt(new String(len));
		//System.out.println("test len = " + tcp_slength);
	
		byte[] buff = new byte[tcp_slength]; // 위에서 한번 짤랐기때문에 데이터길이를 제외하고 나온다.
		read = bis.read(buff, 0, buff.length); 
		//System.out.println("body = " + read);
		if (read < 0) {
			System.out.println("no data");
		}
		String sbuff = new String(buff);
		
		model.addAttribute("output", sbuff);
		System.out.println("output = [ " + sbuff.substring(80, sbuff.length()) + " ]");
		
		 os.close();
		 osw.close(); 
		 bw.close(); 
		 bis.close();
		 socket.close();
}		 

		return "home";
	}
}

완벽한 소스는 아니고 단순히 잘가는지 잘받아지는지만 확인용이다. 

728x90