【发布时间】:2015-03-10 05:00:09
【问题描述】:
我正在尝试通过 UDP 将文件从 java 客户端传输到 c# 服务器。但是,当我尝试在服务器端打开传输的文件(png 图片)时,它没有成功打开它。有人可以帮助我吗?
客户端代码:(java)
import java.io.File;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Program {
public static void main(String [] args)
{
DatagramSocket clientSocket;
try {
clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("192.168.1.15");
File myFile = new File ("D:/Users/user-pc/Downloads/phone.png");
byte [] data = new byte[(int)myFile.length()];
DatagramPacket sendPacket = new DatagramPacket(data, data.length, IPAddress, 3109);
clientSocket.send(sendPacket);
clientSocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
服务器代码:(c#)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
byte[] data;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3109);
UdpClient newsock = new UdpClient(ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
data = newsock.Receive(ref sender);
File.WriteAllBytes(@"d:\pics\pic.png", data);
}
}
}
【问题讨论】: