J2ME:使用socket实现聊天室服务端

本文介绍了一个简单的聊天室服务端程序实现,使用Java语言及MIDP规范开发,通过监听指定端口接收客户端连接,并能转发客户端之间的消息。

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

 import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.*;
import java.util.*;

public class server extends MIDlet implements CommandListener{
 
    private ServerSocketConnection ssc=null;
    private SocketConnection sc=null;
    private String msg=null;
    private TextField tf=new TextField("输入聊天信息", "", 255,TextField.ANY);
    private Command cmdSend=new Command("send",Command.OK,1);
    private Form f=new Form("聊天室服务端");
    private Display disp;
    boolean isrec=false;
    Vector vc=new Vector();
 public server() {
  // TODO Auto-generated constructor stub
  
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  //close();
 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  disp=Display.getDisplay(this);
  disp.setCurrent(f);
  //f.append(tf);
  //f.addCommand(cmdSend);
  f.setCommandListener(this);
  try {
   ssc=(ServerSocketConnection)Connector.open("socket://:9999");     //监听9999端口
   while(true)
   {
   sc=(SocketConnection)ssc.acceptAndOpen();      //获得一个客户端的连接
   client c=new client(sc);                     //为这个客户端创建一个线程    
   c.start();                                   //启动这个线程
   vc.addElement(c);}                 //将线程添加入容器进行管理
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
 }

 public void commandAction(Command c, Displayable d) {
  // TODO Auto-generated method stub
  if(c.equals(cmdSend))
  {
   
  }
 }
 
 
 

 
 
 class client extends Thread
 {
  private DataOutputStream dos=null;
    private DataInputStream dis=null;
    private String name=null;
    private SocketConnection sc=null;
  client(SocketConnection sc)
  {
    this.sc=sc; 
  }
  
  public void run()
  {

   try {
    
    //sc=(SocketConnection)ssc.acceptAndOpen();
    dos=sc.openDataOutputStream();
    dis=sc.openDataInputStream();
    name=dis.readUTF();
    f.append(name+"成功登陆/n");
    dos.writeUTF("欢迎 "+name+" 进入聊天室/n");
    isrec=true;
    
    while(isrec)
    {
    try {
     
     msg=dis.readUTF();
      f.append(name+":"+msg+"/n");
     for(int i=0;i<vc.size();i++)           //当一个收到一条信息,就转发给所有客户端
     {client c;
      c=(client)vc.elementAt(i);
      c.dos.writeUTF(name+":"+msg+"/n");}
     
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }finally
    {close();}
        
    }
    
   } catch (IOException e) {}
  }
   public void close()
   { try {
    sc.close();
    dos.close();
         dis.close();
         ssc.close();
         isrec=true;
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }}
  }
 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值