服务端:
package com;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ServerSocket extends MIDlet implements Runnable, CommandListener {
private Form form;
private StringItem stri;
private Command exitCmd,sendCmd;
private TextField tf;
Sends send;
public ServerSocket() {
form = new Form("serverSocket");
stri = new StringItem("接收信息:","");
tf = new TextField("发送信息:","",8000,0);
exitCmd = new Command("exit",Command.EXIT,1);
sendCmd = new Command("send",Command.SCREEN,1);
form.append(stri);
form.append(tf);
form.addCommand(exitCmd);
form.addCommand(sendCmd);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(form);
Thread th = new Thread(this);
th.start();
}
public void run() {
ServerSocketConnection scn = null;
SocketConnection sn = null;
InputStream is = null;
OutputStream os = null;
try {
scn= (ServerSocketConnection)Connector.open("socket://:6000");
sn = (SocketConnection) scn.acceptAndOpen();
is = sn.openInputStream();
os = sn.openOutputStream();
send = new Sends(os);
while(true){
StringBuffer sb = new StringBuffer();
int count = 0;
while((count=is.read())!='\n'&&count!=-1){
sb.append((char)count);
}
this.stri.setText(sb.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
os.close();
sn.close();
scn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void commandAction(Command cmd, Displayable dis) {
if(cmd==exitCmd){
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cmd==sendCmd){
this.send.send(this.tf.getString());
}
}
}
客户端:
package com;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClinetSocket extends MIDlet implements Runnable, CommandListener {
private Form form;
private StringItem stri;
private Command exitCmd,sendCmd;
private TextField tf;
Sends send;
public ClinetSocket() {
form = new Form("ClinetrSocket");
stri = new StringItem("接收信息:","");
tf = new TextField("发送信息:","",8000,0);
exitCmd = new Command("exit",Command.EXIT,1);
sendCmd = new Command("send",Command.SCREEN,1);
form.append(stri);
form.append(tf);
form.addCommand(exitCmd);
form.addCommand(sendCmd);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(form);
Thread th = new Thread(this);
th.start();
}
public void run() {
SocketConnection sn = null;
InputStream is = null;
OutputStream os = null;
try {
sn = (SocketConnection)Connector.open("socket://localhost:6000");
is = sn.openInputStream();
os = sn.openOutputStream();
send = new Sends(os);
while(true){
StringBuffer sb = new StringBuffer();
int count = 0;
while((count=is.read())!='\n'&&count!=-1){
sb.append((char)count);
}
this.stri.setText(sb.toString());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
os.close();
sn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void commandAction(Command cmd, Displayable dis) {
if(cmd==exitCmd){
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cmd==sendCmd){
this.send.send(this.tf.getString());
}
}
}
发送处理:
package com;
import java.io.IOException;
import java.io.OutputStream;
public class Sends extends Thread {
private OutputStream os;
private String message;
public Sends(OutputStream os){
this.os=os;
this.start();
}
public synchronized void send(String msg){
this.message=msg;
notify();
}
public synchronized void run() {
while(true){
if(this.message==null){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(this.message==null){
break;
}
try {
os.write(this.message.getBytes());
os.write("\r\n".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.message=null;
}
}
}
欢迎大家来加添新功能!!!!
分享到:
相关推荐
一版C# Scanbarcode 范例,刷枪刷完条码之后会自动保存档案
一个jquery.mobile做的ios风格的小网站demo,实现了类似于QQ空间的图文混排,登录注册界面效果也非常不错还有jquery.mobile版的下拉刷新效果也很流畅。
react-mobile-qq音乐演示 本项目为react-mobile-qqMusic的编译文件,用于react-mobile-qqMusic的演示,具体源码请前往查看
FunCar Kit V20 - Mobile Update;FunCar Kit V20 - Mobile Update; 车模型 简易demo
1. 这是一个针对EMDK 2.4的Mobile EDA设备条码读码Demo 2. 需要注意的是:在关闭窗体时要记得加上barcode1.EnableScanner = false;这句 代码 3. 适合于MC50、MC70、MC5590等设备 Email:baifucn@139....
- Camera Window >>DEMO - Cinematics >>DEMO - Content Fitter >>DEMO - Forward Focus >>DEMO - Geometry Boundaries - Limit Distance >>DEMO - Limit Speed >>DEMO - Numeric Boundaries >>DEMO - Pan ...
MediaTek PreLoader USB VCOM
C#代码创建的SQLite数据库操作:其中包含PDA智能设备【VS2008】,VS2010,VS2012使用SQLite数据库,包含项目demo
8.窗体加入了MobileApi属性,指示窗体是调用API移动窗体还是代码模拟移动窗体(1.Api移动窗体弊端:控件过多,移动会卡。利:win7及以上系统有摇晃窗体最小化其他程序的功能,就是与原生窗体无差别的效果) 9.窗体加入...
这个小demo的主要功能是通过PropertiesConfiguration读取配置文件的内容,然后通过HttpClient调读取到的接口,模拟接口调试...Value.saveUser=[{"username":"lili"||"mobile":"17898986754"||"mail":"26325@qq.com"}]
这是一个答题过关送奖品的静态demo <!DOCTYPE html> <title>"钱江晚报今日下沙"信访知识答题送好包 <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-...
用户状态保存是一个很常见的需求,一般用来保存用户状态的方式是在数据库表中创建多个字段来存储相应的用户状态,比如要保存用户是否绑定了手机号和QQ,则需要2个字段(mobile,qq)来保存。 如果使用二进制来保存用户...
主要文件结构如下docs >说明文档web-core ->初期Java源代码web-mobile mobile->响应式界面原始码(在web和移动端是响应式布局) web-pc pc-> pc端界面原始码(非响应式布局)快速配置方法: 1,下载项目源码或打包好...
2分法-通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高) 分页存储过程:排序反转分页法 优化后的通用分页存储过程 sql语句 一些Select检索高级用法 SQL server 2005中新增的排序函数及应用 ...
It's really easy to use and customize, just build it on your device and go outside to try!...If you build for mobile platform (iOS or Android) the map will use your location if available.
ZKWeb是一个着重快速开发和模块开发的网站框架。 提供了动态插件和全自动管理数据库结构的功能。 模板系统和自动生成页面参考了Django的做法,并遵从Don't repeat yourself原则。主要功能.Net Core支持支持运行在...
以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适配,适应移动互联潮流。 本地化支持 相比国外框架,Amaze UI 关注中文排版,根据用户代理调整字体,实现更好的中文排版效果;兼顾...
• Supports solid axle (check Monster Truck in the demo). Damage • Optimized queue-based mesh deformation that spreads processing over multiple frames. • Damage influences vehicle performance and ...
面向中小企业的免费版本,易于安装维护,安全便捷,支持虚拟主机空间。 郑重承诺:本系统无任何后门及其他流氓手段。 =========================================================================== 系统简介: ...
|中文介绍文件 :依赖库,demo,完整应用示例,学习资源仓库更新说明本仓库仅包含编译时代码,全部开源代码参见: : master为稳定版本,除紧急错误修复,每份代码提交都有很严格的发布流程规范,会先在分支经历离散...