java基础教程28

java基础教程28七 网络编程 7 1 IP 协议 最重要的贡献是 IP 地址 7 2 TCP 和 UDP 协议 TCP 快 可靠传输 发送时必须建立连接 三次握手协议 UDP 慢 不可靠传输 发送时无须建立连接 7 3 TCP 的 TCP Server 和 TCP Client 注意 要首先启动 Server 再启动 Client 端口号是应用的入口 有两个字节

大家好,我是讯享网,很高兴认识大家。



七、网络编程

7.1.IP协议

  最重要的贡献是IP地址

7.2.TCP和UDP协议

  TCP(快)可靠传输,发送时必须建立连接(三次握手协议 )

  UDP(慢)不可靠传输,发送时无须建立连接

7.3.TCP 的TCP Server和TCP Client

    

  注意,要首先启动Server,再启动Client

  端口号是应用的入口,有两个字节,所以每个服务器最多能运行65536应用程序,而TCP的端口和UDP的端口不一样,定义端口时,尽量定义1024以上的

  特别典型的端口号,http  80

//TCP Server import java.net.*; import java.io.*; public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); while(true) { Socket s = ss.accept();//阻塞函数,一直在这等,直到有访问,才会继续执行 System.out.println("a client connect!"); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); dis.close(); s.close(); } } }
讯享网

讯享网//TCP Client import java.net.*; import java.io.*; public class TCPClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1", 6666); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); Thread.sleep(3000); dos.writeUTF("hello server!"); dos.flush(); dos.close(); s.close(); } } 

  

7.4.UDP   Server和Client(UDP只能传送字节数组)

//UDP Server import java.net.*; 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); System.out.println(new String(buf,0,dp.getLength()));//String的构造函数 } } }

   

讯享网//UDP Client import java基础教程28 java.net.*; public class TestUDPClient { public static void main(String args[]) throws Exception { byte[] buf = (new String("Hello")).getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678) ); DatagramSocket ds = new DatagramSocket(9999);//client自己的端口,因为UDP不是面向连接的 ds.send(dp); ds.close(); } } 

  

//TestUDP Client import java.net.*; import java.io.*; public class TestUDPClient { public static void main(String args[]) throws Exception { long n = 10000L; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeLong(n); byte[] buf = baos.toByteArray();//将long型数据转化成字符数组的形式,进行输入 System.out.println(buf.length); DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678) ); DatagramSocket ds = new DatagramSocket(9999); ds.send(dp); ds.close(); } } 

  

//TestUDP Server 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());//将字符数组读成long型 } } } 
小讯
上一篇 2024-12-31 14:22
下一篇 2024-12-30 23:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/9042.html