如何在黑莓模拟器上实现socket通信啊?
我建了一个客户端和一个服务器
客户端
public class Client {
StreamConnection conServer = null;
boolean isConnected = false;
public boolean SocketConn(String address,String port) throws IOException{
String strServerAdd = "socket://"+address+":"+port;
try {
conServer = (StreamConnection)Connector.open(strServerAdd);
} catch (Exception e) {
System.out.println("Conect server error!");
return false;
}
if(conServer==null){
isConnected = false;
return false;
}
isConnected = true;
System.out.println("Conect success!");
return true ;
}
服务器
public class Service extends Thread{
ServerSocketConnection SocketSer;
String runlogString = "";
ServiceMainScreen serMainscr = null;
public Service(ServiceMainScreen serMainscr ,String port){
this.serMainscr = serMainscr;
try {
SocketSer = (ServerSocketConnection)Connector.open("socket://:"+port);
this.start();
runlogString = "服务器已经启动!";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 如有连接,则新增一个线程对连接进行处理
}
public void run(){
while(true){
// 等待客户端连接
try {
SocketConnection sc = (SocketConnection)SocketSer.acceptAndOpen();
DataInputStream dis = (DataInputStream) sc.openDataInputStream();
String msg = dis.readUTF();
serMainscr.message.append(sc.getAddress()+"用户:"+msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
然后我在一天电脑上用模拟器运行服务器,再在台局域网内的另一台电脑上用模拟器运行客户端。。发现他们不能连接。。是不是代码存在缺陷还是。遗漏了什么要点啊。。比如模拟器的一些什么设置的?
[解决办法]
客户端为什么不用SocketConnection?
[解决办法]