TCP Client 테스트

Posted by Breeze24
2016. 6. 4. 22:37 Java
TCP Client 테스트


 다른 업체와 TCP로 연동해야 될 경우 JAVA로 클라이언트 모듈을 개발해야 한다. 

 아래 소스는 TCP 클라이언트 예제이다. 

 아래를 참조하여 TCP 클라이언트를 만들면 시간을 단축할 수 있다. 

.

순서

1. Socket 생성 - new Socket(ip, port);

2. 메세지 입력 - sendMsg = "55232........



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.test;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
 
public class ClientTcpTest {
 
    private String ip;
    private int port;
    private String sendMsg;
    private String receiveMsg;
    BufferedReader file;
 
    public ClientTcpTest(String ip, int port) throws IOException {
 
        this.ip = ip;
        this.port = port;
        Socket clientSocket = null;
 
        try {
            clientSocket = new Socket(ip, port);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
        OutputStream os = clientSocket.getOutputStream(); // out Socket
        InputStream is = clientSocket.getInputStream(); // in Socket
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
 
        sendMsg = "55232;2002;sswa;1111;15::::;8::::::\n";   //\n 꼭 입력할 것.
        bw.write(sendMsg);
        bw.flush();
        receiveMsg = br.readLine();
        System.out.println("SERVER Reply : " + receiveMsg);
 
        bw.close();
        br.close();
        clientSocket.close();
 
    }
 
    public static void main(String[] args) throws IOException {
         
        //사전에 명령프롬프트에서 telnet ip port 로 연결이 되는지 확인한다.
        ClientTcpTest clientTcpTest = new ClientTcpTest("127.0.0.1",21);  
    }
 
}
.

'Java' 카테고리의 다른 글

자바 JSON 활용하기 (2/2)  (0) 2016.06.04
Gson 활용하기 (1/2)  (0) 2016.06.04
AS400 JDBC 테스트 방법  (0) 2016.06.04
EUC-KR 문자 변환  (0) 2016.06.04
OCR 연동 API  (2) 2016.06.04