1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 网络编程 TCP/UDP

网络编程 TCP/UDP

时间:2023-01-16 10:13:06

相关推荐

网络编程 TCP/UDP

网络编程

打电话–连接–接了–通话—>TCP连接

发短信------发送了就完事了---->UDP连接

网络编程的目的:

传播交流信息,数据交换、资源共享、通信

想要打到这个效果需要什么:

1.如何准确的定位到网络上的一台主机 192.168.16.124 :端口,定位到这个计算机上的某个资源

2.找到了这个主机如何传输数据?

javaweb:网页编程 B/S

网络编程:TCP/IP C/S

网络通信的要素

如何实现网络的通信:

通信双方的地址

IP端口号

规则:网络通信的协议

http,ftp,smtp,tcp,udp。。。。。

TCP/IP参考模型;

小结

1.网络编程中有两个主要问题

如何准确的定位到网络上的一台或多台主机找到主机之后如何进行通信

2.网络编程中的要素

IP和端口号 IP类和端口类网络通信协议 TCP、UDP

3.万物皆对象

IP

IP地址:InetAddress

唯一定位一台网络上的计算机127.0.0.1 : 代表本机localhostIP地址的分类

IPV4/IPV6

IPV4 127.0.0.1 四个字节组成。 0255,42亿,30亿在北美,亚洲4亿.就用尽了;IPV6 fe80::a507:be9d:af1c:41ce%15 ,128位 ,八个无符号整数!

2001:0bb2:aaaa:0015:0000:0000:1aaa:1312

公网地址(互联网)和私网地址(局域网)

ABCD类地址192.168.xx.xx,专门给组织内部使用的 域名:记忆IP问题! IP:

package com.cjp.ip;import .InetAddress;import .UnknownHostException;//测试IPpublic class TestInetAddress {public static void main(String[] args) {try {//查询本机IP地址InetAddress inetAddress = InetAddress.getByName("127.0.0.1");System.out.println(inetAddress);InetAddress inetAddress1 = InetAddress.getByName("localhost");System.out.println(inetAddress1);InetAddress inetAddress2 = InetAddress.getLocalHost();System.out.println(inetAddress2);//查询网站IP地址InetAddress inetAddress3 = InetAddress.getByName("");System.out.println(inetAddress3);//常用方法System.out.println(inetAddress3.getAddress()); //返回数组System.out.println(inetAddress3.getCanonicalHostName()); //规范的名字System.out.println(inetAddress3.getHostAddress()); //ipSystem.out.println(inetAddress3.getHostName()); //域名 或者自己电脑的名字} catch (UnknownHostException e) {e.printStackTrace();}}}

输出结果:

/127.0.0.1

localhost/127.0.0.1

DESKTOP-A6EE6VO/192.168.242.1

/112.80.248.75

[B@19dfb72a

112.80.248.75

112.80.248.75

端口

端口表示计算机上的一个程序的进程

不同的进程有不同的端口号!用来区分软件!端口被规定0-65535被分为TCP端口和UDP端口:65535*2 TCP端口和UDP端口可以重名不冲突,单个协议下则不能重名端口分类 公有端口0-1023 HTTP:80HTTPS:443FTP:21Telent:23 程序注册端口 :1024-49151,分配给用户或程序 Tomcat:8080MySQL:3306Oracle:1521 动态、私有:49152-65535

1 netstat -ano //查看所有端口

2 netstat -ano findstr “5900” //c查看指定的端口

3tasklist|findstr “8696” # //查看端口的制定进程

package com.cjp.ip;import .InetSocketAddress;//public class TestInetSocketAddress {public static void main(String[] args) {InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);System.out.println(socketAddress);InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080);System.out.println(socketAddress2);System.out.println(socketAddress.getAddress());System.out.println(socketAddress.getHostName());//地址System.out.println(socketAddress.getPort());//端口}}

输出结果:

/127.0.0.1:8080

localhost/127.0.0.1:8080

/127.0.0.1

127.0.0.1

8080

通信协议

协议:相当于约定,就好比我们现在使用的是汉语。

网络通信协议:速率、传输码率、代码结构、传输控制…

问题:非常的复杂

TCP/IP协议簇:实际上是一组协议

TCP:用户传输协议UDP用户数据报协议

出名的协议:

TCP:IP:网络互连协议

TCP UDP对比

TCP:打电话

连接,稳定三次握手 四次挥手(最少需要三次交流才能保证连接)(四次交流断开)客户端、服务端传输完成释放连接,效率低

UDP:发短信

不连接 不稳定客户端、服务端:没有明确的界限不管有没准备好,都可以发给你导弹DDOS:洪水攻击!(饱和攻击)

TCP

客户端

1.连接服务器Socket

2.发送消息

package com.cjp.tcp;import java.io.IOException;import java.io.OutputStream;import .InetAddress;import .Socket;import .UnknownHostException;//客户端public class TcpClientDamo01 {public static void main(String[] args) {Socket socket = null;OutputStream os = null;try {//1.要知道服务器的地址、端口号InetAddress serverIP = InetAddress.getByName("127.0.0.1");int port = 8787;//2.创建一个Socket连接socket = new Socket(serverIP,port);//3.发送消息 IO流os.write("你在干嘛呀?".getBytes());} catch (Exception e) {e.printStackTrace();}finally {if (os!=null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}if (socket!=null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}}

服务器

1.建立服务的端口ServerSocket

2.等待用户的连接 accept

3.接受用户的消息

package com.cjp.tcp;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import .ServerSocket;import .Socket;//服务端public class TcpServerDamo01 {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;ByteArrayOutputStream baos = null;try {//1.要有一个地址serverSocket = new ServerSocket(8787);//2.等待客户端连接过来socket= serverSocket.accept();//3.读取客户端消息is= socket.getInputStream();//管道流baos= new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer))!=-1){baos.write(buffer,0,len);}System.out.println(baos.toString());} catch (IOException e) {e.printStackTrace();}finally {//关闭流if (baos != null) {try {baos.close();} catch (IOException e) {e.printStackTrace();}}if (is!=null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if (socket!=null){try {socket.close();} catch (IOException e) {e.printStackTrace();}}if (serverSocket!=null) {try {serverSocket.close();} catch (IOException e) {e.printStackTrace();}}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
网络编程 tcp udp

网络编程 tcp udp

2024-03-18

【网络编程--UDP TCP】

【网络编程--UDP TCP】

2020-01-22

网络编程-tcp/udp

网络编程-tcp/udp

2023-11-06

TCP/UDP(网络编程)

TCP/UDP(网络编程)

2022-02-12