2016-04-13 37 views
0

UDP Datagram Protokolü'nü kullanarak bir FTP uygulaması yazarım ve 100 karakterden okunan istemci tarafına ihtiyacım var ve bu parçayı programım çalıştırdığımda 5 parça, her parçada 20 karakter gönderdim : Exception in thread "main" java.lang.IllegalArgumentException: illegal length or offset. Sunucunun her satırı beş parça halinde almasını istiyorum, ancak buna göre sırala. sorunlu bölgelerin yorumlarlaDatagram Paket FTP uygulaması

import java.net.*; 
import java.io.*; 

public class FTPClient 
{ 
    public static void main(String[] args) 
    { 
     final int SIZE=100; 
     DatagramSocket skt= null; 
     DatagramPacket pkt = null; 
     BufferedReader read= null; 
     int port = 3131; 
     try 
     { 
      skt=new DatagramSocket(2121); 
      read= new BufferedReader(new FileReader("input.txt")); 
      String line = read.readLine(); 
      byte[] lineByte = new byte[SIZE]; 
      lineByte = line.getBytes(); 
      InetAddress add = InetAddress.getByName("localhost"); 

      for(int i=0;i<100;i+=20) 
      { 
        pkt = new DatagramPacket(lineByte,i,20,add,port); 
        skt.send(pkt); 
      } 

     } 

     catch(IOException e) 
     { 
      System.out.println(e.getMessage()); } 

     finally 
     { 
      skt.close(); 
      // read.close(); 
     } 
    } 
} 

cevap

0

Kodunuz: bu benim kodudur. Hala benim yorumları okuduktan sonra sorunların çözümüne sorunları varsa, sadece bir yorum eklemek ve ben

public static void main(String[] args) { 
    final int SIZE = 100; 
    DatagramSocket skt = null; 
    DatagramPacket pkt = null; 
    BufferedReader read = null; 
    int port = 3131; 
    try { 
     skt = new DatagramSocket(2121); 
     read = new BufferedReader(new FileReader("input.txt")); 
     String line = read.readLine(); // how long is the line? 
     byte[] lineByte = new byte[SIZE]; // this is a redundant assignment 
     lineByte = line.getBytes(); // now the length of the lineBytes is "unknown" 
     InetAddress add = InetAddress.getByName("localhost"); 

     for (int i = 0; i < 100; i += 20) { // you should check the length of lineBytes instead of 100 
      pkt = new DatagramPacket(lineByte, i, 20, add, port); 
      skt.send(pkt); 
     } 

    } 
    catch (IOException e) { 
     System.out.println(e.getMessage()); 
    } 
    finally { 
     skt.close(); 
     // read.close(); 
    } 
} 
+0

Benim için uzunluğunu kontrol için önemsiz, ben farz daha açıklayacağız Bunu kabul edemeyiz –

+0

100 karakterdir 100. 'read.readLine()' sadece bir satır okuyor, yani ilk satır karakterine kadar her şeyi okuyor. – gustf