java 写了个程序用于一台机器向另一台机器传输文件,客户端一直接收不到服务器的包,求帮忙找错,在线等 10
服务器程序:publicclassthi4server{publicstaticvoidmain(String[]args)throwsIOException{Datag...
服务器程序:
public class thi4server {
public static void main(String[] args) throws IOException {
DatagramSocket filereceive = new DatagramSocket(1234);
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
System.out.println("****服务器端已经启动,等待客户端发送数据******");
filereceive.receive(packet);// 此方法在接收到数据报之前会一直阻塞
System.out.println("客户端存在,开始向客户端传输文件。");
//System.out.println("我是服务器,客户端说:"+(new String(data, 0, data.length)) + packet.getAddress()+" "+packet.getPort()+" "+packet.getSocketAddress());
/* * 向客户端响应数据 */
String filename = "e:\\ForTransformTest.txt";
File file = new File(filename);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
// 发送文件名和路径
DatagramSocket filesend = new DatagramSocket();
//System.out.println(new String(filename.getBytes(), 0, filename.getBytes().length));
DatagramPacket packet2 = new DatagramPacket(filename.getBytes(), filename.getBytes().length, packet.getAddress(),1234);
filesend.send(packet2);
// 2.从缓冲流中按字节读取文件,打包发送
byte[] data2 = new byte[1024];
while (br.read(data2) != -1) {
//System.out.println(new String(data2,0,data2.length));
DatagramPacket packet3 = new DatagramPacket(data2, data2.length, packet.getAddress(),1234);
filesend.send(packet3);
}
System.out.println("Done!");
filesend.close();
}
}
客户端程序:
public class thi4client {
public static void main(String[] args) throws IOException {
try {
InetAddress address = InetAddress.getByName("192.168.1.101");
int port = 1234;
byte[] data = "hello".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
DatagramSocket filesend = new DatagramSocket();
System.out.println("before sending");
filesend.send(packet);
System.out.println("after sending" + " " + packet.getSocketAddress());
DatagramSocket filerecieve = new DatagramSocket(port);
byte[] data2 = new byte[1024];
DatagramPacket packetfilename = new DatagramPacket(data2, data2.length);
System.out.println("before receiving");
filerecieve.receive(packetfilename);//卡在这儿!!!
System.out.println("after receiving");
String filename = new String(data2, 0, packetfilename.getLength());
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filename));
byte[] data3 = new byte[4096];
DatagramPacket packetfilecontain = new DatagramPacket(data3, data3.length);
{
filerecieve.receive(packetfilecontain);
String filecontain = new String(data3, 0, packetfilecontain.getLength());
osw.write(filecontain);
}
osw.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
问题已解决,谢谢大家,顺便告诉大家问题所在和解决方法
问题:服务器可以收到客户端发来的数据包,但是服务器依据收到的包再对客户端发送时,发送成功,客户端却没收到。
原因:用于测试的客户端电脑开了虚拟机,虚拟机也有网卡,如此一来,发送的包,端口就不是原来包的地址和端口了
解决方法:关闭所有的虚拟机并禁用其网卡,关闭所有的wifi精灵的软件,确保工作的只有计算机的本身的一个网卡。并且注意客户端和服务器端在同一局域网内,准确记录ip 展开
public class thi4server {
public static void main(String[] args) throws IOException {
DatagramSocket filereceive = new DatagramSocket(1234);
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
System.out.println("****服务器端已经启动,等待客户端发送数据******");
filereceive.receive(packet);// 此方法在接收到数据报之前会一直阻塞
System.out.println("客户端存在,开始向客户端传输文件。");
//System.out.println("我是服务器,客户端说:"+(new String(data, 0, data.length)) + packet.getAddress()+" "+packet.getPort()+" "+packet.getSocketAddress());
/* * 向客户端响应数据 */
String filename = "e:\\ForTransformTest.txt";
File file = new File(filename);
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
// 发送文件名和路径
DatagramSocket filesend = new DatagramSocket();
//System.out.println(new String(filename.getBytes(), 0, filename.getBytes().length));
DatagramPacket packet2 = new DatagramPacket(filename.getBytes(), filename.getBytes().length, packet.getAddress(),1234);
filesend.send(packet2);
// 2.从缓冲流中按字节读取文件,打包发送
byte[] data2 = new byte[1024];
while (br.read(data2) != -1) {
//System.out.println(new String(data2,0,data2.length));
DatagramPacket packet3 = new DatagramPacket(data2, data2.length, packet.getAddress(),1234);
filesend.send(packet3);
}
System.out.println("Done!");
filesend.close();
}
}
客户端程序:
public class thi4client {
public static void main(String[] args) throws IOException {
try {
InetAddress address = InetAddress.getByName("192.168.1.101");
int port = 1234;
byte[] data = "hello".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
DatagramSocket filesend = new DatagramSocket();
System.out.println("before sending");
filesend.send(packet);
System.out.println("after sending" + " " + packet.getSocketAddress());
DatagramSocket filerecieve = new DatagramSocket(port);
byte[] data2 = new byte[1024];
DatagramPacket packetfilename = new DatagramPacket(data2, data2.length);
System.out.println("before receiving");
filerecieve.receive(packetfilename);//卡在这儿!!!
System.out.println("after receiving");
String filename = new String(data2, 0, packetfilename.getLength());
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filename));
byte[] data3 = new byte[4096];
DatagramPacket packetfilecontain = new DatagramPacket(data3, data3.length);
{
filerecieve.receive(packetfilecontain);
String filecontain = new String(data3, 0, packetfilecontain.getLength());
osw.write(filecontain);
}
osw.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
问题已解决,谢谢大家,顺便告诉大家问题所在和解决方法
问题:服务器可以收到客户端发来的数据包,但是服务器依据收到的包再对客户端发送时,发送成功,客户端却没收到。
原因:用于测试的客户端电脑开了虚拟机,虚拟机也有网卡,如此一来,发送的包,端口就不是原来包的地址和端口了
解决方法:关闭所有的虚拟机并禁用其网卡,关闭所有的wifi精灵的软件,确保工作的只有计算机的本身的一个网卡。并且注意客户端和服务器端在同一局域网内,准确记录ip 展开
1个回答
2016-06-19
展开全部
假设你有A、B两台计算机,那么A可以向B发送文件,B也可以向A发送文件。所以在实现的时候,你的软件既要实现服务器的功能,也要实现客户端的功能。即:你的软件既可以申请发送文件,也可以在别人向你发送文件时接收文件。实现也不难,只是你判断用户需要的功能,然后根据功能实现相应的操作就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询