|
@@ -1,5 +1,11 @@
|
|
|
package com.its.vds.xnettcp.center.protocol;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.net.Socket;
|
|
|
+import java.net.SocketAddress;
|
|
|
import java.nio.ByteBuffer;
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
@@ -178,6 +184,14 @@ public class CenterProtocol {
|
|
|
public static final int INT_DSRC_MAX_OPER_ID = 20;
|
|
|
public static final int INT_DSRC_MAX_STATE = 200; /* 최대 DSRC 시설물 상태정보 */
|
|
|
|
|
|
+ /**
|
|
|
+ * 센터 내부 통신 헤더 패킷 생성
|
|
|
+ * @param sndSystem
|
|
|
+ * @param rcvSystem
|
|
|
+ * @param opCode
|
|
|
+ * @param length
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public static byte[] getRequestHead(int sndSystem, int rcvSystem, byte opCode, int length) {
|
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
|
|
@@ -194,4 +208,67 @@ public class CenterProtocol {
|
|
|
return byteBuffer.array();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 통신 서버와 통신 연결
|
|
|
+ * @param ipAddress
|
|
|
+ * @param port
|
|
|
+ * @param connTimeout
|
|
|
+ * @param readTimeout
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static Socket connectServer(String ipAddress, int port, int connTimeout, int readTimeout) throws IOException {
|
|
|
+ SocketAddress socketAddress = new InetSocketAddress(ipAddress, port);
|
|
|
+ Socket socket = null;
|
|
|
+ try {
|
|
|
+ socket = new Socket();
|
|
|
+ socket.setSoTimeout(readTimeout); /* InputStream 에서 데이터읽을때의 timeout */
|
|
|
+ socket.connect(socketAddress, connTimeout); /* socket 연결 자체에대한 timeout */
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ return socket;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 소켓 입력 버퍼로 부터 데이터 길이만큼 읽어 온다
|
|
|
+ * @param inStream
|
|
|
+ * @param buffSize
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static byte[] receiveBytes(InputStream inStream, int buffSize) throws IOException {
|
|
|
+ byte[] buffer;
|
|
|
+ int bytesRead = 0;
|
|
|
+ int readThisTime;
|
|
|
+ buffer = new byte[buffSize];
|
|
|
+ while (bytesRead < buffSize)
|
|
|
+ {
|
|
|
+ readThisTime = inStream.read(buffer, bytesRead, buffSize - bytesRead);
|
|
|
+ if (readThisTime == -1)
|
|
|
+ {
|
|
|
+ throw new IOException("Socket.receive(): Socket closed unexpectedly");
|
|
|
+ }
|
|
|
+ bytesRead += readThisTime;
|
|
|
+ }
|
|
|
+ return buffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 소켓으로 데이터 전송
|
|
|
+ * @param socket
|
|
|
+ * @param data
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean sendData(Socket socket, byte[] data) {
|
|
|
+ try {
|
|
|
+ OutputStream os = socket.getOutputStream();
|
|
|
+ os.write(data);
|
|
|
+ os.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
}
|