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)