简单的网络编程

TCP

package IntenetAddress;

import org.junit.Test;

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


public class TCPtest {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream ops = null;
        try {
            //创建一个Socker对象 指明 服务器端 的IP和端口号
            InetAddress address = InetAddress.getByName("172.21.199.40");
            socket = new Socket(address,8899);
            ops = socket.getOutputStream();
            ops.write("你好".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ops!=null)
                ops.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void server()  {
        ServerSocket ss = null;
        Socket accept = null;
        InputStream ips = null;
        ByteArrayOutputStream baos = null;
        try {
            //创建服务器端的socker 指明自己的端口号
            ss = new ServerSocket(8899);
            accept = ss.accept();
            ips = accept.getInputStream();
            baos = new ByteArrayOutputStream();

            int len;
            byte[] buf = new byte[1024];
            while ((len=ips.read(buf))!=-1){
                baos.write(buf,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos!=null)
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ips!=null)
                ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept!=null)
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss!=null)
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
package IntenetAddress;

import org.junit.Test;

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

/**
 * 客户端发送文件给服务端 服务端将文件保存在本地
 */
public class TCPTest02 {
    @Test
    public void client(){
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream fw = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
            fis = new FileInputStream(new File("src\\2.jpg"));
            fw = socket.getOutputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = fis.read(buf))!=-1){
                fw.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis!=null)
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void server(){
        ServerSocket ss = null;
        OutputStream fw = null;
        Socket socket = null;
        InputStream ips = null;
        try {
            ss = new ServerSocket(8899);
            fw = new FileOutputStream(new File("src\\2tcp.jpg"));
            socket = ss.accept();
            ips = socket.getInputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = ips.read(buf))!=-1){
                fw.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ips!=null)
                ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss!=null)
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Test
    public void client2(){
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream fw = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
            fis = new FileInputStream(new File("src\\hello.txt"));
            fw = socket.getOutputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = fis.read(buf))!=-1){
                fw.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis!=null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void server2(){
        ServerSocket ss = null;
        FileWriter fw = null;
        Socket socket = null;
        InputStream ips = null;
        ByteArrayOutputStream baos =null;
        try {
            ss = new ServerSocket(8899);
            fw = new FileWriter(new File("src\\hello_tcp.txt"));
            baos = new ByteArrayOutputStream();
            socket = ss.accept();
            ips = socket.getInputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = ips.read(buf))!=-1){
                baos.write(buf,0,len);
                fw.write(baos.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ips!=null)
                    ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss!=null)
                    ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (baos!=null)
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

package IntenetAddress;

import org.junit.Test;

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

public class TCPTest03 {
    @Test
    public void client(){
        Socket socket = null;
        FileInputStream fis = null;
        OutputStream fw = null;

        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
            fis = new FileInputStream(new File("src\\2.jpg"));
            fw = socket.getOutputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = fis.read(buf))!=-1){
                fw.write(buf,0,len);
            }
            /**
             *告诉output数据传完了,结束了
             */
            socket.shutdownOutput();
            //接受服务端消息
            InputStream ips = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len2;
            byte[] buf2 = new byte[20];
            while ((len2 = ips.read(buf2))!=-1){
                baos.write(buf2,0,len2);
            }
            ips.close();
            baos.close();
            System.out.println(baos.toString());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis!=null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @Test
    public void server(){
        ServerSocket ss = null;
        OutputStream fw = null;
        Socket socket = null;
        InputStream ips = null;
        try {
            ss = new ServerSocket(8899);
            fw = new FileOutputStream(new File("src\\2tcp.jpg"));
            socket = ss.accept();
            ips = socket.getInputStream();
            int len;
            byte[] buf = new byte[1024];
            while ((len = ips.read(buf))!=-1){
                fw.write(buf,0,len);
            }

            //接受完数据 服务端给客户端反馈
            OutputStream ops = socket.getOutputStream();
            ops.write("接收成功".getBytes());
            ops.close();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ips!=null)
                    ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ss!=null)
                    ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

UDP

package IntenetAddress;

import org.junit.Test;

import java.io.IOException;
import java.net.*;


/**
 * UDP的协议网络编程
 */
public class UDPTest {
    //发送端
    @Test
    public void sender() throws IOException {

        DatagramSocket socket = new DatagramSocket();
        String str ="我是李建昊";
        byte[] data = str.getBytes();
        //对方主机名
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address);
        DatagramPacket packet = new DatagramPacket(data, 0, data.length, address,8899);
        socket.send(packet);

        socket.close();

    }
    //接收端
    @Test
    public void receiver() throws IOException {

        DatagramSocket socket = new DatagramSocket(8899);
        byte[] buf = new byte[100];
        DatagramPacket packet = new DatagramPacket(buf,0,buf.length );
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

URL

package IntenetAddress;

import org.junit.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * http://localhost:8080/examples/beauty.jpg?username=Tom
 *  协议    主机名    端口号       资源地址         参数列表
 */
public class URLTest {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
        System.out.println(url.getProtocol());//获取该URL的协议名
        System.out.println(url.getHost());//获取url的主机名
        System.out.println(url.getPort());//获取该url端口号
        System.out.println(url.getPath());//获取该url的文件路径
        System.out.println(url.getFile());//获取该url的文件名
        System.out.println(url.getQuery());//获取该url的查询名
        /**
         * http
         * localhost
         * 8080
         * /examples/beauty.jpg
         * /examples/beauty.jpg?username=Tom
         * username=Tom
         */
        URLConnection urlConnection =  url.openConnection();
        urlConnection.connect();

        InputStream is = urlConnection.getInputStream();
        FileOutputStream ops = new FileOutputStream("");
        BufferedOutputStream bos = new BufferedOutputStream(ops);
        BufferedInputStream bis = new BufferedInputStream(is);
        int len;
        byte[] buf = new byte[20];
        while ((len = bis.read(buf))!=-1){
            bos.write(buf,0,len);
        }
        System.out.println("下载完成");

        bos.close();
        bis.close();
        //try-catch-finally


    }
}

上一篇:SpringSecurity的简单使用(1)


下一篇:客户端+服务端1(提升)