日期:2014-05-20  浏览次数:20740 次

J2ME学习(4)-------socket

服务器:

ServerSocketConnection connection = (ServerSocketConnection)Connector.open("socket://:9999");?? ??? ??? ?
?? ???? ??? SocketConnection s = (SocketConnection)connection.acceptAndOpen();//监听客户,程序堵塞
?? ???? ??? System.out.println(connection.getLocalAddress() + connection.getLocalPort());
?? ???? ??? System.out.println(s.getAddress() + s.openDataInputStream().readUTF()); ??? //如果客户没有发送东西,则死等
?? ???? ??? s.openDataOutputStream().writeUTF("欢迎连接服务器");

?

客户:

??? ??? ??? SocketConnection con = (SocketConnection)Connector.open("socket://192.168.1.2:9999");
??? ??? ??? DataOutputStream dos = con.openDataOutputStream();
??? ??? ??? dos.writeUTF("你好");
??? ??? ??? System.out.println(con.openDataInputStream().readUTF());

?

线程来接受数据

extend thread

while(true)

{

System.out.println(s.getAddress() + s.openDataInputStream().readUTF()); ?

}