JAVA - 쌩기초... 조금만 조언을 주세요.
JAVA 에 대해서는 기본적인... 이것이 무엇인지만 압니다.
간단한 네트웍 소켓 데몬이 필요해서 java.sun.com 을 둘러보니
좋은 예제가 있어서 가져와서 실행하여보니 아주 잘 됩니다.
근데... 문제는 조금 수정을 해야 하는데 이게 문제군요 ㅡㅡ;;;
스스로 생각해 봐도 그 해결해야될 문제가 워낙에 기초적인 부분인데...
어쨌든... 원하는 것은...
어떤 ip (예. 192.168.100.100) 에 소켓접속을 해서
fput 으로 /home 이라는 문자열을 날렸다고 가정합니다.
이와 같은 상황일때 192.168.100.100 에는 이미
소켓서버가 실행되어 있습니다.
리쓴해서 외부 프로그램을 실행하면 되는것입니다.
exec 죠...
위 예를 들자면 ls -al /home
과 같은 경우 입니다.
즉... 다른곳에서 날린 문자열을 받아서
서버쪽에서 ls -al 문자열
을 실행하는 것입니다.
sun 싸이트에서 select 방식의 소켓서버 소스가 있더군요.
아래와 같습니다.
아래 소스에서
Process pc = runtime.exec(?????????????????);
의 물음표 부분에서 실행하면 되겠죠?
그런데... 소켓 접속해서 날려준 문자열을 받아서 처리해야 하는데
어떻게 해야 할까요?
이미 다 있는것 같은데 기본적인 부분을 몰라서 해결을 하지 못하고
있습니다.
조금만 도움을 주시면 감사드립니다.
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
public class process_server {
private static int PORT = 9876;
private static int BUFFER_SIZE = 2048;
public static void main (String args[]) {
ByteBuffer sharedBuffer =
ByteBuffer.allocateDirect(BUFFER_SIZE);
Selector selector = null;
ServerSocket serverSocket = null;
try {
ServerSocketChannel serverSocketChannel =
ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocket = serverSocketChannel.socket();
InetSocketAddress inetSocketAddress =
new InetSocketAddress(PORT);
serverSocket.bind(inetSocketAddress);
selector = Selector.open();
serverSocketChannel.register(
selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
System.err.println("Unable to setup environment");
System.exit(-1);
}
try {
while (true) {
int count = selector.select();
// nothing to process
if (count == 0) {
continue;
}
Set keySet = selector.selectedKeys();
Iterator itor = keySet.iterator();
while (itor.hasNext()) {
SelectionKey selectionKey =
(SelectionKey) itor.next();
itor.remove();
Socket socket = null;
SocketChannel channel = null;
if (selectionKey.isAcceptable()) {
System.out.println("Got acceptable key");
try {
socket = serverSocket.accept();
System.out.println
("Connection from: " + socket);
channel = socket.getChannel();
} catch (IOException e) {
System.err.println("Unable to accept channel");
e.printStackTrace();
selectionKey.cancel();
}
if (channel != null) {
try {
System.out.println
("Watch for something to read");
channel.configureBlocking(false);
channel.register
(selector, SelectionKey.OP_READ);
} catch (IOException e) {
System.err.println("Unable to use channel");
e.printStackTrace();
selectionKey.cancel();
}
}
}
if (selectionKey.isReadable()) {
System.out.println("Reading channel");
SocketChannel socketChannel =
(SocketChannel) selectionKey.channel();
sharedBuffer.clear();
int bytes = -1;
try {
while (
(bytes = socketChannel.read(sharedBuffer)) > 0)
{
System.out.println("Reading...");
sharedBuffer.flip();
while (sharedBuffer.hasRemaining()) {
System.out.println("Exec...");
//socketChannel.write(sharedBuffer);
Runtime runtime = Runtime.getRuntime();
Process pc = runtime.exec(?????????????????);
}
sharedBuffer.clear();
}
} catch (IOException e) {
System.err.println("Error writing back bytes");
e.printStackTrace();
selectionKey.cancel();
}
try {
System.out.println("Closing...");
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
selectionKey.cancel();
}
}
System.out.println("Next...");
}
}
} catch (IOException e) {
System.err.println("Error during select()");
e.printStackTrace();
}
}
}


Java Almanac
http://javaalmanac.com/egs/java.net/ReadFromSocket.html?l=rel
자바 code snippet은 위 사이트에서...
----
I paint objects as I think them, not as I see them.
atie's minipage
댓글 달기