我希望通过我的计算机主机(服务器)和Raspberry pi板(客户端)之间的套接字进行通信。我使用以下代码:
对于服务器
public void startApp() {
try
{
ServerSocketConnection ssc = (ServerSocketConnection)
Connector.open("socket://:9002");
StreamConnection sc = null;
InputStream is = null;
try{
sc = ssc.acceptAndOpen();
is = sc.openInputStream();
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = is.read()) != -1){
sb.append((char)ch);
}
System.out.println(sb.toString());
} finally{
ssc.close();
sc.close();
is.close();
}
} catch (IOException x) {
x.printStackTrace();
}
}
对于客户
try{
SocketConnection sc = (SocketConnection)
Connector.open("socket://10.91.40.145:9002");
OutputStream os = null;
try{
os = sc.openOutputStream();
byte[] data = "Hello from a socket!".getBytes();
os.write(data);
} finally{
sc.close();
os.close();
}
} catch (IOException x){
x.printStackTrace();
}
当我在Raspberry pi上运行客户端时,我得到了以下日志:
Java is starting. Press Ctrl-C to exit
[CRITICAL] [SECURITY] iso=2:Permission check failed: javax.microedition.io.SocketProtocolPermission "socket://10.91.40.145:9002" ""
TRACE: <at java.security.AccessControlException: >, startApp threw an Exception
java.security.AccessControlException:
- com/oracle/meep/security/AccessControllerInternal.checkPermission(), bci=128
- java/security/AccessController.checkPermission(), bci=1
- com/sun/midp/io/j2me/socket/Protocol.open(), bci=127
- com/sun/midp/io/j2me/socket/Protocol.openPrim(), bci=4
- javax/microedition/io/Connector.open(), bci=77
- javax/microedition/io/Connector.open(), bci=6
- javax/microedition/io/Connector.open(), bci=3
- smartgridapidemopi/SmartGridAPIdemoPI.startApp(SmartGridAPIdemoPI.java:48)
- javax/microedition/midlet/MIDletTunnelImpl.callStartApp(), bci=1
- com/sun/midp/midlet/MIDletPeer.startApp(), bci=5
- com/sun/midp/midlet/MIDletStateHandler.startSuite(), bci=264
- com/sun/midp/main/AbstractMIDletSuiteLoader.startSuite(), bci=38
- com/sun/midp/main/CldcMIDletSuiteLoader.startSuite(), bci=5
- com/sun/midp/main/AbstractMIDletSuiteLoader.runMIDletSuite(), bci=132
- com/sun/midp/main/AppIsolateMIDletSuiteLoader.main(), bci=26
java.security.AccessControlException:
- com/oracle/meep/security/AccessControllerInternal.checkPermission(), bci=[ERROR] [AMS] iso=1:Sending the notification: com.oracle.midp.proxy.RunCommand@d17b7662. FAILED, proxy is disconnected
128
- java/security/AccessController.checkPermission(), bci=1
- com/sun/midp/io/j2me/socket/Protocol.open(), bci=127
- com/sun/midp/io/j2me/socket/Protocol.openPrim(), bci=4
- javax/microedition/io/Connector.open(), bci=77
- javax/microedition/io/Connector.open(), bci=6
- javax/microedition/io/Connector.open(), bci=3
- smartgridapidemopi/SmartGridAPIdemoPI.startApp(SmartGridAPIdemoPI.java:48)
- javax/microedition/midlet/MIDletTunnelImpl.callStartApp(), bci=1
- com/sun/midp/midlet/MIDletPeer.startApp(), bci=5
- com/sun/midp/midlet/MIDletStateHandler.startSuite(), bci=264
- com/sun/midp/main/AbstractMIDletSuiteLoader.startSuite(), bci=38
- com/sun/midp/main/CldcMIDletSuiteLoader.startSuite(), bci=5
- com/sun/midp/main/AbstractMIDletSuiteLoader.runMIDletSuite(), bci=132
- com/sun/midp/main/AppIsolateMIDletSuiteLoader.main(), bci=26
[ERROR] [AMS] iso=1:Sending the notification: com.oracle.midp.proxy.RunCommand@d17b76f0. FAILED, proxy is disconnected
[ERROR] [AMS] iso=1:Sending the notification: com.oracle.midp.proxy.StopCommand@d17b76ff. FAILED, proxy is disconnected
如何处理此AccessControlException。??
答案 0 :(得分:0)
端口9002被防火墙或防病毒软件关闭。检查端口可用性。
答案 1 :(得分:0)
提供API权限 javax.microedition.io.SocketProtocolPermission(“套接字:// :”) javax.microedition.io.SocketProtocolPermission(“套接字://:*”)
你的jad文件中的
您可以通过导航项目属性 - &gt;在netbeans中执行此操作。应用描述符 选择API Permission选项卡。