java的UDP通信中,怎么从服务器端获取文本文件内容并显示在组件上

java的UDP通信中,怎么从服务器端获取文本文件内容并显示在组件上,第1张

1:客户端程序

[java] view plain copy

package compbudp;

import javaioIOException;

import javanetDatagramPacket;

import javanetInetAddress;

import javanetUnknownHostException;

import javanetDatagramSocket;

/

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

@author ccna_zhang

/

public class Sender {

public static void main(String[] args) {

String msg = "Hello, World";

byte[] buf = msggetBytes();

try {

InetAddress address = InetAddressgetByName("192168180"); //服务器地址

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

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

DatagramPacket dataGramPacket = new DatagramPacket(buf, buflength, address, port);

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

socketsend(dataGramPacket); //通过套接字发送数据

//接收服务器反馈数据

byte[] backbuf = new byte[1024];

DatagramPacket backPacket = new DatagramPacket(backbuf, backbuflength);

socketreceive(backPacket); //接收返回数据

String backMsg = new String(backbuf, 0, backPacketgetLength());

Systemoutprintln("服务器返回的数据为:" + backMsg);

socketclose();

} catch (UnknownHostException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

}

2:服务器端程序

[java] view plain copy

package compbudp;

import javaioIOException;

import javanetDatagramPacket;

import javanetDatagramSocket;

import javanetInetAddress;

import javanetSocketAddress;

import javanetUnknownHostException;

/

服务器端程序

@author ccna_zhang

/

public class Receiver {

public static void main(String[] args) {

try {

InetAddress address = InetAddressgetLocalHost();

int port = 8080;

//创建DatagramSocket对象

DatagramSocket socket = new DatagramSocket(port, address);

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

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

socketreceive(packet); //通过套接字接收数据

String getMsg = new String(buf, 0, packetgetLength());

Systemoutprintln("客户端发送的数据为:" + getMsg);

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

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

int clientPort = packetgetPort(); //获得客户端的端口号

SocketAddress sendAddress = packetgetSocketAddress();

String feedback = "Received";

byte[] backbuf = feedbackgetBytes();

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

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

socketclose(); //关闭套接字

} catch (UnknownHostException e) {

eprintStackTrace();

} catch(IOException e) {

eprintStackTrace();

}

}

}

3:程序运行结果

服务器端:

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

客户端:

服务器返回的数据为:Received

思路:把服务器上的文件下载到本地电脑,然后打开。

URL url = new URL("http://");

BuffereInputStream bis = new BufferedInputStream(urlopenConnection()getOutputStream());

先把文件读取到本地,然后从本地打开。

/

读取某个文件夹下的所有文件

/

public static boolean readfile(String filepath) throws FileNotFoundException, IOException {

try {

File file = new File(filepath);

if (!fileisDirectory()) {

Systemoutprintln("文件");

Systemoutprintln("path=" + filegetPath());

Systemoutprintln("absolutepath=" + filegetAbsolutePath());

Systemoutprintln("name=" + filegetName());

} else if (fileisDirectory()) {

Systemoutprintln("文件夹");

String[] filelist = filelist();

for (int i = 0; i < filelistlength; i++) {

File readfile = new File(filepath + "\\" + filelist[i]);

if (!readfileisDirectory()) {

Systemoutprintln("path=" + readfilegetPath());

Systemoutprintln("absolutepath="

+ readfilegetAbsolutePath());

Systemoutprintln("name=" + readfilegetName());

} else if (readfileisDirectory()) {

readfile(filepath + "\\" + filelist[i]);

}

}

}

} catch (FileNotFoundException e) {

Systemoutprintln("readfile() Exception:" + egetMessage());

}

return true;

}

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » java的UDP通信中,怎么从服务器端获取文本文件内容并显示在组件上

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情