public void sender(String send) throws Exception{
InetAddress serverAddr = InetAddress.getByName("127.0.0.1");//TCPServer.SERVERIP
//String serverAddr="127.0.0.1";
Log.d("TCP", "C: Connecting...");
//h.setText(send);
Socket ksocket=null;
try{
ksocket = new Socket(serverAddr, 20020); //就是这一步无法执行
}
catch (Exception e){}
if (ksocket==null)
h.setText("null");
else
h.setText(send);
String message=send;
try {
Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(ksocket.getOutputStream())),true);
out.println(message);
out.flush();
} catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { ksocket.close(); }
}有一个button,当触发onClick事件时执行这一个函数。经过测试,其他部分都没有问题。就是在函数中无法定义socket变量。 本人 android小白, 请高手赐教...
InetAddress serverAddr = InetAddress.getByName("127.0.0.1");//TCPServer.SERVERIP
//String serverAddr="127.0.0.1";
Log.d("TCP", "C: Connecting...");
//h.setText(send);
Socket ksocket=null;
try{
ksocket = new Socket(serverAddr, 20020); //就是这一步无法执行
}
catch (Exception e){}
if (ksocket==null)
h.setText("null");
else
h.setText(send);
String message=send;
try {
Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(ksocket.getOutputStream())),true);
out.println(message);
out.flush();
} catch(Exception e) { Log.e("TCP", "S: Error", e); } finally { ksocket.close(); }
}有一个button,当触发onClick事件时执行这一个函数。经过测试,其他部分都没有问题。就是在函数中无法定义socket变量。 本人 android小白, 请高手赐教...
Android的socket与Java的socket有很大的区别,Android的socket是延用于Linux的思想,而不是从Java 的socket