当前位置:   article > 正文

udp客户端 Java_java使用UDP来进行客户端和服务器端通信的简单例子 | 学步园

java upd客户获取服务端数据

1:客户端程序

package com.pb.udp;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.net.DatagramSocket;

/**

* 使用Sender类来代表客户端程序,

* @author ccna_zhang

*

*/

public class Sender {

public static void main(String[] args) {

String msg = "Hello, World";

byte[] buf = msg.getBytes();

try {

InetAddress address = InetAddress.getByName("192.168.1.80"); //服务器地址

int port = 8080; //服务器的端口号

//创建发送方的数据报信息

DatagramPacket dataGramPacket = new DatagramPacket(buf, buf.length, address, port);

DatagramSocket socket = new DatagramSocket(); //创建套接字

socket.send(dataGramPacket); //通过套接字发送数据

//接收服务器反馈数据

byte[] backbuf = new byte[1024];

DatagramPacket backPacket = new DatagramPacket(backbuf, backbuf.length);

socket.receive(backPacket); //接收返回数据

String backMsg = new String(backbuf, 0, backPacket.getLength());

System.out.println("服务器返回的数据为:" + backMsg);

socket.close();

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2:服务器端程序

package com.pb.udp;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketAddress;

import java.net.UnknownHostException;

/**

* 服务器端程序

* @author ccna_zhang

*

*/

public class Receiver {

public static void main(String[] args) {

try {

InetAddress address = InetAddress.getLocalHost();

int port = 8080;

//创建DatagramSocket对象

DatagramSocket socket = new DatagramSocket(port, address);

byte[] buf = new byte[1024]; //定义byte数组

DatagramPacket packet = new DatagramPacket(buf, buf.length); //创建DatagramPacket对象

socket.receive(packet); //通过套接字接收数据

String getMsg = new String(buf, 0, packet.getLength());

System.out.println("客户端发送的数据为:" + getMsg);

//从服务器返回给客户端数据

InetAddress clientAddress = packet.getAddress(); //获得客户端的IP地址

int clientPort = packet.getPort(); //获得客户端的端口号

SocketAddress sendAddress = packet.getSocketAddress();

String feedback = "Received";

byte[] backbuf = feedback.getBytes();

DatagramPacket sendPacket = new DatagramPacket(backbuf, backbuf.length, sendAddress); //封装返回给客户端的数据

socket.send(sendPacket); //通过套接字反馈服务器数据

socket.close(); //关闭套接字

} catch (UnknownHostException e) {

e.printStackTrace();

} catch(IOException e) {

e.printStackTrace();

}

}

}

3:程序运行结果

服务器端:

客户端发送的数据为:Hello, World

客户端:

服务器返回的数据为:Received

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

闽ICP备14008679号