本帖最后由 陈兆辉 于 2014-10-10 10:41 编辑
如题,写多线程上传文件的例子,我运行服务端的时候没问题,一运行客户端就发生绑定异常:java.net.BindException: Address already in use: JVM_Bind。无论我改端口号还是重启eclipse或者终止javax都没有用。最后也没找到错误在哪,求大家帮我看看哪错了。
首先是服务端的代码:
- package review.part4;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- class ServerThread implements Runnable{
- //接收客户端对象
- private Socket client;
- ServerThread(Socket client){
- this.client = client;
- }
-
- @Override
- public void run() {
- //打印ip信息
- String ip = client.getInetAddress().getHostAddress();
- System.out.println(ip+": connected");
-
- //创建要上传的文件
- int count = 1;
- try {
- File file = new File(ip+"("+(count)+").mp3");
- while(file.exists()){
- file = new File(ip+"("+(++count)+").mp3");
- }
-
- //创建网络读取流和文件写入流。
- BufferedInputStream bufIn = new BufferedInputStream(client.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
-
- //循环读取,写入所创建的文件中
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = bufIn.read(buf))!=-1){
- bos.write(buf, 0, len);
- bos.flush();
- }
-
- //创建网络写入流写出回馈信息,然后关闭资源
- PrintWriter pw = new PrintWriter(client.getOutputStream());
- pw.println("ok,传完啦!");
- bos.close();
- client.close();
-
- } catch (IOException e) {
- throw new RuntimeException(ip+"传输失败!");
- }
- }
-
- }
- class ServerConcurrencyUpload {
- public static void main(String[] args) throws IOException {
- //建立服务器端点,循环接收请求,并建立线程。
- ServerSocket server = new ServerSocket(10086);
- while(true){
- Socket client = server.accept();
- new Thread(new ServerThread(client)).start();
- }
- }
- }
复制代码
然后是客户端的代码:
- package review.part4;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- class ClientConcurrencyUpload {
- public static void main(String[] args) throws UnknownHostException, IOException {
- //建立客户端
- Socket socket = new Socket(InetAddress.getLocalHost(),10086);
- //根据所传路径建立文件,并检查所传路径是否正确
- File file = new File(args[0]);
- //是否选择了一个jpg文件
- if(!(args.length==1 && args[0].endsWith(".mp3"))){
- System.out.println("请选择一个MP3格式的文件");
- socket.close();
- return;
- }
- //文件是否存在
- if(!(file.exists() && file.isFile())){
- System.out.println("所选文件不存在!");
- socket.close();
- return;
- }
- //文件大小检查
- if(file.length()>5*1024*1024){
- System.out.println("文件不能超过5M");
- socket.close();
- return;
- }
-
- //检查正确,开始读取所选文件,然后上传
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
- BufferedOutputStream bufOut = new BufferedOutputStream(socket.getOutputStream());
-
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = bis.read(buf))!=-1){
- bufOut.write(buf,0,len);
- bufOut.flush();
- }
-
- //关闭写入流,读取服务器反馈信息,关闭资源
- socket.shutdownOutput();
- BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- System.out.println(reader.readLine());
- bis.close();
- socket.close();
- }
- }
复制代码
|