-1

Server 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();
    }   
}
    

Client 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 cannot be resolved or is not a field in server
  2. getInputStream cannot be resolved or is not a field in client

please help me to resolve this query.

Tushar Jain
  • 134
  • 1
  • 12

3 Answers3

2

getInputStream and getOutputStream are methods, so you must call them which means putting parenthesis after their name: s.getInputStream() instead of just s.getInputStream.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

Mind the paranthesis, since you call a method in Socket: s.getOutputStream**()** s.getInputStream**()**

Alex Funk
  • 346
  • 2
  • 7
-1

**Corrected code **

Fixed Bugs are

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

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

3.Create a loop for reading and writing in file otherwise data might loss.

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());
     }
    }
}
Tushar Jain
  • 134
  • 1
  • 12
  • Providing copy-pasteable code without comment is a terrible idea, because it teaches nothing. Also, you didn't even fix the broken read-without-loop. – Joachim Sauer Jun 30 '20 at 17:35
  • @Joachim Sauer I am unable to understand your comment . Can you please elaborate it? Then i can fix that problem which you want to specify. – Tushar Jain Jul 01 '20 at 07:20
  • There's two problems: 1. you post code without explanation of what you fixed, this doesn't help with learning. 2. You call `read` without checking the return value. `read` is not guaranteed to read the full length of the `InputStream` and calling it outside of a loop is almost always wrong. – Joachim Sauer Jul 01 '20 at 07:52
  • @JoachimSauer i got your point you are right , i fixed the bug. – Tushar Jain Jul 02 '20 at 02:48