readUTF报错 connection reset
public class TClient {
public static void main(String[] args) throws UnknownHostException, IOException {
//从磁盘上读,输入流 字节流
InputStream is=new FileInputStream("d:/zz.txt");
//写到管道中
Socket client=new Socket("localhost", 8000);
OutputStream os=client.getOutputStream();
//边读边写 while 因为我们不知道文件的大小
int len=is.read();//初始变化量 文件末尾是-1
while(len!=-1){
os.write(len);
len=is.read();
}
client.shutdownOutput();
//接收服务器响应
DataInputStream dis=new DataInputStream(client.getInputStream());
System.out.println(dis.readUTF());
client.shutdownInput();
//关闭
dis.close();
os.close();
is.close();
client.close();
}
}
client端
public class TServer {
public static void main(String[] args) throws IOException {
//从管道内读数据
ServerSocket server=new ServerSocket(8000);
//监听
Socket client= server.accept();
//接收客户端socket中流的数据 对服务器来说 就是输入流
InputStream is= client.getInputStream();
//写到磁盘上
OutputStream os=new FileOutputStream("kk.txt");
int len=0;
while((len=is.read())!=-1){
os.write(len);
}
client.shutdownInput();
//服务器给客户端一个响应
DataOutputStream dos=new DataOutputStream(client.getOutputStream());
dos.writeUTF("上传成功");
client.shutdownOutput();
//关闭流
dos.close();
os.close();
is.close();
client.close();
server.close();
}
}
|
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|