黑马程序员-网络编程- 第一天

来源:互联网 发布:非洲网络制式 编辑:程序博客网 时间:2024/05/17 04:02

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

网络模型:

1,OSI参考模型   ISO组织制定

2,TCP/IP参考模型

网络通讯要素:

1,IP地址

2,端口号

3,传输协议


网络编程过程:


0到1024端口大部分被系统保留,端口范围:0到65535


tomcat:8080

MySQL:3306


TCP和UDP

1,UDP将数据及源和目的封装成数据包,不需要建立链接,每个数据报的大小在现状在64k内,因无连接,所以是不可靠的协议,不需要建立连接,速度快。

2,TCP建立连接,形成传输数据的通道,在连接中进行大数据量传输,通过三次握手完成连接,是可靠协议,必须建立连接,效率会稍低。


Socket:

网络编程就是Socket编程,Socket就是为网络服务提供的一种机制,通信的两端都有Socket,网络通信其实就是Socket间的通信,数据在两个Socket间通过IO传输。


需求:通过udp传输方式,将一段文字数据发送出去。

思路:

1,建立updsocket服务。

2,提供数据,并将数据封装到数据包中。

3,通过socket服务的发送功能,将数据包发出去。

4,关闭资源。




需求:

定义一个应用程序,用于接收udp协议传输的数据并处理。

思路:

1,定义udpsocket服务,通常会监听一个端口,其实就是给这个接受网络应用程序定义数字标识。方便于明确哪些数据过来该应用程序可以处理。

2,定义一个数据包,因为要存储接收到的字节数据。因为数据包对象中有更多功能可以提取字节数据中的不同数据。

3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中。

4,通过数据包对象的特有功能,将这些不同的数据取出。打印在控制台上。

5,关闭资源。

例如:

import java.net.*; class UdpSend {public static void main(String[] args) throws Exception{//1,创建udp服务,通过DatagramSocket对象。DatagramSocket ds = new DatagramSocket();//2,确定数据,并封装成数据包。byte[] buf = "udp ge men lai le".getBytes();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),13330);//3,通过socket服务,将已有的数据包发出去,通过send方法。ds.send(dp);//4,关闭资源。ds.close();}}class UdpRece{public static void main(String[] args) throws Exception{//1,创建udpsocket,建立端点。DatagramSocket ds = new DatagramSocket(13330);while(true){//2,定义数据包,用于存储数据。byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf,buf.length);//3,通过服务的receive方法将收到数据存入数据包中。ds.receive(dp); //阻塞式方法  //4,通过数据包的方法获取其中的数据。String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());int port = dp.getPort();System.out.println(ip + "::" + data + "::" +port);//5,关闭资源ds.close();}}}



传输键盘输入信息:

import java.net.*;import java.io.*;class UdpSend2 {public static void main(String[] args) throws Exception{DatagramSocket ds = new DatagramSocket();BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;while((line = bufr.readLine())!=null){if("886".equals(line))break;byte[] buf = line.getBytes();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10001);ds.send(dp);}ds.close();}}class UdpRece2{public static void main(String[] args) throws Exception{DatagramSocket ds = new DatagramSocket(10001);while(true){byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf,buf.length);ds.receive(dp);String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());System.out.println(ip+"::"+data);}}}


简单的ChatMode:

/*编写一个聊天程序。有收数据的部分,和发数据的部分。这两部分需要同时执行。那就需要用到多线程技术。一个线程控制收,一个线程控制发*/import java.io.*;import java.net.*;class Send implements Runnable{private DatagramSocket ds;public Send(DatagramSocket ds){this.ds = ds;}public void run(){try{BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));String line = null;while((line = bufr.readLine())!=null){if("886".equals(line))break;byte[] buf = line.getBytes();DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10003);ds.send(dp);}}catch (Exception e){throw new RuntimeException("发送端失败");}}}class Rece implements Runnable{private DatagramSocket ds;public Rece(DatagramSocket ds){this.ds = ds;}public void run(){try{while(true){byte[] buf = new byte[1024];DatagramPacket dp = new DatagramPacket(buf,buf.length);ds.receive(dp);String ip = dp.getAddress().getHostAddress();String data = new String(dp.getData(),0,dp.getLength());System.out.println(ip+"::"+data);}}catch (Exception e){throw new RuntimeException("接收端失败");}}}class ChatDemo{public static void main(String[] args) {DatagramSocket sendSocket = new DatagramSocket();DatagramSocket receSocket = new DatagramSocket(10003);new Thread(new Send(sendSocket)).start();new Thread(new Rece(receSocket)).start();}}


---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net


0 0