import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class lesson10s extends Thread
{
Socket s=new Socket();
lesson10s(Socket s)
{
this.s=s;

}
public void run()
{
try {

OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
os.write("hell welcome to my server host".getBytes());
byte[] buf=new byte[512];
int i=is.read(buf);
System.out.println(new String(buf,0,i).trim());
os.close();
is.close();
s.close();

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




public static void main(String[] args) 
{
ServerSocket ss;
try {
ss = new ServerSocket(6000);

while(true)
{
     Socket sk=ss.accept();
     new lesson10s(sk).start();
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}错误提示:
java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
at java.net.ServerSocket.bind(ServerSocket.java:319)
at java.net.ServerSocket.<init>(ServerSocket.java:185)
at java.net.ServerSocket.<init>(ServerSocket.java:97)
at def.lesson10s.main(lesson10s.java:43)

解决方案 »

  1.   

    谢谢你的回答 解决了错误 但是我的客户端接受不到数据,是不是哪里又错了
    这是客户端:import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;public class lesson10 {

    public static void client() 
    {
    try
    {
    Socket s=new Socket(InetAddress.getByName(null),6000);
    OutputStream os=s.getOutputStream();
    InputStream is=s.getInputStream();
    byte[] buf=new byte[100];
    int len=is.read(buf);
    System.out.println(new String(buf,0,len));
    os.write("hello i am yyw".getBytes());
    os.close();
    is.close();
    s.close();

    catch (UnknownHostException e) 
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static void main(String[] args)
    {



    client();
    }}还有 端口被占用过怎么把它释放? 呵呵问题有点多 麻烦你啦
      

  2.   

    java.net.BindException: Address already in use: JVM_Bind 去进程里把他关掉就好