Java网络编程

时间:2021-04-16 12:09:10   收藏:0   阅读:0

简介

通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样。在计算机网络中,这些连接和通信的规则被称为网络通信协议,它对数据的传输格式、传输速率、传输步骤等做了统一规定,通信双方必须同时遵守才能完成数据交换。

网络模型

技术图片


网络编程三要素


TCP编程

package com.smile.test.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class GreetingServer extends Thread{
    private ServerSocket serverSocket;
    public GreetingServer(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(100000);
    }
    public void run(){
        while(true){
            try{
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                DataInputStream dataInputStream = new DataInputStream(inputStream);
                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataOutputStream.writeUTF("thank you for you connection" + socket.getLocalSocketAddress());
                socket.close();
            }catch (SocketTimeoutException e) {
                System.out.println("socket timeout");
                break;
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        int port = 6066;
        try{
            Thread t = new GreetingServer(port);
            t.run();
        }catch (IOException e) {
            e.printStackTrace();
        }
        }
}

package com.smile.test.socket;

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

public class SocketTest {

    public static void main(String[] args){
        String serverName = "localhost";
        int port = 6066;
        try {
            System.out.println("连接到" + serverName + "端口号" + port);
            Socket client = new Socket(serverName, port);
            System.out.println("远程主机地址:" + client.getRemoteSocketAddress());
            OutputStream outputStream = client.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeUTF("hello from" + client.getLocalSocketAddress());
            InputStream inputStream = client.getInputStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            System.out.println("服务器响应" + dataInputStream.readUTF());
            client.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}

UDP编程

package com.smile.test.socket;

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

public class UDPClient {
    public static void main(String[] args) throws IOException {
        InetAddress localHost = InetAddress.getByName("localhost");
        DatagramSocket datagramSocket = new DatagramSocket();
        String msg = "hello";
        DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),msg.length(),localHost,9000);
        datagramSocket.send(datagramPacket);
        datagramSocket.close();
    }
}
package com.smile.test.socket;

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

public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9000);
        byte[] buff = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buff, buff.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0, packet.getLength()));
        socket.close();
    }
}

参考博客:
https://www.cnblogs.com/bj-mr-li/p/11106390.html
https://blog.csdn.net/qq_41923622/article/details/85805003

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!