网络编程常用的class
- java.net.DatagramPacket
- java.net.DatagramSocket
- java.net.InetAddress
- java.net.Inet4Address
- java.net.Inet6Address
- java.net.ServerSocket
- java.net.ServerSocket
UDP-101
用户数据报协议 UDP(User Datagram Protocol),是无连接的,即不可靠传输。
先运行UDPRecv,运行的程序处于阻塞状态,直到有数据接收到为止,即UDPSend运行后,UDPRecv接收到数据后才停止。
- 先运行UDPRecv
- 再运行UDPSend
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.io.IOException; import java.net.*;; public class UDPRecv { public static void main(String[] args) throws IOException{ DatagramSocket ds = new DatagramSocket(3000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); ds.close(); String strRecv = new String(dp.getData(), 0, dp.getLength()); System.out.println(strRecv); System.out.println(dp.getAddress().getHostAddress()); System.out.println(dp.getPort()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.io.*; import java.net.*; public class UDPSend { public static void main(String[] args) throws IOException{ DatagramSocket ds = new DatagramSocket(); String str = "hello!"; byte[] buf = str.getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"),3000); ds.send(dp); ds.close(); } }
|
TCP-101
传输控制协议 TCP(Transmission Control Protocol),是面向连接的,可靠传输。
先运行TCPServer,运行的程序处于阻塞状态,直到有数据接收到为止,即TCPClient运行后,TCPServer接收到数据后才停止。
- 先运行TCPServer
- 再运行TCPClient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.io.OutputStream; import java.io.InputStream; public class TCPServer { public static void main(String[] args) { try{ ServerSocket ss = new ServerSocket(8001, 3, InetAddress.getByName("10.3.42.166")); Socket s = ss.accept(); OutputStream ops = s.getOutputStream(); String str = "hello "+s.getInetAddress().getHostAddress(); ops.write(str.getBytes()); InputStream ips = s.getInputStream(); byte[] buf = new byte[1024]; int len = ips.read(buf); System.out.println(new String(buf,0,len)); ips.close(); ops.close(); s.close(); ss.close(); }catch(Exception e){ System.out.println(e.getMessage()); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.net.InetAddress; import java.net.Socket; import java.io.*; public class TCPClient { public static void main(String[] args){ try{ Socket s = new Socket(InetAddress.getByName("10.3.42.166"),8001); OutputStream ops = s.getOutputStream(); String str = "from client "+s.getInetAddress().getHostAddress(); ops.write(str.getBytes()); InputStream ips = s.getInputStream(); byte[] buf = new byte[1024]; int len = ips.read(buf); System.out.println(new String(buf,0,len)); ops.close(); s.close(); }catch(Exception e){ System.out.println(e.getMessage()); } } }
|
Tcp协议保证收发双方正确地数据传送,但没法保证接受方正确地理解发送方的数据的意义。
在Tcp上有各种网络应用,有的用于在两个程序之间传送邮件(stmp和pop3),
- 有的是在两个程序之间传送文件(ftp)
- 有的是在两个程序之间传送www网页(http)
只有基于这些协议,接收方才能正确理解发送方的数据。
总之,把Tcp协议比作电话,把各种网络应用协议(ftp,smtp,pop3,http等)比作各种语言。
当然,我们的语言不一定非要通过电话系统传递一样的道理,我们的应用程序协议也可以在其他的网络通信协议(非Tcp协议)上传送。