PC와 Device간에 공유기를 이용한 TCP/IP통신을 구현하고 있습니다.
소스를 뒤적이다가 이해 안되는 부분이 있써서 글을 올리는 데요....
PC에서 서버를 열고 안드로이드 디바이스가 PC의 IP주소와 서버에 SocketPort??라고 해야지 맞는 표현인지는 모르겠지만...
이 두개가 존재해야 통신을 할수 있다고 생각을하는 데
지금 보고있는 소스에는 PC의 주소가 전혀 없써서...이게 의외였습니다.
PC에서 사용하고 있는 소스는 구할수가 없써서...
대신 안드로이드 소스를 올려드리겠습니다.
이 소스와 연동 시키려면 어떤 구조로 만들어야 할지...
이것이 제일 궁금증입니다.
부분 소스입니다.
class TCPServer implements Runnable {
private InputStream input = null;
private OutputStream output = null;
private Socket socket = null;
public void run() {
Log.d(TAG, "TCPServer - run() ================ 끝");
// TODO Auto-generated method stub
is_alive = true;
try {
serverSocket = new ServerSocket(SERVERPORT);
System.out.print("Conneting..");
while (!isStop) {
// 서버 데몬 실행
socket = serverSocket.accept();
addPCLinkStatus(socket.getInetAddress().getHostAddress()
+ " " + getString(R.string.connected));
// PC와 연결되었습니다.
// 데이터 송수신
int input_read = 0;
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
// addPCLinkStatus(getString(R.string.measure_progress));
while (input_read > -1) {
// 데이터 수신
byte[] buffer = new byte[1024];
input_read = input.read(buffer);
System.out.print(input_read);
String tempbuffer = new String(buffer);
tempbuffer = tempbuffer.trim();
Log.d(TAG, tempbuffer);
// 잠깐 쉬기..
SystemClock.sleep(100);
// 데이터 송신
String sendMsg = ParsingCommand(tempbuffer);
System.out.print(sendMsg);
Log.d(TAG, "Send: " + sendMsg);
output.write(sendMsg.getBytes());
output.flush();
System.out.print("Socket Write");
if (isStop)
break;
}
}
// Socket 종료
addPCLinkStatus(socket.getInetAddress().getHostAddress() + " "
+ getString(R.string.disconnected));// PC 연결이 해제되었습니다.
closeSocket();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
closeSocket();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
serverSocket = null;
}
}
addPCLinkStatus("PC Link End");// PC Link를 종료하였습니다.\r\n
is_alive = false;
Log.d(TAG, "TCPServer - run() ================ 끝");
}
// Client Socket 종료
private void closeSocket() {
Log.d(TAG, "closeSocket()");
try {
if (input != null) {
input.close();
input = null;
}
if (output != null) {
output.close();
output = null;
}
if (socket != null) {
socket.close();
socket = null;
}
} catch (Exception e) {
Log.d(TAG, "closeSocket() error:" + e.getMessage());
e.printStackTrace();
}
}
}
}
디바이스가 할당 받은 IP를 사용해서 서버에 접속시킬수가 있는 건가요???