「java数据包」java 数据

博主:adminadmin 2023-03-22 04:07:09 604

今天给各位分享java数据包的知识,其中也会对java 数据进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么自定义一个数据包并把它发送出去?

客户端代码

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.net.InetSocketAddress;

import java.net.Socket;

public class Client {

public static void main(String[] args) {

int length = 0;

byte[] sendBytes = null;

Socket socket = null;

DataOutputStream dos = null;

FileInputStream fis = null;

try {

try {

socket = new Socket();

socket.connect(new InetSocketAddress("192.168.0.104", 3000),

10 * 1000);

dos = new DataOutputStream(socket.getOutputStream());

File file = new File("Moon.zip");

fis = new FileInputStream(file);

sendBytes = new byte[10240];

while ((length = fis.read(sendBytes, 0, sendBytes.length)) 0) {

dos.write(sendBytes, 0, length);

dos.flush();

}

} finally {

if (dos != null)

dos.close();

if (fis != null)

fis.close();

if (socket != null)

socket.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

//////////////////////////////////////////////////////////

服务器代

import java.net.*;

import java.io.*;

public class Server implements Runnable {

public static void main(String[] args) {

try {

final ServerSocket server = new ServerSocket(3000);

Thread th = new Thread(new Runnable() {

public void run() {

while (true) {

try {

System.out.println("开始监听...");

Socket socket = server.accept();

System.out.println("有链接");

receiveFile(socket);

} catch (Exception e) {

}

}

}

});

th.run(); //启动线程运行

} catch (Exception e) {

e.printStackTrace();

}

}

public void run() {

}

public static void receiveFile(Socket socket) {

byte[] inputByte = null;

int length = 0;

DataInputStream dis = null;

FileOutputStream fos = null;

try {

try {

dis = new DataInputStream(socket.getInputStream());

fos = new FileOutputStream(new File("receive.MV"));

inputByte = new byte[1024];

System.out.println("开始接收数据...");

while ((length = dis.read(inputByte, 0, inputByte.length)) 0) {

System.out.println(length);

fos.write(inputByte, 0, length);

fos.flush();

}

System.out.println("完成接收");

} finally {

if (fos != null)

fos.close();

if (dis != null)

dis.close();

if (socket != null)

socket.close();

}

} catch (Exception e) {

}

}

}

如何用java写数据包?

你好,java中的socket编程,要把数据最后转成byte[]来进行通信,对于你这种情况,我认为你至少有两种方法可行

1、在java中也整一个类对象,等效于你这里的struct,然后使用java中把Object转换成byte[]的方法

2、把struct里的数据按照一定的格式存成字符串,然后再把字符串转成byte[]

方法一可以参考下这个代码:

public static byte[] convertToByteArray(Object obj) throws IOException{

ObjectOutputStream os = null;

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);

os = new ObjectOutputStream(new BufferedOutputStream(byteStream));

os.flush();

os.writeObject(obj);

os.flush();

byte[] sendBuf = byteStream.toByteArray();

os.close();

return sendBuf;

}

Java怎么导出dpb数据包

在使用的数据包的功能中,进行点击wireshark的菜单中的“file”的菜单选项。

这样就会弹出了下拉菜单中进行选择为“export packet dissections”的选项。

选择完了export packet dissections之后,来进行选择为as “csv” (comma separated values packes)

这样就会弹出了一个保存到电脑中的路径的,进行再输入框中的文件名csv

这样在导出到电脑中的界面中的,为csv的文件中,

在这个的打开了csv的文件中之后,导出的数据保存到了csv中。

java+socket能抓到包但是无法到达服务器?

如果你使用 Java Socket 抓到了数据包,但是数据包并没有到达服务器,那么这可能是由于以下一些原因导致的:

防火墙配置:

防火墙可能会阻止某些流量通过,你需要检查服务器的防火墙配置,确保它允许该端口的流量通过。

目标端口问题:

你需要确保你连接的端口是正确的,服务器上有一个服务在该端口上运行,并且该服务正在监听该端口。

代码错误:

代码错误可能导致数据包无法到达服务器。你需要检查你的代码,确保你正确地构造了数据包并正确地发送了它们。

路由问题:

在某些情况下,路由问题可能会导致数据包无法到达服务器。你可以尝试使用 traceroute 命令来检查路由。

以上是一些可能导致数据包无法到达服务器的原因,你需要逐一排查并解决。

java显示原始数据包内容

目测是二进制数据

public byte[] readToByte(InputStream in) throws IOException {

ByteArrayOutputStream out = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int len;

while ((len = in.read(b)) != -1) {

out.write(b, 0, len);

}

out.close();

return out.toByteArray();

}

public int bytes2Int(byte[] b, int point, int len) {

int result = 0;

for (int i = 0; i len; i++)

result += (long) (((int) (b[point + i]) 0xff) (i * 8));

return result;

}

public String bytes2Value(byte[] b, int point, int len) throws UnsupportedEncodingException {

return new String(b, point, len, "GBK");

}

java中Socket如何实现数据包传输的打包和解包?

socket一般调用accept方法等待连接:

public class ServerThread extends Thread{

ServicePanel servicePanel = null;

ServerSocket serverSocket = null;

Socket socket = null;

public ServerThread(ServicePanel servicePanel,ServerSocket serverSocket) {

this.servicePanel = servicePanel;

this.serverSocket = serverSocket;

}

public void run(){

while (true) {

try {

socket = serverSocket.accept();//等待连接

new ReciveFromClient(servicePanel,socket).start();

} catch (IOException e) {

// e.printStackTrace();

break;

}

}

}

}

至于收包,发包是以流的形式传送的:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

一般包里面的内容也就是被我们封装在某个类中,这个类要实现序列化,即实现Serializable接口;

发送方法那些就要自己写了~

关于java数据包和java 数据的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。