반응형
자바로 소켓(모드버스 프로토콜)을 이용한 클라이언트 단을 테스트로 만들어 봤습니다.
딱 클라이언트의 기본 역할만 한 것이기 때문에 활용하셔서 응용하시면 될 것 같아요
주고받는 데이터는 바이트로 주고받기 때문에 일부로 바이트 배열을 사용하여 진행하였습니다.
저도 계속 수정하고 보완하려구요.
후에는 서버쪽 만들어 보도록 하겠습니다.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
class Client {
// SendData
byte[] SData = new byte[12];
byte[] RData = new byte[15];
int read;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[15];
Boolean Client = false;
/**************************************************************
* 소켓 연결 메소드
* SendData() 메소드 사용
*
* @return
*************************************************************/
public int InClient() {
int iret = 1;
String serverIP = "127.0.0.1";
try {
Socket socket = new Socket(serverIP, 502);
System.out.println("소켓 연결 완료");
if (socket != null) {
Client = true;
}
while (Client) {
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
SendData();
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
ReceiveData(read, socket);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return iret;
}
/**
* ******************************************************************
* 보내는 데이터 설정
* 모드버스 프로토콜 사용
*******************************************************************/
public void SendData() {
try {
byte[] SendData = new byte[12];
SendData[0] = (byte) 0x00;
SendData[1] = (byte) 0x00;
SendData[2] = (byte) 0x00;
SendData[3] = (byte) 0x00;
SendData[4] = (byte) 0x00;
SendData[5] = (byte) 0x06;
SendData[6] = (byte) 0x01;
SendData[7] = (byte) 0x03;
SendData[8] = (byte) 0x00;
SendData[9] = (byte) 0x00;
SendData[10] = (byte) 0x00;
SendData[11] = (byte) 0x19;
System.arraycopy(SendData, 0, SData, 0, SendData.length);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/** ***************************************
* 데이터 받는 메소드
*
*/
public void ReceiveData(int read, Socket socket) {
try {
System.out.println(read);
if (read > 0) {
System.arraycopy(RData, 0, buffer, 0, read );
}
byte[] data = baos.toByteArray();
System.out.println(data.length);
for(int i = 0; i< data.length; i++) {
System.out.println(data[i]);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Ex1 {
public static void main(String[] args)
{
Client c = new Client();
c.InClient();
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'JAVA' 카테고리의 다른 글
자바에서 상속이란 무엇일까? (0) | 2019.07.17 |
---|---|
싱글톤 디자인 패턴 (0) | 2019.07.14 |
톰캣 서버 실행 에러(해결방법) (2) | 2019.07.09 |
SVN 형상관리 서버 사용하기 (0) | 2019.07.03 |
자바를 이용한 PostgresqlDB 이미지 파일 삽입 및 출력 (0) | 2019.06.20 |