---
title: "Socket连接技术"
author: "rayloo"
date: "2007-09-29T15:26:00Z"
tags: []
slug: "socket"
draft: false
lastmod: "2007-09-29T15:26:00Z"
id: 1008887
url: "https://rayloo.iteye.com/blog/1008887"
---

使用Socket 是连接两台计算机最简单的方法，另外由于Socket 使用的是TCP 协议，所以  
也就保证了传输的质量。但在这里要注意的是，并不是所有的MIDP 设备都支持Socket 网络。  
在这部分中我们主要涉及到的两个接口是SocketConnection 和ServerSocketConnection。这  
个两个接口的使用方法其实和J2SE 中的Socket 和ServerSocket 类的使用方法很相似。不同的是  
ServerSocketConnection 中打开一个SocketConnection 作为监听者的方法是acceptAndOpen()。同  
时你可以用getLocalAddress()和getLocalPort()两个方法获得本地的绑定IP 地址和所打开的端口  
号，这样你就可以告诉另外一台MIDP 设备你所使用的IP 和端口，使得另一台MIDP 设备可以  
连接到你的设备上。  
在这里我们除了强调使用acceptAndOpen()从一个ServerSocketConnection 对象中打开一个  
SocketConnection 作为监听者外，还要说明的是作为套接字我们是可以设置一些属性的，这些属  
性的设置是通过SocketConnection.setSocketOption()方法来设置

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagesocketText;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.OutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.Connector;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.ServerSocketConnection;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.SocketConnection;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Alert;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.AlertType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Command;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.CommandListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Display;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Displayable;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Form;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.StringItem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.TextField;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.midlet.MIDlet;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.midlet.MIDletStateChangeException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorleilu服务器端代码  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassSocketServerextendsMIDletimplementsCommandListener,Runnable  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateDisplaydisplay;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateFormf;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateStringItemsi;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateTextFieldtf;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatebooleanstop;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建Command对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateCommandsendCommand=newCommand("send",Command.ITEM,1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privateCommandexitCommand=newCommand("Exit",Command.EXIT,1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//输入流  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)InputStreamis;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//输出流  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)OutputStreamos;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Socket连接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SocketConnectionsc;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//Socket服务器连接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ServerSocketConnectionscn;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicSocketServer()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)display=Display.getDisplay(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f=newForm("SocketServer");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)si=newStringItem("Status:","");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//显示发送消息的控件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)tf=newTextField("Send:","",30,TextField.ANY);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.append(si);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.append(tf);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.addCommand(exitCommand);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//f.addCommand(sendCommand);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.setCommandListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)display.setCurrent(f);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//启动线程  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Threadt=newThread(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedvoidstartApp()throwsMIDletStateChangeException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedmethodstub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedvoidpauseApp()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedmethodstub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedmethodstub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidcommandAction(Commandc,Displayabled)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(sendCommand==c)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)os.write(tf.getString().getBytes());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)os.write(" ".getBytes());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(IOExceptione)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedcatchblock  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)elseif(exitCommand==c)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)destroyApp(false);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)this.notifyDestroyed();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(MIDletStateChangeExceptione)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedcatchblock  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidrun()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)si.setText("Waitingforconnection");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//创建一个服务器的ServerSocketConnection连接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)scn=(ServerSocketConnection)Connector.open("socket://:5000");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//等待一个连接  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sc=(SocketConnection)scn.acceptAndOpen();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设置提示信息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)si.setText("Connectionaccepted");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)is=sc.openInputStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)os=sc.openOutputStream();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//连接完成后允许发送  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.addCommand(sendCommand);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//读取客户端发来的消息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(true)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringBuffersb=newStringBuffer();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intc=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//读取数据  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(((c=is.read())!=' ')&&(c!=-1))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sb.append((char)c);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//如果读取数据失败  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(c==-1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//显示客户端反发送过来的消息  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)si.setText("Messagereceved-"+sb.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)si.setText("Connectionisclosed");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//连接结束，则不实现发送按钮  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)f.removeCommand(sendCommand);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(IOExceptionioe)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(ioe.getMessage().equals("ServerSocketOpen"))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//提示端口己经被占用  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Alerta=newAlert("Server","Port5000isalreadytaken.",  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)null,AlertType.ERROR);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)a.setTimeout(Alert.FOREVER);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)a.setCommandListener(this);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)display.setCurrent(a);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!stop)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ioe.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exceptione)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//释放资源  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicvoidstop()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stop=true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(is!=null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)is.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(os!=null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)os.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(sc!=null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)sc.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(scn!=null)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)scn.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(IOExceptione)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//TODOAuto-generatedcatchblock  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)packagesocketText;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.IOException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.InputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.io.OutputStream;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.ConnectionNotFoundException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.Connector;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.io.SocketConnection;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Alert;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.AlertType;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Command;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.CommandListener;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Display;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Displayable;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.Form;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.StringItem;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.lcdui.TextField;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.midlet.MIDlet;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavax.microedition.midlet.MIDletStateChangeException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassSocketClientextends