用socket建立一个连接然后再用I/O流去读和写应该不难吧

解决方案 »

  1.   

    我用JBuilder自己做了框架,但是在选择文件和存取文件不知道怎么弄,急求急求代码!谢谢!
      

  2.   

    P2P吧!网上有开源的C/C++的代码!
      

  3.   

    以前做的一个,看看对你有没有用主要是客户端,服务器端只是为了测试客户端,服务器读一个文件发送给客户端,客户端接收到后,存为一个新的文件.配置信息存在config.txt里.--------------------------------------------------------------------------------
    config.txtIP=202.112.157.81
    PORT=8821
    OS=Windwos2000
    PATH=D:\\Work\\Current\\IMClient\\IMClientNoGui\\1.txt--------------------------------------------------------------------------------
    客户端: import java.net.*;
    import java.io.*;/**
     * 
    public class IMClientSocket {
      private String ip;
      private int port;
      private Socket socket = null;
      DataOutputStream out = null;
      DataInputStream getMessageStream = null;
      public IMClientSocket(String ip, int port) {
        this.ip = ip;
        this.port = port;
      }  /**
       * 创建socket连接
       * @throws Exception exception
       */
      public void CreateConnection() throws Exception {
        try {
          socket = new Socket(ip, port);
        }
        catch (Exception e) {
          e.printStackTrace();
          if (socket != null) socket.close();
          throw e;
        }
        finally {
        }
      }  public void sendMessage(String sendMessage) throws Exception {
        try {
          out = new DataOutputStream(socket.getOutputStream());
          if (sendMessage.equals("Windwos2000")) {
            out.writeByte(0x1);
            out.flush();
            return;
          }
          if (sendMessage.equals("Unix")){
            out.writeByte(0x2);
            out.flush();
            return;
          }
          if (sendMessage.equals("Linux")){
            out.writeByte(0x3);
            out.flush();
          }
          else{
            out.writeUTF(sendMessage);
            out.flush();
          }
        }
        catch (Exception e) {
          e.printStackTrace();
          if (out != null) out.close();
          throw e;
        }
        finally {
        }
      }  public DataInputStream getMessageStream() throws Exception {
         try {
          getMessageStream = new DataInputStream(new BufferedInputStream(socket.
              getInputStream()));
          return getMessageStream;
        }
        catch (Exception e) {
          e.printStackTrace();
          if (getMessageStream != null) getMessageStream.close();
          throw e;
        }
        finally {
        }
      }
      public void shutDownConnection(){
        try{
        if (out != null) out.close();
        if (getMessageStream != null) getMessageStream.close();
        if (socket != null) socket.close();
        }catch(Exception e){    }
      }
    }--------------------------------------------------------------------------------
    import java.io.*;
    import java.util.*;/**
     * 
      }  /**
       *
       */
      private boolean createConnection() {
        imcs = new IMClientSocket(ip, port);
        try {
          imcs.CreateConnection();
          System.out.print("连接服务器成功!" + "\n");
          return true;
        }
        catch (Exception e) {
          System.out.print("连接服务器失败!" + "\n"); ;
          return false;
        }  }  private void sendMessage() {
        if (imcs == null)return;
        try {
          imcs.sendMessage(sendMessage);
        }
        catch (Exception e) {
          System.out.print("发送消息失败!" + "\n");
        }
      }  /* private void saveAsFile() {
           try {
             BufferedWriter toFile = new BufferedWriter(new OutputStreamWriter(new
                 FileOutputStream(path, true), "GB2312"));
             toFile.write(sGetMessage, 0, sGetMessage.length());
             toFile.close();
           }
           catch (Exception e) {
             e.printStackTrace();
           }
       }*/  private Properties getProperties() {
        Properties cfgProps = new Properties();
        try {
          InputStream is = new FileInputStream("./config.txt");
          cfgProps.load(is);    }
        catch (Exception e) {
          System.out.println("不能读取属性文件config.txt" + "\n");
          return null;
        }
        return cfgProps;
      }  private void getMessage() {
        if (imcs == null)return;
        DataInputStream inputStream = null;
        try {
          inputStream = imcs.getMessageStream();
        }
        catch (Exception e) {
          System.out.print("接收消息缓存错误\n");
          return;
        }    sGetMessage = new String();
        try {
          if (inputStream.readByte() != 0x01) {
            System.out.println("接收数据开始标识不对!");
            return;
          }
          DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new
              BufferedOutputStream(
              new FileOutputStream(path))));
          int len = inputStream.readInt();
          System.out.println("文件的长度为 " + len + "\n");
          System.out.println("开始接收文件!" + "\n");
          boolean bShow = false;
          float fFlag = 0.1f;
          for (int i = 0; i < len; i++) {
            float f = (float) i / (float) len;
            if (f > fFlag) bShow = true;
            if (bShow) {
              System.out.println("文件接收了" + (int) (fFlag * 100f) + "%" + "\n");
              bShow = false;
              fFlag = fFlag + 0.1f;
            }
            fileOut.writeByte(inputStream.readByte());
          }
          if (inputStream.readByte() == 0x03) {
            System.out.println("接收完成,文件存为" + path + "\n");
          }
          else {
            System.out.println("接收完成,结束标志没接收正确,文件没有保存" + "\n");
          }
          fileOut.close();
        }
        catch (Exception e) {
          System.out.println("接收消息错误" + "\n");
          return;
        }
      }  public static void main(String arg[]) {
        new IMClientNoGui();
      }}--------------------------------------------------------------------------------
    服务器端:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    public class ServerTest
    {
     int port = 8810;
     void start(){
      Socket s = null;
      try{
      ServerSocket ss = new ServerSocket(port);
      while(true){
       File fi = new File("1.exe");
       
       System.out.println("length"+(int)fi.length());
       s= ss.accept();
       System.out.println("a connection");
       DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
       dis.readByte();
       
       DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream("1.exe")));
       DataOutputStream  ps = new DataOutputStream(s.getOutputStream());
       ps.writeByte(0x01);
       ps.flush();
       ps.writeInt((int)fi.length());
       ps.flush();
       for(int i = 0;i < fi.length();i++){
        byte b = fis.readByte();
        ps.writeByte(b);
       }
       System.out.println("send over");
       ps.flush();
       ps.writeByte(0x03);
       ps.flush();
      }
       
     }catch(Exception e){
      e.printStackTrace();
     }
     System.out.println("connection over");
     
     }
     public static void main(String arg[]){
      new ServerTest().start();
     }

    Title: 集中维护系统通信客户端
     * 
    Description: 
     * 
    Copyright: Copyright (c) 2004
     * 
    Company: Netone
     * @author ly
     * @version 1.0
     */
    public class IMClientNoGui {
      private IMClientSocket imcs = null;
      private String sGetMessage;
      private String ip;
      private int port;
      private String sendMessage;
      private String path;
      public IMClientNoGui() {
        try {
          if (getProperties() != null) {
            ip = getProperties().getProperty("IP");
            port = Integer.parseInt(getProperties().getProperty("PORT"));
            sendMessage = getProperties().getProperty("OS");
            path = getProperties().getProperty("PATH");
            if (createConnection()) {
              sendMessage();
              getMessage();
            }
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }Title: 集中维护系统通信客户端
     * 
    Description: 
     * 
    Copyright: Copyright (c) 2004
     * 
    Company: Netone
     * @author ly
     * @version 1.0