import java.io.*;
import java.net.*;Socket s;
BufferedReader in;
PrintWriter out;s = new Socket(server,port); // 建立连接
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); //输入数据流
in = new BufferedReader(new InputStreamReader(s.getInputStream())); //输出数据流command = "ls\n";out.write(command); //发送命令
out.flush();String str = in.readLine(); // 读回服务器响应

解决方案 »

  1.   

    write(byte[] b, int off, int len)b.length > 1024?
    off > 1024 ?
    or 
    len > 1024?which one?
      

  2.   

    当b>1024 (off=0,int=0)
    时发送时数据就错了(乱了)
    我现在是
    发——————收
    收------------回应(如果收到)否则不回应
    再发----------再收
    .
    .
    .
    .
      

  3.   

    为什么非要固定的1024呢。还要回应。效率太低了DatagramPacket的话也不行啊他用了UDP。UDP不对数据进行检查和纠正数据回有误。不知道那位高手有好办法啊
      

  4.   

    I don't understand your meaning. You mean that if you input more than 1024 bytes' data in the byteArrayOutputStream, then some errors will happen?what's the meaning of 当b>1024 (off=0,int=0)??
      

  5.   

    那好只要告诉我如何解决b>1024时避免发送的数据不会出错。
    应该如何处理
      

  6.   

    我做的一个例子,通过socket传输zip文件,超过4K都没错,给你参考一下:
    (协议跟逻辑有关,这里就省了)服务器端程序:
    import java.io.*;
    import java.net.*;
    import java.io.BufferedInputStream;public class SocketServer {
    ServerSocket ss=null;
    Socket s=null;
    DataInputStream inStream=null;
    DataOutputStream outStream=null;
    FileInputStream fin = null;public SocketServer() {
      try{
        ss=new ServerSocket(765);
        s.setSoTimeout(3000);
      }catch(Exception e){
        System.out.println(e.toString());
      }
    }void waitForClient(){
      try{
      while(true){
      s=ss.accept();
      ThreadServer thread = new ThreadServer(s);
      thread.start();
      }
      }catch(Exception e){
        System.out.println(e.toString());
      }
    }public static void main(String[] args) {
    SocketServer socketServer1 = new SocketServer();
    socketServer1.waitForClient();
    }
    }线程类:import java.io.*;
    import java.net.*;class ThreadServer extends Thread{  private Socket socket;
      private DataInputStream inStream=null;
      private DataOutputStream outStream=null;
      private FileInputStream fin = null;  public ThreadServer(Socket sock){
        this.socket = sock;
      }  public void run(){
      boolean bool = false;
      //while(!bool){
      try{
      inStream=new DataInputStream(socket.getInputStream());
      outStream=new DataOutputStream(socket.getOutputStream());
      fin = new FileInputStream("C:/temp/socket/200212060001_ds.zip");
      //socket.setSoTimeout(3000);
      byte[] b = new byte[200];
      int i;
      while((i=fin.read(b))!=-1){
      outStream.write(b);
      }
      fin.close();
      socket.close();
      //bool = true;
      }catch(IOException ex){
      System.out.println(ex);
      }
      //}
      }}客户端:
    import java.net.*;
    import java.io.*;public class SocketClient{
    Socket s=null;
    DataInputStream inStream=null;
    DataOutputStream outStream=null;
    FileOutputStream fout = null;public SocketClient() {
    try{
    s=new Socket("192.9.207.52",765); //把这里的IP改成你运行SocketServer.class的IP
    inStream=new DataInputStream(s.getInputStream());
    outStream=new DataOutputStream(s.getOutputStream());
    fout = new FileOutputStream("C:/temp/socket/test11.zip");
    s.setSoTimeout(3000);
    waitData();
    }
    catch(Exception e){
    System.out.println(e.toString());
    }
    }void init() throws Exception{}void waitData(){
    try{
      byte[] b = new byte[200];  
      int i;
      while((i=inStream.read(b))!=-1){
        fout.write(b);
      }
      fout.flush();
      fout.close();
      s.close();
    }catch(Exception e){
    System.out.println(e.toString());
    }
    }    public static void main(String[] args) {
    SocketClient socketClient1 = new SocketClient();
    }
    }在win2k jdk1.3下调试通过,希望能帮到你。