OutputStream outs = s.getOutputStream();
PrintWriter pw = new PrintWriter(outs,true);
pw.println("上传成功");
s.close();
ss.close();
}
}
import java.io.*;import java.net.*;
public class UploadFileSocket { public static void main(String[] args) throws Exception { //建立以本机为服务器的socket对象 Socket s = new Socket("60.24.67.97",10003); //定义一个读取文本的缓冲区流对象 BufferedReader br = new BufferedReader(new FileReader(new File("H:\\r.txt"))); //获取socket输出对象 OutputStream out = s.getOutputStream(); PrintWriter pw = new PrintWriter(out,true); //读取文件内容,并输出 String line=null; while((line=br.readLine())!=null) { pw.println(line); System.out.println(line); } s.shutdownOutput(); BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()) ); System.out.println(bufin.readLine()); bufin.close(); }
}
运行时报错Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383) at java.net.ServerSocket.bind(ServerSocket.java:328) at java.net.ServerSocket.<init>(ServerSocket.java:194) at java.net.ServerSocket.<init>(ServerSocket.java:106) at com.itheima.UploadFileSocketServer.main(UploadFileSocketServer.java:15)