Java教程

17.网络编程

本文主要是介绍17.网络编程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.atguigu.java2;

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

/*
一、网络编程中两个主要的问题:
1.定位网络上一台或多台主机:定位主机上的特定应用
2.找到主机后,可靠高效地进行数据传输

二、网络编程中两要素
1.提供具体的ip以及端口号
2.提供网络通信协议:TCP/IP参考模型:应用层、传输层、网络层、物理+数据链路层

IP和端口号
在java中使用InetAddress类代表IP
IP分类 :Ipv4 和IPv6
域名: www.baidu.com
本地回路地址:127.0.0.1 对应 localhost

端口号标识正在计算机上运行的进程

传输控制协议TCP协议
用户数据报协议UDP
 */
public class InterAddressTest {
    public static void main(String[] args) {
        try {
            InetAddress inet = InetAddress.getByName("192.168.10.14");


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

TCP

package com.atguigu.java2;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/*
TCP网络编程
练习:客户端发送文件给服务器,服务器端保存到本地,
并返回”发送成功“给客户端,并关闭相应连接
 */
public class TCPTest {
    @Test
    public void clinet(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos =null;
        try {
            //1.
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9988);
            //2
            os = socket.getOutputStream();
            //
            fis = new FileInputStream(new File("timg.jpg"));
            //
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1) {
                os.write(buffer,0,len);
            }
            //关闭数据的输出
            socket.shutdownOutput();

            //接受来自服务器端信息,并显示到控制台
             is = socket.getInputStream();
             baos = new ByteArrayOutputStream();
            byte[] buffer1 = new byte[20];
            int len1 ;
            while((len1 = is.read(buffer1))!=-1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.关闭流操作
            try {
                baos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            //1
            ss = new ServerSocket(9988);
            //2
            socket = ss.accept();
            //3
            is = socket.getInputStream();
            //4
            fos = new FileOutputStream(new File("longmao.jpg"));
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
            os = socket.getOutputStream();
            os.write("你好,图片已收到".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

UDP

package com.atguigu.java2;

import org.junit.Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/*
UDP的网络编程
 */
public class UDPTest {
    //发送端
    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String str= "UDP方式发送信息";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);

        socket.close();

    }

    //接受端
    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);

        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

 

这篇关于17.网络编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!