在我的 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);
}
问题是什么?