private String RecvData(String ip, int port, String sendMsg)
{
String result = "";
SocketChannel socketChannel = null;
try
{
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(ip, port));
// Finish Connect Method 호출 간 Sleep
Thread.sleep(100);
if(socketChannel.finishConnect() == true)
{
ByteBuffer sendBuffer = ByteBuffer.wrap(sendMsg.getBytes());
socketChannel.write(sendBuffer);
// Data Recv 대기를 위한 Sleep
Thread.sleep(100);
ByteBuffer recvBuffer = ByteBuffer.allocate(1024);
socketChannel.read(recvBuffer);
recvBuffer.flip();
CharBuffer charBuffer = Charset.forName("UTF-8").decode(recvBuffer);
StringBuilder builder = new StringBuilder();
while (charBuffer.hasRemaining())
{
builder.append(charBuffer.get());
}
charBuffer.clear();
recvBuffer.clear();
sendBuffer.clear();
if(socketChannel != null)
{
socketChannel.socket().close();
socketChannel.close();
socketChannel = null;
}
result = builder.toString();
}
}
catch (Exception ex)
{
}
return result;
}
자바로 클라이언트 짰는데
서버로부터 결과값가져와서 String으로 반환 ㅇㅇ
댓글 0