UDP知识点与案例:
DatagramPacket dp = new DatagramPacket(buf, buf.length); 后面的字节数组用于接收发进来的数据报。
DatagramPacket dp = new DatagramPacket(buf, buf.length,new InetSocketAddress("127.0.0.1", 5678));后面的inetsocketaddress是用户发送数据报的目标地址和端口号,以及发送的字节数组长度与字节数组。
DatagramSocket ds = new DatagramSocket(5678);//创建一个数据报套接字并绑定到本地具体的端口上。
Constructs a datagram socket and binds it to the specified port on the local host machine. The socket will be bound to the wildcard
address, an IP address chosen by the kernel.
1 import java.net.*; 2 import java.io.*; 3 4 public class TestUDPClient 5 { 6 public static void main(String args[]) throws Exception 7 { 8 long n = 100000L; 9 ByteArrayOutputStream baos = new ByteArrayOutputStream();10 DataOutputStream dos = new DataOutputStream(baos);11 dos.writeLong(n);12 13 byte[] buf = baos.toByteArray();//转换为字节数组14 System.out.println(buf.length);15 DatagramPacket dp = new DatagramPacket(buf, buf.length,new InetSocketAddress("127.0.0.1", 5678));16 DatagramSocket ds = new DatagramSocket(9999);17 ds.send(dp);18 ds.close();19 20 }21 }
import java.net.*;import java.io.*;public class TestUDPServer{ public static void main(String args[]) throws Exception { byte buf[] = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, buf.length); DatagramSocket ds = new DatagramSocket(5678); while(true) { ds.receive(dp); ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readLong()); } }}
TCP知识点与典型案例:
1.客户端要创建Socket并且指定自己的ip地址和要绑定的端口号:Socket s1 = new Socket("127.0.0.1", 8888);
2.服务端要穿件serversocket并且连接到哪个端口号:ServerSocket s = new ServerSocket(8888);
3.服务器端的ServerSocket中的accept()方法用于接收客户端的连接请求,返回值为一个socket,常常用socket的getinputstream或getoutputstream方法获取流来进行交流。
1 * 要 点: 2 * 1. Java Socket编程步骤 3 * 2. Socket/ServerSocket类用法 4 * 3. 通过Socket对象可以获取通信对方Socket的信息 5 */ 6 import java.net.*; 7 import java.io.*; 8 9 public class TestServer {10 public static void main(String args[]) {11 try { 12 ServerSocket s = new ServerSocket(8888);13 while (true) {14 Socket s1 = s.accept();15 OutputStream os = s1.getOutputStream();16 DataOutputStream dos = new DataOutputStream(os);17 dos.writeUTF("Hello," + s1.getInetAddress() + 18 "port#" +s1.getPort() + " bye-bye!");19 dos.close();20 s1.close();21 }22 }catch (IOException e) {23 e.printStackTrace();24 System.out.println("程序运行出错:" + e); 25 }26 }27 }
1 /* 范例名称:简单的client/server程序 2 * 源文件名称:TestClient.java/TestServer.java 3 * 要 点: 4 * 1. Java Socket编程步骤 5 * 2. Socket/ServerSocket类用法 6 * 3. 通过Socket对象可以获取通信对方Socket的信息 7 */ 8 9 import java.net.*;10 import java.io.*;11 12 public class TestClient {13 public static void main(String args[]) {14 try {15 Socket s1 = new Socket("127.0.0.1", 8888);16 InputStream is = s1.getInputStream();17 DataInputStream dis = new DataInputStream(is);18 System.out.println(dis.readUTF());19 dis.close();20 s1.close();21 } catch (ConnectException connExc) {22 connExc.printStackTrace();23 System.err.println("服务器连接失败!");24 } catch (IOException e) {25 e.printStackTrace();26 }27 }28 }