哦,不会写线程啊?随便找本书来看看,Java线程还是比较简单的。

解决方案 »

  1.   

    public class Client implements Runnable{     public void start() {
            Thread t = new Thread(this);
            t.start();
        }
       public void run() {
            try {
                System.out.println("===================the client thread run");
                sc = (SocketConnection) Connector.open("socket://localhost:"+PORT);
                si.setText("Connected to server");
                dis = sc.openDataInputStream();
                dos = sc.openDataOutputStream();
                // Start the thread for sending messages - see Sender's main
                // comment for explanation            sendID++;
                sender = new Sender(dos);
              //开始接受数据Oct.13
               while(true){
               }}其中Sender类是一个用来发送数据的线程extends Thread
      

  2.   

    wjsfr(令狐葱) 大虾如果我想对某个缓存扫描,扫描到一条记录启动一个线程去发送它,这样是不是好点?如果这样可以的话那么这个结构框架应该是线程里面套线程,怎么个套法?谢谢!
      

  3.   

    用2个端口,开一个线层在一个端口监听,发送消息包中目的ip和端口是你c++的,源端口和ip是你监听那个的,就可以达到效果了。
      

  4.   

    我上边给你说的就可以实现你的做法阿,用Sender来进行缓存的扫描
      

  5.   

    关于Sender  能具体讲点吗?
      

  6.   

    给你个sun的demo,你自己改一下就可以用了
    /*
     * @(#)Sender.java 1.6 04/04/25
     *
     * Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved. 
     * PROPRIETARY/CONFIDENTIAL
     * Use is subject to license terms
     */
    package socket;import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import javax.microedition.lcdui.*;
    import java.io.*;public class Sender extends Thread {    private OutputStream os;
        private String message;    public Sender(OutputStream os) {
            this.os = os;
            start();
        }    public synchronized void send(String msg) {
            message = msg;
            notify();
        }    public synchronized void run() {        while (true) {            // If no client to deal, wait until one connects
                if (message == null) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }            if (message == null) {
                    break;
                }            try {
                    os.write(message.getBytes());
                    os.write("\r\n".getBytes());
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }            // Completed client handling, return handler to pool and
                //  for wait
                message = null;
            }
        }    public synchronized void stop() {
            message = null;
            notify();
        }
    }
      

  7.   

    谢谢 wjsfr(令狐葱) 大哥,
    我现在遇到一个问题, 客户端和服务器端是两个分开的类 Client.class 
    和Server.class因为程序要同为客户端和服务器端与C++ Socket 的服务器端和客户端,在两个端口通讯,我在整合JAVA 的Client.class 与 Server.class时, 我增加了一个 Frame.class(表单)Frame.clsee的文本筐输入的数据送往发送缓存,而这个缓存的数据必须要让Client.class 的线程能够扫描到,这个发送缓存区我应该怎么去定义两边才能兼顾呢?时不时把Client.class 作为Frame.class的一个子类这样好做点?请大侠们指教
      

  8.   

    顺便请大家看看这个贴子, 关于POI的,谢谢.
    http://community.csdn.net/Expert/topic/3706/3706109.xml?temp=.6670801
      

  9.   

    import java.io.*;
    import java.net.*;
    import java.util.*;public class Server implements Runnable { public final static int MAX_CONNECTIONS = 50;//默认连接数50
    public final static int DEFAULT_MAX_BACKLOG = 5;private void getIni(){
    connections = new Vector(max_connections);
    //启动线程
    Thread t=new Thread(this);
    t.start();
    }//server run -------------------------------------------------------
    public void run(){ }


    public class Client implements Runnable{  //内部类
        private String inok;public void init() {
          try {      }
          catch(Exception e) {
            e.printStackTrace();
          }
        }public void run(){
        }public boolean getIni(){
                //***
                //启动线程
                conn = new Vector(maxconn);
                Thread thrd = new Thread(this);
                thrd.start();
                return true;          }        }//main----------------------------------------------------
    public static void main(String args[]){
        try {
    String cmdstr;
    Server ufs=new Server();
    ufs.getIni();
    Client C=Client(); //// 问题出在这里cannot resolve symbol: 
                                         //// class Client at line 68, column 9
                                         //// 这个内部类应该怎么调用?
    C.getIni();
        }
        catch(Exception e) {
          System.out.println("Unable to connect to server!");
          e.printStackTrace();
        }
    } }
    Client 这个内部类应该怎么调用?
      

  10.   

    若Client是你的Server的内部类,就像我所看到的,你在main中可以这么写:
    Server ufs=new Server();
    Server.Client client=ufs.new Client();
    try this@