SocketTest.bsh
source("/sdcard/com.googlecode.bshforandroid/extras/bsh/android.bsh");
a = Android();
code = a.call("dialogGetInput","Socket Test","Input Area code:").get("result");
print(code);
try
{
// 创建Socket
socket = new Socket("192.168.1.102",54321);
// 向服务器发送消息
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(code);
// 接收来自服务器的消息
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = br.readLine();
if ( msg != null )
{
print(msg);
}
else
{
print("null");
}
// 关闭流
out.close();
br.close();
// 关闭Socket
socket.close();
}
catch (Exception e)
{
print(e);
}
参考: Android应用开发揭秘