import java.io.*;
import java.net.*;
import java.util.*;
public class UploadFileSocketServer {
public static void main(String[] args) throws Exception
{
//建立服务端socket服务,并监听一个端口
ServerSocket ss = new ServerSocket(10003);
//通过accept方法获取链接过来的客户端对象
Socket s = ss.accept();
//获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据
BufferedReader bufin =
new BufferedReader(new InputStreamReader(s.getInputStream()) );
//定义一个输出对象
PrintWriter out = new PrintWriter(new FileWriter("H:\\server.txt"),true);
//读取文件内容,并输出
String line=null;
while((line=bufin.readLine())!=null)
{
System.out.println(line);
out.println(line);
}
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)
求大神帮忙,想了好长时间了,没有弄明白,求帮忙!!
|