import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
*
* @author jerry
*
*/
public class Clent extends MIDlet {
private Form f;
private Display dis;
public Clent() {
dis=Display.getDisplay(this);
f = new Form("xxxxxx");
dis.setCurrent(f);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
new Thread() {
public void run() {
try {
String s = "AA/n";
log("begin");
SocketConnection sc = (SocketConnection) Connector.open("socket://221.221.146.89:6000");
log("cn_success:"+sc.getAddress());
OutputStream os = sc.openOutputStream();
log("os_success:"+os.toString());
os.write(s.getBytes());
os.flush();
log("write_end.....");
InputStream is = sc.openInputStream();
log("is_success:"+is.toString());
int count = 0;
byte[] array = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((count = is.read(array, 0, array.length)) != -1) {
out.write(array, 0, count);
}
log("read_success");
String ss = new String(out.toByteArray());
log("read resout"+ss);
os.close();
is.close();
sc.close();
} catch (Exception e) {
log(e.getMessage());
}
}
}.start();
}
private void log(String name){
f.append(name);
System.out.println(name);
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
import java.io.*;
import java.net.*;
//服务器端
class Service extends Thread
{
String data=null;
String tempData=null;
DataInputStream input=null;
PrintStream output=null;
Socket client=null;
public Service(Socket client)
{
this.client=client;
}
public void run()
{
try
{
this.input=new DataInputStream(this.client.getInputStream());
this.output=new PrintStream(this.client.getOutputStream());
this.data=this.input.readLine();
this.tempData="From Client: "+this.data;
this.output.println(this.tempData);
this.output.flush();
System.out.println(this.tempData);
closeInput(this.input);
closeOutput(this.output);
closeSocket(this.client);
System.out.println("Over");
}
catch(IOException e)
{
closeInput(this.input);
closeOutput(this.output);
closeSocket(this.client);
e.printStackTrace();
}
finally
{
closeInput(this.input);
closeOutput(this.output);
closeSocket(this.client);
}
}
public static void closeOutput(OutputStream output)
{
try
{
if(output!=null)
{
output.close();
output=null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void closeInput(InputStream input)
{
try
{
if(input!=null)
{
input.close();
input=null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void closeSocket(Socket s)
{
try
{
if(s!=null)
{
s.close();
s=null;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class B
{
public static void main(String[] args)
{
try
{
ServerSocket server=new ServerSocket(6000);
System.out.println("Now Socket Server Start!");
while(true)
{
Socket client=server.accept();
Service ss=new Service(client);
ss.start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}