0

请帮帮我...正在发送文件名,客户端也读取了文件的内容,但内容无法发送到服务器,反之亦然...这是服务器端代码。 ..

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

public class Server {

public static void main(String [] args) throws Exception{
ServerSocket s_sock = new ServerSocket(4567);
System.out.println("Server ready for connection...");
Socket c_sock = s_sock.accept(); 
System.out.println("Connection established ...server listening...");

    // reading the filename from client

InputStream is1 = c_sock.getInputStream();
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
String f_name1 = br1.readLine();
System.out.println("File sent from client is :" + f_name1);

    // receiving file contents from client and copying

String contents1 = null;
InputStream is2 = c_sock.getInputStream();
    BufferedReader br2 =  new BufferedReader(new InputStreamReader(is2));
FileWriter fw1 =  new FileWriter("from_Client.txt");
BufferedWriter bw1 = new BufferedWriter(fw1);

        while((contents1= br2.readLine()) != null) {
            bw1.write(contents1);
        }   
System.out.println("File copied successfully");


    // reading the filename from client and sending contents to client

InputStream is3 = c_sock.getInputStream();
BufferedReader br3 = new BufferedReader(new InputStreamReader(is3));
String f_name2 = br3.readLine();
System.out.println("File required by client is :" + f_name2);

String contents2 = null;
    FileReader fr2 = new FileReader(f_name2);
    BufferedReader br4 =  new BufferedReader(fr2);
OutputStream os1 = c_sock.getOutputStream();
PrintWriter pw1 = new PrintWriter(os1, true);

while((contents2 = br4.readLine()) != null) {
            pw1.println(contents2);
        }   



  }

 }        

这是客户端代码

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

public class Client {

public static void main(String args[]) throws Exception {
Socket c_sock = new Socket("127.0.0.1", 4567);

    //specify file name to send to server   

System.out.print("Enter the file name to upload to the server:"+"\t");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String f_name1 = br1.readLine();
OutputStream os1 = c_sock.getOutputStream();
PrintWriter pw1 = new PrintWriter(os1, true);
pw1.println(f_name1);

    //sending file contents to server

String contents1 = null;
    FileReader fr1 = new FileReader(f_name1);
    BufferedReader br2 =  new BufferedReader(fr1);
OutputStream os2 = c_sock.getOutputStream();
PrintWriter pw2 = new PrintWriter(os2, true);

while((contents1 = br2.readLine()) != null) {

    pw2.println(contents1);

      } 

    //specify file name to send to server   

System.out.println();
System.out.print("Now enter the file name to download from server:"+"\t");
BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
String f_name2 = br3.readLine();

OutputStream os3 = c_sock.getOutputStream();
PrintWriter pw3 = new PrintWriter(os3, true);
pw3.println(f_name2); 

    // receiving file contents from server and copying

String contents2 = null;
InputStream is1 = c_sock.getInputStream();
    BufferedReader br4 =  new BufferedReader(new InputStreamReader(is1));

FileWriter fw1 =  new FileWriter("from_Server.txt");
BufferedWriter bw1 = new BufferedWriter(fw1);

        while((contents2 = br4.readLine()) != null) {
            bw1.write(contents2);
        }   
System.out.println("File copied successfully");



}

}
4

1 回答 1

0

摆脱第二个 BufferedReader 并将第一个用于所有内容。他们正在互相窃取数据。

同样适用于客户端,也适用于两个 PrintWriter。

当然,整个方法只适用于文本文件。如果您通过此代码发送二进制文件,它们将被 char 转换损坏。

于 2013-10-23T07:30:09.897 回答