weixin_42472582
weixin_42472582
2011-08-29 10:06

Socket编程java.net.BindException: Address already in use: JVM_Bind

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

本人最近想学Socket编程:无奈遇到个问题.不知道是啥原因,还请各位不吝赐教.
问题是这样的:我想用Socket在服务端读取文本文件,第一次运行的时候,什么都没数出来,(不停止第一次开启的服务)第二次输出了文本内容,却报了个地址被占用的错.这是为什么呢?
Socket客户端:
package socket;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;

public class socketClient {
private String ip;
public socketClient() {
// TODO Auto-generated constructor stub
}
public socketClient(String ip){
this.ip=ip;
}
public void client(){
PrintWriter print=null;
BufferedReader read=null;
File file=null;
try {
Socket ket=new Socket(ip,9000);//与服务器取得链接.
file=new File("e:\我的地址.txt");
read=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String readLine=null;
print=new PrintWriter(ket.getOutputStream(),true);
while((readLine=read.readLine())!=null){
print.print(readLine);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
read.close();
print.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

}

Socket服务器:
package socket;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class SocketRemot {
private Map map=new HashMap();
public void reMoteRun(){
BufferedReader read=null;
PrintWriter write=null;
try {
while(true){
ServerSocket soc=new ServerSocket(9000);//为服务器Socket指定端口
Socket socket=soc.accept();//服务器端阻塞,一直等待客户端来访问.
map.put(socket.getPort()+"",socket);
System.out.println("我在等待.......");
//读取客户端文件.
//得到socket读取文件的流//字节流
/*
* InputStream是用于读取二进制文件的流,而服务器端读取的文件很有可能是文本形式的.所以要转换为字符流
* 字节流和字符流之间的转换通过InputStreamReader,和OutputStreamWrite来转换.
*/
read=new BufferedReader(new InputStreamReader(socket.getInputStream()));//获取读文件的字符流
write=new PrintWriter(socket.getOutputStream(),true);//通过现有的output流来创建新的printWrite流
System.out.println(read.readLine());
// String str=null;
// while((str=read.readLine())!=null){
// System.out.println(read.readLine());//边读边写.
// }
// write.close();
// read.close();
// socket.close();

        }


    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            if(read!=null){
                read.close();
            }
            if(write!=null){
                write.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

public void closeSocket(){
    Iterator tor=map.values().iterator();
    while(tor.hasNext()){
        Socket soc=(Socket)tor.next();
        if(!soc.isClosed()){
            try {
                soc.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

}

Test:
package socket;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {
public static void main(String[] args) {
SocketRemot remot=new SocketRemot();
socketClient ket=new socketClient(getAddIp());
remot.reMoteRun();
ket.client();
remot.closeSocket();

}

public static String getAddIp(){
 String serverIp=null;
   try {
    serverIp = InetAddress.getLocalHost().toString();
    serverIp = serverIp.substring(serverIp.indexOf("/")+1);
    System.out.println(serverIp);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } 
    return serverIp;

}
}

error:
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.(Unknown Source)
at java.net.ServerSocket.(Unknown Source)
at socket.SocketRemot.reMoteRun(SocketRemot.java:25)
at socket.Test.main(Test.java:10)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

9条回答

为你推荐

换一换