0

在我的 TCP 套接字程序中,我必须将数据从客户端发送到服务器。在服务器端,我必须读取流并将其写入文件。但是文件被创建了,里面没有写任何东西。

发送文件的客户端编码:

try
{

    Socket ss = new Socket("localhost", 5010);

    BufferedOutputStream put = new BufferedOutputStream(ss.getOutputStream());
    BufferedReader st = new BufferedReader(new InputStreamReader(ss.getInputStream()));

    File f = new File("e://read.txt");
    FileInputStream fis = new FileInputStream(f);
    byte buf[] = new byte[1024];
    int read;
    while((read = fis.read(buf, 0, 1024)) != -1)
    {
        put.write(buf,0,read);
        put.flush();
    }
    //d.close();
    System.out.println("File transfered");
    ss.close();

}
catch(Exception e)
{
    System.out.println(e);
}

服务器读取输入流并将其写入文件:

try
{
    ServerSocket ss = new ServerSocket(5010);
    Socket s = ss.accept();
    BufferedReader get = new BufferedReader(new InputStreamReader(s.getInputStream()));

    FileWriter writedata = new FileWriter("c://write.txt");

    BufferedWriter bw = new BufferedWriter(writedata);

    String line=bw.toString();
    while ((line = get.readLine()) != null) {
        bw.write(line + "\n");
    }
}
catch(Exception e)
{
    System.out.println(e);
}

问题是什么?

4

2 回答 2

2

您忘记了 bw.close 和 bw.flush ....以下是有效的代码...

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class TestServer
{
    public static void main(String[] args)
    {
        try
        {
        ServerSocket ss=new ServerSocket(5010);
        Socket s=ss.accept();
        BufferedReader get= new BufferedReader(new InputStreamReader(s.getInputStream()));

        FileWriter writedata=new FileWriter("c://Test//testoutput.txt");

        BufferedWriter bw=new BufferedWriter(writedata);

        String line=bw.toString();
        while ((line = get.readLine()) != null) {

            bw.write(line + "\n");

        }
        bw.flush();
        bw.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TestClient
{
    public static void main(String[] args)
    {
        try
        {

           Socket ss=new Socket("localhost",5010);

           BufferedOutputStream put=new BufferedOutputStream(ss.getOutputStream());
           BufferedReader st=new BufferedReader(new InputStreamReader(ss.getInputStream()));

           File f=new File("c://Test//testinput.txt");
           FileInputStream fis=new FileInputStream(f);
           byte buf[]=new byte[1024];
           int read;
           while((read=fis.read(buf,0,1024))!=-1)
           {
               put.write(buf,0,read);
               put.flush();
           }
           //d.close();
           System.out.println("File transfered");
           ss.close();
           ss.close();


        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
于 2013-02-04T18:26:48.543 回答
0

除非您知道数据是文本,否则不要使用 Readers 和 Writers。使用 InputStreams 和 OutputStreams,并复制它们:

while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}
out.close();
in.close();

在客户端和服务器中都使用此逻辑。

笔记:

  1. flush()将一个循环放入该循环会适得其反。
  2. 如果buffer大于 4096(应该是),那么使用 a 是没有意义的BufferedInputStream
于 2013-02-04T22:52:11.507 回答