---
title: "J2ME学习（4）-------socket"
author: "GH0769"
date: "2010-05-06T14:43:48Z"
tags: []
categories: ["移动开发"]
slug: "j2me-4-socket"
draft: false
lastmod: "2010-05-06T14:43:48Z"
id: 660388
url: "https://gh0769.iteye.com/blog/660388"
---

服务器：

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());  

}