黑马程序员技术交流社区
标题:
你敢进来看看?
[打印本页]
作者:
彭宾
时间:
2012-12-24 11:24
标题:
你敢进来看看?
下面的这个代码为什么会一直等待呢?虽然复制文件的功能实现了。
import java.net.*;
import java.io.*;
class Send
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("192.168.0.104",5002);
BufferedReader br=
new BufferedReader(new FileReader("wo.java"));
BufferedWriter bwout=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader brin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=null;
while((str=br.readLine())!=null)
{
bwout.write(str);
bwout.newLine();
bwout.flush();
}
String in=brin.readLine();
System.out.println(in);
br.close();
s.close();
}
}
class Rece
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(5002);
Socket s=ss.accept();
InputStream is=s.getInputStream();
BufferedReader brin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw=
new BufferedWriter(new FileWriter("d:\\c.txt"));
BufferedWriter bwout=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String str=null;
String name=s.getInetAddress().getHostAddress();
while((str=brin.readLine())!=null)
{
bw.write(str);
bw.newLine();
bw.flush();
}
System.out.println(name);
bwout.write("上传成功!");
bwout.newLine();
bwout.flush();
s.close();
}
}
作者:
李培根
时间:
2012-12-24 12:05
本帖最后由 李培根 于 2012-12-25 09:52 编辑
主要是缺少结束标记,帮你修改了下,看红色字体的部分,修改后运行正常。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
class Send
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("127.0.0.1",10007);
BufferedReader br=
new BufferedReader(new FileReader("wo.java"));
BufferedWriter bwout=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader brin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=null;
while((str=br.readLine())!=null)
{
bwout.write(str);
bwout.newLine();
bwout.flush();
}
s.shutdownOutput();//
这里缺少结束标记,告诉服务端上传完毕,让服务端停止读取
String in=brin.readLine();
System.out.println(in);
br.close();
s.close();
}
}
class Rece
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10007);
Socket s=ss.accept();
InputStream is=s.getInputStream();
BufferedReader brin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw=
new BufferedWriter(new FileWriter("d:\\c.txt"));
BufferedWriter bwout=
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String str=null;
String name=s.getInetAddress().getHostAddress();
while((str=brin.readLine())!=null)
{
bw.write(str);
bw.newLine();
bw.flush();
}
System.out.println(name);
bwout.write("上传成功!");
bwout.newLine();
bwout.flush();
s.close();
}
}
作者:
彭宾
时间:
2012-12-24 13:34
。。。。。。。。。ok
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2