服务器客户端文件传输程序出错?

Error in server client File Transfer program?

服务器class:

import java.net.*;
import java.io.*;
public class fileserver {
    public static void main(String args []) {
        ServerSocket ss=new ServerSocket(2345);
        Socket s= ss.accept();
        FileInputStream f=new FileInputStream("D:\FEATURED.txt");
        DataOutputStream dout= new DataOutputStream(s.getOutputStream);
        byte[] b=new byte[2002];
        f.read(b,0,b.length);
        dout.write(b,0,b.length);
        dout.close();
        f.close();
        s.close();
    }   
}
    

客户class:

import java.io.*;
import java.net.*;

public class clientserver {
    public static void main(String args []) {
      Socket s=new Socket("localhost",2345);
      FileOutputStream f=new FileOutputStream("E:\FEATUREDCOPIED.txt");
      DataInputStream din= new DataInputStream(s.getInputStream);
      byte[] b=new byte[2002];
      din.read(b,0,b.length);
      f.write(b,0,b.length);
        din.close();
        f.close();
        s.close();
    }
}

Error:

  1. getOutputStream 无法解析或者不是服务器中的字段
  2. getInputStream 无法解析或不是客户端中的字段

请帮我解决这个问题。

getInputStreamgetOutputStream 是方法,所以你必须调用它们,这意味着在它们的名称后面加上括号:s.getInputStream() 而不是 s.getInputStream.

注意括号,因为您在 Socket 中调用了一个方法:s.getOutputStream**()** s.getInputStream**()**

**更正代码**

修复的错误是

1.Change s.getInputStream -> s.getInputStream()

2.Change s.getOutputStream -> s.getOuputStream()

3.Create文件读写循环,否则数据丢失

Server class:

  
import java.net.*;
import java.io.*;
public class fileserver {
        static Final int PORT_NUM= 4465;
    public static void main(String args []) {
     try {
            ServerSocket ss=new ServerSocket(PORT_NUM);
            Socket s= ss.accept();// new socket is created which is now connected with client.
            FileInputStream fin=new FileInputStream("D:\FEATURED.txt");
            DataOutputStream dout= new DataOutputStream(s.getOutputStream());
            byte[] Buffer=new byte[20];
            int read;//read variable is use to read length of bytes which is read by fin in one iterations.
            while((read=fin.read(Buffer,0,Buffer.length))>0){
                dout.write(Buffer,0,read);
            }
            dout.close();
            fin.close();
            s.close();
        }
     catch(Exception e) {
         System.out.println(e.getMessage());
     }
    }
}

Client class :

import java.io.*;
import java.net.*;
import java.lang.*;

public class clientserver {
        static Final int PORT_NUM=4465;
    public static void main(String args []) {
     try {
        Socket s=new Socket("localhost",PORT_NUM);
        FileOutputStream fout=new FileOutputStream("E:\FEATUREDCOPIED.txt");// Output file stream is used to write on file
        DataInputStream din= new DataInputStream(s.getInputStream());// input stream is created to read data from buffer.
        byte[] Buffer=new byte[20];
        int readlen;//readlen variable is to read the length of bytes which is read by din in one iteration. 
        while((readlen=din.read(Buffer,0,Buffer.length))>0){
            f.write(Buffer,0,readlen);
        }
        din.close();
        fout.close();
        s.close();
     }
     catch(Exception e) {
         System.out.println(e.getMessage());
     }
    }
}