----------------------
ASP.Net+Android+IOS开发
、
href="http://edu.csdn.net"target="blank">.Net培训
、期待与您交流! ----------------------
4、端口名
1、网络编程
1)找到对方IP
2)数据要求发送到对方指定的应用程序上,为了标识这些应用程序,所以给这些网络应用程序都用数字进行标识,为了方便称呼这个数字,叫做端口,逻辑端口。
3)定义通信规则,这个通讯规则成为协议,国际组织定义了通用协议TCP/IP。
2、网络模型:
1)OSI参考模型:7层
应用层
表示层
会话层
传输层(UDP、TCP)
网络层(IP地址)
数据链路层
物理层
2)TCP/IP参考模型:4层
应用层
传输层
网际层
主机至网络层
3、IP地址:InetAddress
本地回环地址:127.0.0.1
主机名:localhost
- public static void main(String[] args) throws Exception{
- InetAddress i = InetAddress.getLocalHost();
- //获取主机名称+ip地址
- / System.out.println(i.toString());
- //获取主机地址
- / System.out.println("address:"+i.getHostAddress());
- //获取主机名称
- / System.out.println("name:"+i.getHostName());
- InetAddress ia = InetAddress.getByName("192.168.1.102");
- System.out.println("address:"+ia.getHostAddress());
- System.out.println("name:"+ia.getHostName());
- }
用于标识进程的逻辑地址,不同进程的标识
有效端口:0~65535,其中0~1024系统使用或保留端口。
5、传输协议
通讯的规则
常见协议:TCP,UDP
6、UDP
将数据及源和目的封装成数据包中,不需要建立连接
每个数据包的大小限制在64k内
因无连接,是不可靠协议
不需要建立连接,速度快
7、TCP
建立连接,形成传输数据的通道
在连接中进行大数据量传输
通过三次握手完成连接,是可靠协议
必须建立连接,效率会稍低
8、Socket
Socket就是为网络服务提供的一种机制。
通信的两端都有Socket
网络通信其实就是Socket间的通信
数据在两个Socket间通过IO传输
- /*
- 需求:通过udp传输方式,将一段文字数据发送出去
- 思路:
- 1、建立udpsocket服务
- 2、提供数据,并将数据封装到数据包中
- 3、通过socket服务的发送功能,将数据包发出去
- 4、关闭资源
- */
- public class UdpsendDemo {
- public static void main(String[] args)throws Exception {
- //1、创建udp服务,通过DatagramSocket对象
- DatagramSocket ds = new DatagramSocket(8888);
- //2、确定数据,并封装成数据包
- byte[] buf = "udp ge men lai le".getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10000);
- //3、通过socket服务,将已有的数据包发送出去,通过send方法
- ds.send(dp);
- //4、关闭资源
- ds.close();
- }
- }
- /*
- 需求:定义一个应用程序,用于接收udp协议传输的数据并处理
- 思路:
- 1、定义udpsocket,通常会监听一个端口,其实就是给这个接收网络应用程序定义数字标识
- 2、定义一个数据包,因为要存储接收到的字节数据,因为数据包对象中有更多功能可以提取字节数据中的不同数据信息
- 3、通过socket服务的receive方法将收到的数据存入已定好的数据包中
- 4、通过数据包对象的特有功能,将这些不同的数据取出,并打印在控制台上
- 5、关闭资源
- */
- public class UdpReceiveDemo {
- public static void main(String[] args) throws Exception{
- //1、创建udp socket,建立端点
- DatagramSocket ds = new DatagramSocket(10000);
- //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();
- }
- }
- /*
- 编写一个聊天程序
- 有收数据的部分,和发数据的部分
- 这两部分需要同时执行
- 那就要用到多线程技术
- 一个线程控制收,一个线程控制发
- 因为收和发动作是不一致的,所以要定义两个run方法
- 而且这两个方法要封装到不同的类中
- */
- public class CharDemo {
- public static void main(String[] args) throws Exception{
- DatagramSocket sendSocket = new DatagramSocket();
- DatagramSocket receSocket = new DatagramSocket(10000);
- new Thread(new Send(sendSocket)).start();
- new Thread(new Rece(receSocket)).start();
- }
- }
- 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.255"),10000);
- 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());
- int port = dp.getPort();
- System.out.println(ip+"::"+data+"::"+port);
- }
- }catch(Exception e) {
- throw new RuntimeException("接收端失败");
- }
- }
- }
9、TCP传输
- /*
- 演示tcp的传输的客户端和服务端的互访
- 需求:客户端服务端发送数据,服务端收到后,给客户端反馈信息
- 客户端:
- 1、建立socket服务,指定要连接主机和端口
- 2、获取socket流中的输出流,将数据写到该流中,通过网络发送给服务端
- 3、获取socket流中的输入流,将服务端反馈的数据获取到,并打印
- 4、关闭客户端资源
- */
- public class TcpClientDemo2 {
- public static void main(String[] args)throws Exception {
- Socket s = new Socket("192.168.1.102",10005);
- OutputStream out = s.getOutputStream();
- out.write("服务端,你好".getBytes());
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
- s.close();
- }
- }
- public class TcpServerDemo2 {
- public static void main(String[] args)throws Exception {
- ServerSocket ss = new ServerSocket(10005);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....connected");
- InputStream in = s.getInputStream();
- byte[] buf = new byte[1024];
- int len = in.read(buf);
- System.out.println(new String(buf,0,len));
- OutputStream out = s.getOutputStream();
- out.write("shoudaole ".getBytes());
- s.close();
- ss.close();
- }
- }
- public class TcpUploadClientTest {
- public static void main(String[] args)throws Exception {
- Socket s = new Socket("192.168.1.102",10007);
- BufferedReader bufr = new BufferedReader(new FileReader(""));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- String line = null;
- while((line= bufr.readLine())!=null) {
- out.println(line);
- }
- s.shutdownOutput();//关闭客户端的输出流,相当于给流中加入一个结束标记-1
- // out.println("over");
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufIn.readLine();
- System.out.println(str);
- bufr.close();
- s.close();
- }
- }
- public class TcpUploadServerTest {
- public static void main(String[] args)throws Exception {
- ServerSocket ss = new ServerSocket(10007);
- Socket s = ss.accept();
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter out = new PrintWriter(new FileWriter(""),true);
- String line = null;
- while((line=bufIn.readLine())!=null) {
- // if("over".equals(line))
- // break;
- out.println(line);
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("长传成功");
- out.close();
- s.close();
- ss.close();
- }
- }
---------------------- ASP.Net+Android+IOS开发 、 href="http://edu.csdn.net"target="blank">.Net培训 、期待与您交流! ----------------------详细请查看: http://edu.csdn.net