현재이 문제가 발생했습니다.java.net.ConnectException : /192.168.1.106:8002 - 연결이 거부되었습니다 (Android 예외)
장치에 명령을 보내고 장치에서 응답을 안드로이드 에뮬레이터에서 소켓으로받을 수 있습니다.
하지만 동일한 애플리케이션을 태블릿에 설치하면 문제가 발생합니다. 처음 장치를 연결했는지 여부를 확인하는 명령을 보내면 장치가 연결되어있는 응답을 보내지 만 다음 명령을 두 번째로 보내면 다음 예외가 throw됩니다.
java.net.ConnectException : /192.168.1.106:8002 - 연결이 거부되었습니다.
이
는 요청을 수행하는 코드이다 :public static String sendRequestandResponse(final String host,final int port,
final String command,
final int timeoutInMillis,final int responseLength) throws UnknownHostException,NetworkSettingException
{
if (host == null)
{
throw new NullPointerException("host is null"); //NOPMD
}
Socket clientSocket=null;
try {
/**
* Creating socket connection with IP address and port number to send Command
*/
try{
clientSocket = new Socket();
SocketAddress remoteAdr = new InetSocketAddress(host, port);
clientSocket.connect(remoteAdr, 1000);
clientSocket.setSoTimeout(timeoutInMillis);
}catch (Exception e) {
e.printStackTrace();
throw new NetworkSettingException(e.getMessage());
}
final PrintWriter outPutStream = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), CHARSET));
try
{
outPutStream.print(command);
outPutStream.flush();
BufferedReader responseString = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), CHARSET));
response = new StringBuilder();
try
{
int pos = 0;
while (true)
{
pos++;
System.out.println(pos);
int i=responseString.read();
byte[] resp={(byte)i};
System.out.println(new String(resp));
response.append(new String(resp));
if(pos>=responseLength){
{
clientSocket.shutdownInput();
clientSocket.shutdownOutput();
clientSocket.close();
Log.d("ConnectionSocket", "Socket closed with break");
break;
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
finally
{
responseString.close();
}
}
finally
{
outPutStream.close();
}
}
catch(IOException ex){
}
catch(NullPointerException ex){ //NOPMD
}
finally
{
try {
clientSocket.shutdownInput();
clientSocket.shutdownOutput();
clientSocket.close();
} catch (NullPointerException ex) { //NOPMD
} catch (IOException ex) {
}
}
return response.toString();
}
나는 그것이 소켓 처음을 닫습니다 나던 생각, 그래서 두 번째는 연결을 거부한다.
동일한 코드가 에뮬레이터에서도 작동합니다.
탭이 언급 된 시스템에 연결할 수 있는지 확인하십시오. –
"연결 거부"는 대상 호스트의 소켓에서 수신 대기중인 서버가 없음을 의미합니다. 따라서 클라이언트 코드는 여기서는 쓸모가 없습니다. – Ingo
을 sidenote로 사용하십시오 ... 예외 처리가 끔찍합니다. http://source.android.com/source/code-style.html#java-language-rules – mibollma