当前位置:   article > 正文

【Java学习】建立TCP和UDP连接,实现通信(27)_建立udp连接 java

建立udp连接 java

UDP连接

两端无需建立连接,不可靠连接,效率高,数据可能会丢失,数据大小有限制,一次发送64kb的数据包

以下是模拟客户端发消息,服务器端收消息
(其实服务器端是负责转发消息的,用户不与服务器通信)
[之后再写聊天室项目]

客户端向服务器端发一条消息
UDPCilent客户端

package com.westos.csdn;

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

public class UDPCilent {
    public static void main(String[] args) throws IOException {

        DatagramSocket ds = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要发送的字符串");
        String data = scanner.nextLine();
        byte[] str = data.getBytes();
        InetAddress inetAddress = InetAddress.getByName("192.168.6.116");
        DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888);
        ds.send(dp);

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

UDPServer服务器端

package com.westos.csdn;


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


public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(8888);
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        ds.receive(dp);
        byte[] data = dp.getData();;
        String string = new String(data, 0,dp.getLength());
        System.out.println(string);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

客户端向服务器端循环发消息
UDPCilent客户端代码

package com.jingfei.csdn1;

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

public class UDPCilent {
    public static void main(String[] args) throws IOException {

        DatagramSocket ds = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要发送的字符串");
        while (true){
            String data = scanner.nextLine();
            if (":EXIT".equals(data))break;
            byte[] str = data.getBytes();
            InetAddress inetAddress = InetAddress.getByName("192.168.6.116");
            DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888);
            ds.send(dp);
        }
        ds.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

UDPServer服务器端

package com.jingfei.csdn1;


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


public class UDPServer {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(8888);
        while (true){
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
            ds.receive(dp);
            byte[] data = dp.getData();
            String string = new String(data, 0,dp.getLength());
            //if (":EXIT".equals(string))break;
            System.out.println(string);
        }

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

客户端与服务器端通信相互发消息
用到线程,一边既开启服务器端接收消息,又当作客户端发送消息,端口和ip绑定相互对应,这边客户端对应那边服务器端,这边服务器端对应那边客户端,(我用的同一台电脑所以ip相同,你可以换成对方的ip)
将接受消息的代码放在子线程中,将输入消息的代码依旧在主线程中(好像输入消息也可以放在另一个子线程中,但有时好像不行)

UDPServer 服务器端

package com.jingfei.csdn2;


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


public class UDPServer {
    public static void main(String[] args) throws IOException {
        new Thread() {
            @Override
            public void run() {
                try {
                    DatagramSocket ds = new DatagramSocket(8887);
                    while (true) {
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                        ds.receive(dp);
                        byte[] data = dp.getData();
                        String string = new String(data, 0, dp.getLength());
                        //if (":EXIT".equals(string))break;
                        String ip = dp.getAddress().getHostAddress();
                        System.out.println(ip + "发来的信息为:" + string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        DatagramSocket ds = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要发送的字符串");
        while (true) {
            String data = scanner.nextLine();
            if (":EXIT".equals(data)) break;
            byte[] str = data.getBytes();
            InetAddress inetAddress = InetAddress.getByName("192.168.6.116");
            DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8888);
            ds.send(dp);
        }
        ds.close();


    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

UDPCilent客户端

package com.jingfei.csdn2;

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

public class UDPCilent {
    public static void main(String[] args) throws IOException {
        new Thread(){
            @Override
            public void run() {
                try {
                    DatagramSocket ds = new DatagramSocket(8888);
                    while (true){
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
                        ds.receive(dp);
                        byte[] data = dp.getData();
                        String string = new String(data, 0,dp.getLength());
                        //if (":EXIT".equals(string))break;
                        String ip = dp.getAddress().getHostAddress();
                        System.out.println(ip+"发来的信息为:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        DatagramSocket ds = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要发送的字符串");
        while (true){
            String data = scanner.nextLine();
            if (":EXIT".equals(data))break;
            byte[] str = data.getBytes();
            InetAddress inetAddress = InetAddress.getByName("192.168.6.116");
            DatagramPacket dp = new DatagramPacket(str, str.length, inetAddress, 8887);
            ds.send(dp);
        }
        ds.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

TCP连接

两端需要建立连接,可靠连接,相对效率低一点,数据大小无限制.

TCP连接循环向服务器发送数据
TCPCilent客户端

package com.jingfei.csdn;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TCPCilent {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.43.209", 8787);
        OutputStream out = socket.getOutputStream();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            if (":EXIT".equals(s)) {
                out.write(s.getBytes());
                break;
            }
            out.write(s.getBytes());
        }
        socket.close();


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

TCPSever服务器端

package com.jingfei.csdn;

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

public class TCPSever {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8787);
        Socket s = ss.accept();
        InputStream in = s.getInputStream();
        while (true) {
            byte[] bytes = new byte[1024];
            int len = in.read(bytes);
            String string = new String(bytes, 0, len);
            if (":EXIT".equals(string)) {
                break;
            }
            System.out.println(string);
        }
        ss.close();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

TCP连接客户端与服务器通信互发信息
用到线程,一边既开启服务器端接收消息,又当作客户端发送消息,端口和ip绑定相同,TCP连接既可以获取输入流,也可以获取输出流,将接受消息的代码放在子线程中,将输入消息的代码依旧在主线程中(好像输入消息也可以放在另一个子线程中,但有时好像不行)

TCPCilent客户端

package com.jingfei.csdn3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TCPCilent {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("192.168.43.209", 8787);
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        new Thread(){
            @Override
            public void run() {
                try {
                    while (true) {
                        byte[] bytes = new byte[1024];
                        int len = in.read(bytes);
                        String string = new String(bytes, 0, len);
                        if (":EXIT".equals(string)) {
                            break;
                        }
                        String ip = socket.getInetAddress().getHostAddress();
                        System.out.println(ip+"发来的消息为:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            if (":EXIT".equals(s)) {
                out.write(s.getBytes());
                break;
            }
            out.write(s.getBytes());
        }
        socket.shutdownOutput();
        socket.close();


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

TCPSever服务器端

package com.jingfei.csdn3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPSever {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8787);
        Socket s = ss.accept();
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        new Thread(){
            @Override
            public void run() {
                try {
                    while (true) {
                        byte[] bytes = new byte[1024];
                        int len = in.read(bytes);
                        String string = new String(bytes, 0, len);
                        if (":EXIT".equals(string)) {
                            break;
                        }
                        String ip = s.getInetAddress().getHostAddress();
                        System.out.println(ip+"发来的消息为:"+string);

                    }
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            if (":EXIT".equals(str)) {
                out.write(str.getBytes());
                break;
            }
            out.write(str.getBytes());
        }
        s.shutdownOutput();
        s.close();


    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

谢谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/146717?site
推荐阅读
相关标签
  

闽ICP备14008679号