真机测试socket通信

本文介绍了一个简单的Java客户端-服务器通信程序示例。该示例演示了如何使用Socket进行客户端与服务器之间的数据发送与接收。客户端向服务器发送字符串消息,并接收服务器返回的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值