请问楼主:能否用socket实现?

解决方案 »

  1.   

    简单说就是数据交换吧. 一般分同步交换和异步交换
    同步交换可用socket,jms
    异步交换可借助数据库,文件等间接的存储交换不知道你的实际需求是什么,动手去做吧
      

  2.   

    用socket就行,有个问题,如果网络断的怎么办,如客户端到服务器端的网络断了, 那客户端的数据是不是丢失了、
      

  3.   

    客户端的数据可以存放到cookie或者session里
      

  4.   

    http://www.lifeispig.cn/article.asp?id=103  这有个例子。你改一点就可以了。。
      

  5.   


    public class Client { /**
     * @param args
     */
    public static void main(String[] args) {
    String serverName = "192.168.0.158";
    try {
    Socket s = new Socket(serverName, 8888);
    System.out.println("连接上服务器,开始接收数据");

    InputStream is = s.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] data = new byte[1024];
    int len = 0;
    File f = null;

    len = bis.read(data);
    String msg = new String(data, 0, len);
    if (msg.indexOf("FILE:") != -1) {
    f = new File(msg.substring(msg.indexOf(':')+1));
    } else {
    f = new File("data.dat");
    }
    // f = new File("popCopy.jpg");

    FileOutputStream fos = new FileOutputStream(f);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    while (true) {
    len = bis.read(data);
    // if (new String(data, 0, len).equalsIgnoreCase("quit")) {
    // break;
    // }
    if (len == -1) {
    break;
    }
    bos.write(data, 0, len);
    bos.flush();
    }

    System.out.println("文件接收完毕");
    bos.close();
    bis.close();
    s.close();

    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }}
      

  6.   


    public class Server {

    private static final int MAX = 10;

    public Server() {
    try {
    ServerSocket ss = new ServerSocket(8888);
    System.out.println("服务器启动……");

    while (true) {
    if (ClientSocket.getCount() < MAX) {
    Socket s = ss.accept();
    new ClientSocket(s);
    } else {
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    } catch (IOException e) {
    e.printStackTrace();
    }
    } /**
     * @param args
     */
    public static void main(String[] args) {
    new Server();
    }}class ClientSocket extends Thread {
    private static int count = 0;
    private Socket s = null;
    private InetAddress ia = null;
    private String ip = null;

    public ClientSocket(Socket s) {
    count++;
    this.s = s;
    this.ia = s.getInetAddress();
    this.ip = this.ia.getHostAddress();
    this.start();
    }

    public void run() {
    try {
    File f = new File("pop.jpg");
    if (f.exists()) {
    OutputStream os = s.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    FileInputStream fis = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] data = new byte[1024];
    int len = 0;

    System.out.println("开始向" + ip + "传送文件");

    bos.write(("FILE:" + f.getName()).getBytes());
    bos.flush();

    while ((len = bis.read(data)) != -1) {
    bos.write(data, 0, len);
    bos.flush();
    }

    // bos.write("quit".getBytes());
    bis.close();
    bos.close();
    System.out.println("向" + ip + "传送文件完毕");
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    count--;
    try {
    s.close();
    System.out.println("关闭");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static int getCount() {
    return count;
    }
    }
      

  7.   

    楼主我以前写过这样的例子,是C/S架构的,你要的话E-mail我一下,我发给你参考参考[email protected]
      

  8.   

    好的,多谢,老兄,我的email:
    [email protected]