黑马程序员技术交流社区

标题: 聊天 [打印本页]

作者: kylin_huang    时间: 2012-10-30 01:24
标题: 聊天
package kylin.javase.udpStork;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/*
* *  聊天
* 分析
* 读取信息
* 发送信息
*
* */

public class Chat {
/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  
  new Thread(new ReceiveThread()).start();
  new Thread(new SendThread()).start();
  
  //receive();
  //send();
  
}
  
}
class ReceiveThread extends Thread
{
  public void run() {
   
   DatagramSocket re = null;
   try {
    re = new DatagramSocket(2250);
   } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  while (true) {
  byte[] buf = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf,buf.length);
  try {
   re.receive(dp);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();}
  }
}
}



class SendThread extends Thread
{
  public void run() {
   DatagramSocket send = null;
   try {
    send = new DatagramSocket(2250);
   } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   
   while (true) {
    BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    String line = null;
    try {
     while((line=bufr.readLine())!=null){
      byte[] buf = line.getBytes();
     DatagramPacket dp= new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.123"), 2250);
     send.send(dp);
}
    } catch (UnknownHostException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
   }
  }
}

  



java.net.BindException: Address already in use: Cannot bind
at java.net.TwoStacksPlainDatagramSocketImpl.bind0(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:95)
at java.net.TwoStacksPlainDatagramSocketImpl.bind(TwoStacksPlainDatagramSocketImpl.java:81)
at java.net.DatagramSocket.bind(DatagramSocket.java:376)
at java.net.DatagramSocket.<init>(DatagramSocket.java:231)
at java.net.DatagramSocket.<init>(DatagramSocket.java:284)
at java.net.DatagramSocket.<init>(DatagramSocket.java:256)
at kylin.javase.udpStork.ReceiveThread.run(Chat.java:54)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-1" java.lang.NullPointerException
at kylin.javase.udpStork.ReceiveThread.run(Chat.java:63)
at java.lang.Thread.run(Thread.java:722)
java.net.BindException: Address already in use: Cannot bind
at java.net.TwoStacksPlainDatagramSocketImpl.bind0(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:95)
at java.net.TwoStacksPlainDatagramSocketImpl.bind(TwoStacksPlainDatagramSocketImpl.java:81)
at java.net.DatagramSocket.bind(DatagramSocket.java:376)
at java.net.DatagramSocket.<init>(DatagramSocket.java:231)
at java.net.DatagramSocket.<init>(DatagramSocket.java:284)
at java.net.DatagramSocket.<init>(DatagramSocket.java:256)
at kylin.javase.udpStork.SendThread.run(Chat.java:78)
at java.lang.Thread.run(Thread.java:722)

  
  
  
  



作者: 张忠豹    时间: 2012-10-30 09:16
Address already in use: Cannot bind
意思是端口被占用了,不能绑定。最好选择一个10000以上的端口(我不是说1024以外的不行啊)
而且,当这个程序运行之后,如果你还想要再次运行,就应该修改一下端口号。因为上次的端口号在内存中还存在(意思说还在占用)。
所以,运行一次,修改一下端口号,就行了。
作者: kylin_huang    时间: 2012-10-30 21:24
太感谢了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2