java网络编程实战(1)

计算机网络:计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件以及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。

网络编程的目的:传播交流信息,数据交换,通信

如何准确定位网络上的一台主机:IP地址、端口,定位到计算机上的某个资源;

网络通信的要素:

  1. 通信双方的地址:IP地址、端口号
  2. 网络通信的协议

IP地址:InetAddress
唯一定位网络上的一台计算机,127.0.0.1:本机的localhost
IP地址的分类:IPV4/IPV6
IPV4:127.0.0.1,4个字节,2011年已经用尽;
IPV6:fe80::1ce8:f05d:1698:cdf0%5,8个无符号整数

InetAddress 类
封装计算机的 IP 地址,不包含端口号

InetAddress 类常用的方法

1 String getHostAddress() 获得 IP 地址

2 String getHostName() 获得主机名

3 static InetAddress getByName(String host) 根据主机名获得 IP 地址
域名:记忆IP问题

获取本机IP地址以及获取网站IP地址方法如下:

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

//测试IP
public class study_ip {
    public static void main(String[] args) throws UnknownHostException {
        //查询本机地址
        InetAddress address1=InetAddress.getByName("127.0.0.1");
        System.out.println(address1);
        InetAddress address3=InetAddress.getByName("localhost");
        System.out.println(address3);
        InetAddress address4=InetAddress.getLocalHost();
        System.out.println(address4);
        //查询网站地址
        InetAddress address2=InetAddress.getByName("www.baidu.com");
        System.out.println(address2);

        System.out.println(address2.getHostName());
        System.out.println(address2.getHostAddress());
        
    }
}

端口表示计算机上一个程序的进程,不同的进程有不同的端口号,用来区分软件!
被规定:0-65535
单个协议下端口号不能重复

端口分类:公有端口:0-1023
程序注册端口:1024-49151,分配用户或者程序

cmd命令:
netstat -neo:统计所有的端口
任务管理器中可以查看每个进程的端口号,(Ctrl+Shift+Esc查看任务管理器快捷键)
netstat -neo findstr “5900”:查看5900端口
tasklist findstr “8696” :查看8696端口的进程

InetSocketAddress 类
此类用于实现 IP 套接字地址 (IP 地址+端口号),用于socket 通信

InetSocketAddress 类常用的方法

1 InetAddress getAddress() 获取 InetAddress 对象

2 int getPort() 获取端口号

3 String getHostName() 获取主机名

import java.net.InetSocketAddress;

public class IntSocketAddress {
    public static void main(String[] args) {
        InetSocketAddress socketAddress1=new InetSocketAddress("127.0.0.1",8080);
        InetSocketAddress socketAddress2=new InetSocketAddress("localhost",8080);
        System.out.println(socketAddress1.getAddress());
        System.out.println(socketAddress2.getAddress());
        System.out.println(socketAddress1.getHostName());//获取地址
        System.out.println(socketAddress1.getPort());//获取端口号
    }
}

TCP通讯流程
客户端
1.连接服务器socket
2.发送消息

import java.io.ByteArrayOutputStream;
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 serverIP=InetAddress.getByName("127.0.0.1");
            //2.要知道服务器端口号
            int port=9999;
            //3.创建socket连接
            socket=new Socket(serverIP,port);
            //4.发送消息,IO流
            os=socket.getOutputStream();
        os.write("我是张晓宁".getBytes());}
        catch (IOException e){
            e.printStackTrace();
        }
        finally {
            if(socket!=null){
                try{socket.close();}
                catch(IOException e){
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try{os.close();}
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

服务端
1.建立服务的端口SeverSocket
2.等待用户的连接accept
3.接受用户的消息

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 In =null;
        ByteArrayOutputStream baos=null;
        try {
            //1.我得有一个地址
            serversocket = new ServerSocket(9999);
            //2.等待客户端连接过来
            while(true){
                socket = serversocket.accept();
                //3.读取客户端的消息
                In = socket.getInputStream();

                //管道流
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = In.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(In!=null){
                try{In.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();
                }
            }
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值