在 UDP 中接收数据包- Java
Receiving packets in UDP- Java
您好,我正在尝试使用 UDP 从客户端向服务器发送 1 kb 的数据包。我可以收到所有数据包,但问题是循环没有退出,所以我使用套接字超时来实现 this.Since 这不适合动态环境,我想更换一些合理的东西。下面是我的服务器端和客户端代码,客户端跟随服务器端,而 loop.I 只想实现 RDT 1.0,其中数据包被分成 1kb 并通过 UDP 套接字发送到另一端接收。但是在接收所有 1kb 数据包时,我尝试了很多事情来退出 while 循环并最终在接收端设置套接字超时。我还声明 'i'(我知道我的文件大小 <9)退出循环,以便在 timeout.Is 之前执行其余的编码 有一种方法我可以改变我的 while 循环,这样它适合所有环境(发送N个数据包)。
//Client side
class Client
{
public static DatagramSocket sock =null;
public static FileInputStream in=null;
public static DatagramPacket[] sendpacket=new DatagramPacket[1024];
public static void main(String[] args) throws Exception
{
byte[] array = new byte[1024];
DatagramSocket sock = new DatagramSocket();
InetAddress ip= InetAddress.getByName("localhost");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a file name: ");
System.out.flush();
String filename = scanner.nextLine();
File file = new File(filename);
in=new FileInputStream(file);
int length =0,checksum=0,h=1;
while((length = in.read(array))!=-1)
{
sendpacket[h]=new DatagramPacket(array,array.length,ip,1234);
sock.send(sendpacket[h]);
checksum+=length;
System.out.println("Sent packet "+h);
System.out.println(length);
System.out.println(checksum);
h++;
}
in.close();
}
}
//Server side
public class server
{
public static DatagramPacket[] recvpacket=new DatagramPacket[100];
public static DatagramSocket sock=null;
public static FileOutputStream fos=null;
public static BufferedOutputStream bos=null;
public static void main(String args[])
{
try
{
try
{
sock=new DatagramSocket(1234);//opening a socket
}
catch(IOException ex)
{
System.out.println(ex);
}
sock.setSoTimeout(30000);
boolean socketalive=true;
byte[] array=new byte[1024];
int i=1,checksum=0;
System.out.println("server is ready to receive packets");
while(recvpacket!=null)//I need change in this loop
{
recvpacket[i]=new DatagramPacket(array,array.length);
sock.receive(recvpacket[i]);
System.out.println("server received packet "+i);
int length=array.length;
checksum+=length;
System.out.println(length);//here i get the size of the buffer so //checksum is wrong
System.out.println(checksum);
fos=new FileOutputStream(new File("profile1.docx"),true);
fos.write(array);
for(int j=0; j<1024; j++)
{
array[j]=0;
}
i++;
}//while
System.out.println("server received packet "+i);
}//try
catch(IOException e)
{
System.out.println(e);
}
}//main
}//class
您可能 运行 遇到一些问题。 UDP 不像 TCP 那样可靠,因此数据包可能会被丢弃,或者更糟的是可能会乱序。在将数据写入文件之前,您需要将数据存储在本地缓冲区中,或者将其写入文件中的适当位置(可能会留下一个遗漏的空间)。
理想情况下,UDP 监听循环应该在它自己的线程中,这样其他代码就可以 运行。如果你想通过 UDP 实现某种 FTP,那么你需要在开始时发送一个数据包,其中包含有关传输的信息,例如大小、数据包数量、校验和。然后对于每个数据包,您还需要为单个数据包包含一个序列号和一个校验和。在最后(或沿途),服务器需要回复客户端请求 missing/corrupt 数据包。我建议在最后发送一个 "transfer complete" 数据包。
如果所有数据包都已收到且没有损坏,那么您可以终止循环!
你应该能够找到现有的东西来为你处理所有这些,而不是从头开始写,但如果你必须自己写,那么你必须复制 TCP 的开销。
您好,我正在尝试使用 UDP 从客户端向服务器发送 1 kb 的数据包。我可以收到所有数据包,但问题是循环没有退出,所以我使用套接字超时来实现 this.Since 这不适合动态环境,我想更换一些合理的东西。下面是我的服务器端和客户端代码,客户端跟随服务器端,而 loop.I 只想实现 RDT 1.0,其中数据包被分成 1kb 并通过 UDP 套接字发送到另一端接收。但是在接收所有 1kb 数据包时,我尝试了很多事情来退出 while 循环并最终在接收端设置套接字超时。我还声明 'i'(我知道我的文件大小 <9)退出循环,以便在 timeout.Is 之前执行其余的编码 有一种方法我可以改变我的 while 循环,这样它适合所有环境(发送N个数据包)。
//Client side
class Client
{
public static DatagramSocket sock =null;
public static FileInputStream in=null;
public static DatagramPacket[] sendpacket=new DatagramPacket[1024];
public static void main(String[] args) throws Exception
{
byte[] array = new byte[1024];
DatagramSocket sock = new DatagramSocket();
InetAddress ip= InetAddress.getByName("localhost");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a file name: ");
System.out.flush();
String filename = scanner.nextLine();
File file = new File(filename);
in=new FileInputStream(file);
int length =0,checksum=0,h=1;
while((length = in.read(array))!=-1)
{
sendpacket[h]=new DatagramPacket(array,array.length,ip,1234);
sock.send(sendpacket[h]);
checksum+=length;
System.out.println("Sent packet "+h);
System.out.println(length);
System.out.println(checksum);
h++;
}
in.close();
}
}
//Server side
public class server
{
public static DatagramPacket[] recvpacket=new DatagramPacket[100];
public static DatagramSocket sock=null;
public static FileOutputStream fos=null;
public static BufferedOutputStream bos=null;
public static void main(String args[])
{
try
{
try
{
sock=new DatagramSocket(1234);//opening a socket
}
catch(IOException ex)
{
System.out.println(ex);
}
sock.setSoTimeout(30000);
boolean socketalive=true;
byte[] array=new byte[1024];
int i=1,checksum=0;
System.out.println("server is ready to receive packets");
while(recvpacket!=null)//I need change in this loop
{
recvpacket[i]=new DatagramPacket(array,array.length);
sock.receive(recvpacket[i]);
System.out.println("server received packet "+i);
int length=array.length;
checksum+=length;
System.out.println(length);//here i get the size of the buffer so //checksum is wrong
System.out.println(checksum);
fos=new FileOutputStream(new File("profile1.docx"),true);
fos.write(array);
for(int j=0; j<1024; j++)
{
array[j]=0;
}
i++;
}//while
System.out.println("server received packet "+i);
}//try
catch(IOException e)
{
System.out.println(e);
}
}//main
}//class
您可能 运行 遇到一些问题。 UDP 不像 TCP 那样可靠,因此数据包可能会被丢弃,或者更糟的是可能会乱序。在将数据写入文件之前,您需要将数据存储在本地缓冲区中,或者将其写入文件中的适当位置(可能会留下一个遗漏的空间)。
理想情况下,UDP 监听循环应该在它自己的线程中,这样其他代码就可以 运行。如果你想通过 UDP 实现某种 FTP,那么你需要在开始时发送一个数据包,其中包含有关传输的信息,例如大小、数据包数量、校验和。然后对于每个数据包,您还需要为单个数据包包含一个序列号和一个校验和。在最后(或沿途),服务器需要回复客户端请求 missing/corrupt 数据包。我建议在最后发送一个 "transfer complete" 数据包。
如果所有数据包都已收到且没有损坏,那么您可以终止循环!
你应该能够找到现有的东西来为你处理所有这些,而不是从头开始写,但如果你必须自己写,那么你必须复制 TCP 的开销。