Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

sockets - Getting "java.net.SocketException broken pipe" error when I try to send the file name. Please provide a solution

My server side program:

    import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPS1 {
    public static void main(String[] args) throws IOException {
        ServerSocket se=new ServerSocket(2000);
        String str;
        Socket s=se.accept();
        System.out.println("Connection Established");
        BufferedReader fn=new BufferedReader(new InputStreamReader(s.getInputStream()));
        str = fn.readLine();
        boolean b;
        FileReader f =null;
        BufferedReader ff=null;

        File r = new File(str);
        if(r.exists()){
            b=true;
        }else{
            b=false;
        }
        DataOutputStream fc=new DataOutputStream(s.getOutputStream());
        if(b==true){
        try{
            fc.writeBytes("Yes");
            }catch(Exception e){
                System.out.println(e);
            }
        }
        if(b==true){
            String qq;
            f=new FileReader(str);
            ff= new BufferedReader(f);
            while((qq=ff.readLine())!=null){
            try{
               fc.writeBytes(qq+"
");
               }catch(Exception e){
                System.out.println(e);
               }
            }

        }

        fc.close();
        
        ff.close();
        f.close();
        fn.close();
        s.close();
        se.close();
        


    }
    }

And this is the server side:

    import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.net.*;

public class TCPC1 {
    public static void main(String[] args) throws IOException {
        Socket s=new Socket("localhost",2000);
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter file name: ");
        String str= b.readLine();

        DataOutputStream d = new DataOutputStream(s.getOutputStream());
        d.writeBytes(str);

        BufferedReader inp = new BufferedReader(new InputStreamReader(s.getInputStream()));
        if(inp.equals("Yes")){
            System.out.println("File found
");
            while((str=inp.readLine())!=null){
                System.out.println(str + "
");
            }
        }else{
            System.out.println("File not found");
        }
        
        inp.close();
        d.close();
        b.close();
        s.close();
    }
   }

When I give the filename, I get "broken pipe" error because the host disconnected.Please provide a solution. In the output i run the server program first and then the client and the connection is successfully established.But then it asks for the filename and when I give that I get the broken pipe error.please Help.

question from:https://stackoverflow.com/questions/65907056/getting-java-net-socketexception-broken-pipe-error-when-i-try-to-send-the-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...