java怎么自定义一个数据包并把它发送出去?
端口9600发到主机地址192.168.0.110(随便一台主机都行,主要是定义这个包部分,每个部分可以用0暂时填充一下。)包格式如图(急!!向各位大神神求助!)最好有详...
端口9600发到主机地址192.168.0.110(随便一台主机都行,主要是定义这个包部分,每个部分可以用0暂时填充一下。)包格式如图(急!! 向各位大神神求助!)
最好有详细的代码实现,把这个自定义包的每一部分按顺序封装起来(不知道的部分例如DIP留出空间填0就可以了),用socket根据目的主机地址端口号发出去就可以了
发出去 展开
最好有详细的代码实现,把这个自定义包的每一部分按顺序封装起来(不知道的部分例如DIP留出空间填0就可以了),用socket根据目的主机地址端口号发出去就可以了
发出去 展开
2个回答
展开全部
客户端代码
/*
* 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) {
}
}
}
码
/*
* 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) {
}
}
}
码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询