import java.io.IOException;
import java.net.*;
public class SendUdpTest {
public static void main(String[] args) {
try {
DatagramSocket socket1 = new DatagramSocket();
InetAddress address = InetAddress.getByName("127.0.0.1");
String words [] = "welcome to java world".split(" ");
int len = words.length;
for (int i = 0 ; i < len ;i++){
byte [] byt = words[i].getBytes();
DatagramPacket packet1 = new DatagramPacket(byt,byt.length,address,2021);
System.out.println("发送:"+words[i]);
socket1.send(packet1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
socket1.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at CourseExamples.Chapter_19.CilentTest.main(CilentTest.java:16)