网络编程学习

1.1 IP

IP 在java中的class
InetAddress 无构造器 无需new

package com.shy.lesson1;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {
    public static void main(String[] args) {
        try {
            ///查询本机地址
            InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress3=InetAddress.getLocalHost();
            System.out.println(inetAddress3);
            InetAddress inetAddress4=InetAddress.getByName("localhost");
            System.out.println(inetAddress4);
            查询网站ip地址
            InetAddress inetAddress2=InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);


            常用方法
           // System.out.println(inetAddress2.getAddress());
           // System.out.println(inetAddress2.getCanonicalHostName());///获取规范名字 ip值
            System.out.println(inetAddress2.getHostAddress());
            System.out.println(inetAddress2.getHostName());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

1.2 端口

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

不同的进程有不同的端口号!!用来区分软件
被规定 0~65535

端口分类:

共有端口:
          0~1023
           HTTP:80
           HTTPS:443
           FTP:21
           Telent:23
程序注册端口:
            1024~49151, 分配用户或程序
            Tomcat: 8080
            MySQL :3306
            Oracle:1521
动态、私有:
         49152~65535
         cmd里 netstat -ano  查看所有的端口
         newtstat -ano|findstr "5900" 查看指定的端口  
         ctrl+shift+esc 打开任务管理器

1.3通信协议

网络通信协议:速率,传输码率,传输控制…

1.4 TCP

客户端

   1.连接服务器Socket
   2.发送消息

服务器

    1.建立服务的端口  ServerSocket
    2.等待用户的连接accept
    3.接受用的消息
package com.shy.lesson2;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket=null;
        OutputStream os=null;
        try {
            //1.要知道服务器的地址
            InetAddress severIp=InetAddress.getByName("127.0.0.1");
            //2创建端口号
            int port=9999;
            socket=new Socket(severIp,port);//创建一个socket连接
            os=socket.getOutputStream();
            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();
                }
            }

        }
    }
}

package com.shy.lesson2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket socket=null;
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        try {
            //1.我得有一个地址
            serverSocket=new  ServerSocket(9999);
            //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();
                }
            }
        }
        //System.out.println("It is over!");
    }
}

文件上传

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值