线程代码意外停止

时间:2012-07-31 14:04:00

标签: java multithreading sockets

此代码不起作用你可以告诉我这里有什么问题吗?它已经开始,但是在线程第一个for循环之前进程停止了。

我正在尝试创建一个在wifi或任何无线数据传输中使用的代码..因为数据在无线数据通信中松动。

服务器代码:

public class Server {

  public static void main(String[] args) throws Exception{
      final fbufr a1 = null,a2 = null,a3 = null,a4 = null,a5 = null,a6 = null,a7 = null,a8 = null;
       final byte[] dd = new byte[1024];

    ServerSocket servsock = new ServerSocket(5001);

    int pos=0;

    final File myFile = new File("Music.mp4");
    final int packetsize=1024;
    final double nosofpackets=Math.ceil(((int) myFile.length())/packetsize);
    System.out.println(nosofpackets);
    final Socket sock = servsock.accept();
    System.out.println("server up!!");
    final OutputStream os = sock.getOutputStream();

    Thread t1 = new Thread(){
        public void run(){
            try{
                System.out.println("thread1 up!!!");
                    int isbyte;
                    int checksum=0;
                    double i=0;
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
                    System.out.println("near loop 1!!!");
                    for(i=0;i<nosofpackets+1;i=i+8)
                    {
                        a1.flag=0;
                        a2.flag=0;
                        a3.flag=0;
                        a4.flag=0;
                        a5.flag=0;
                        a6.flag=0;
                        a7.flag=0;
                        a8.flag=0;

                        isbyte = bis.read(a1.arr, 0,a1.arr.length);
                        System.out.println("Packet:"+(i+1)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a2.arr, 0,a2.arr.length);
                        System.out.println("Packet:"+(i+2)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a3.arr, 0,a3.arr.length);
                        System.out.println("Packet:"+(i+3)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a4.arr, 0,a4.arr.length);
                        System.out.println("Packet:"+(i+4)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a5.arr, 0,a5.arr.length);
                        System.out.println("Packet:"+(i+5)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a6.arr, 0,a6.arr.length);
                        System.out.println("Packet:"+(i+6)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a7.arr, 0,a7.arr.length);
                        System.out.println("Packet:"+(i+7)+"ready");
                        checksum += isbyte;

                        isbyte=bis.read(a8.arr, 0,a8.arr.length);
                        System.out.println("Packet:"+(i+8)+"ready");
                        checksum += isbyte;

                        for(int j=1 ;j<=8;j++){
                            send(j);        
                        //  Thread.sleep(500);
                        }
                        //Thread.sleep(1000);

                        checkdat(1);

                        //Thread.sleep(10000);
                }

                        checkdat(1);
                        System.out.println(checksum+"data sent");

            }catch (Exception e) {
                // TODO: handle exception
            }
        }

        private void checkdat(int j) throws Exception {
            // TODO Auto-generated method stub
            switch(j){
            case 1 :    if(a1.flag==0){ send(1); }
            case 2 :    if(a2.flag==0){ send(2); }
            case 3 :    if(a3.flag==0){ send(3); }
            case 4 :    if(a4.flag==0){ send(4); }
            case 5 :    if(a5.flag==0){ send(5); }
            case 6 :    if(a6.flag==0){ send(6); }
            case 7 :    if(a7.flag==0){ send(7); }
            case 8 :    if(a8.flag==0){ send(8); }
                        break;
            default :   break;
            }
        }

        private void send(int j) throws Exception {
            // TODO Auto-generated method stub
            setbufr(j);
            os.write(dd, 0, packetsize);
            System.out.println("packet "+j+" recieved");
            os.flush();         
        }

        private void setbufr(int j) {
            // TODO Auto-generated method stub
            switch(j){
            case 1 :    System.arraycopy(a1.arr, 0, dd, 0, packetsize);
                        break;
            case 2 :    System.arraycopy(a2.arr, 0, dd, 0, packetsize);
                        break;
            case 3 :    System.arraycopy(a3.arr, 0, dd, 0, packetsize);
                        break;
            case 4 :    System.arraycopy(a4.arr, 0, dd, 0, packetsize);
                        break;
            case 5 :    System.arraycopy(a5.arr, 0, dd, 0, packetsize);
                        break;
            case 6 :    System.arraycopy(a6.arr, 0, dd, 0, packetsize);
                        break;
            case 7 :    System.arraycopy(a7.arr, 0, dd, 0, packetsize);
                        break;
            case 8 :    System.arraycopy(a8.arr, 0, dd, 0, packetsize);
                        break;
            default :   break;
            }

        }
    };
    Thread t2 = new Thread(){
        public void run(){
            try{
                BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                Integer da = Integer.parseInt(br.readLine());
                donef(da);
            }catch (Exception e) {
                // TODO: handle exception
            }
        }

        private void donef(Integer da) {
            // TODO Auto-generated method stub
            switch(da){
            case 1 :    a1.flag=1;
                        break;
            case 2 :    a2.flag=1;
                        break;
            case 3 :    a3.flag=1;
                        break;
            case 4 :    a4.flag=1;
                        break;
            case 5 :    a5.flag=1;
                        break;
            case 6 :    a6.flag=1;
                        break;
            case 7 :    a7.flag=1;
                        break;
            case 8 :    a8.flag=1;
                        break;
            default :   break;
            }
        }
    };

        t1.start();
        t1.setPriority(10);
        t2.start();
      //sock.close();

    }
  }

客户代码:

public class client {

  public static void main(String[] args) throws Exception{
      final fbufr a1 = null,a2 = null,a3 = null,a4 = null,a5 = null,a6 = null,a7 = null,a8 = null;
       final byte[] dd = new byte[1024];

    int pos=0;

    final int packetsize=1024;
    Scanner s= new Scanner(System.in);
    final double nosofpackets=s.nextDouble();
    //final double nosofpackets=Math.ceil(((int) myFile.length())/packetsize);
    System.out.println(nosofpackets);
    final Socket sock = new Socket("127.0.0.1", 5001);
    final OutputStream os = sock.getOutputStream();
    final FileOutputStream fos = new FileOutputStream("Music.mp4");
    final InputStream bis = sock.getInputStream();

    //Timer tym1,tym2;
    //Timer tym1= new Timer(){};

    Thread t1 = new Thread(){

        public void run(){
            try{

                int isbyte=0;
                    for(double i=0;i<nosofpackets+1;i++)
                    {
                    isbyte = bis.read(dd, 0,dd.length);if(isbyte==-1)System.out.println("data received");
                    setbufr((int)i%8);

                    }
            }catch (Exception e) {
                // TODO: handle exception
            }
        }

        private void rece(int j) throws Exception {
            // TODO Auto-generated method stub
            setbufr(j);
            bis.read(dd, 0, packetsize);
            System.out.println("packet "+j+" recieved");
            os.flush();         
        }

        private void setbufr(int j) {
            // TODO Auto-generated method stub
            switch(j){
            case 1 :    System.arraycopy(dd, 0, a1.arr, 0, packetsize);a1.flag=1;
                        break;
            case 2 :    System.arraycopy(dd, 0, a2.arr, 0, packetsize);a2.flag=1;
                        break;
            case 3 :    System.arraycopy(dd, 0, a3.arr, 0, packetsize);a3.flag=1;
                        break;
            case 4 :    System.arraycopy(dd, 0, a4.arr, 0, packetsize);a4.flag=1;
                        break;
            case 5 :    System.arraycopy(dd, 0, a5.arr, 0, packetsize);a5.flag=1;
                        break;
            case 6 :    System.arraycopy(dd, 0, a6.arr, 0, packetsize);a6.flag=1;
                        break;
            case 7 :    System.arraycopy(dd, 0, a7.arr, 0, packetsize);a7.flag=1;
                        break;
            case 8 :    System.arraycopy(dd, 0, a8.arr, 0, packetsize);a8.flag=1;
                        break;
            default :   break;
            }
        }
    };

    Thread t2 = new Thread(){
        public void run(){
            try{
                int x = 0;
                x=sendack();
                int n=sack(x);
                if((x!=0)&&(n==1)){
                PrintWriter pwr = new PrintWriter(sock.getOutputStream());
                pwr.println( x);
                pwr.flush();
                }
            }catch (Exception e) {
                // TODO: handle exception
            }
        }

        private int sack(int x) {
            // TODO Auto-generated method stub
            switch(x){
            case 1:return a1.m;
            case 2:return a2.m;
            case 3:return a3.m;
            case 4:return a4.m;
            case 5:return a5.m;
            case 6:return a6.m;
            case 7:return a7.m;
            case 8:return a8.m;

            default: break;

            }
            return 0;
        }

        private int sendack() {
            // TODO Auto-generated method stub
            switch(1){
            case 1:     if(a1.flag==1 && a1.c==false){a1.c=true;a1.m++;return 1;}
            case 2:     if(a2.flag==1 && a2.c==false){a2.c=true;a2.m++;return 2;}
            case 3:     if(a3.flag==1 && a3.c==false){a3.c=true;a3.m++;return 3;}
            case 4:     if(a4.flag==1 && a4.c==false){a4.c=true;a4.m++;return 4;}
            case 5:     if(a5.flag==1 && a5.c==false){a5.c=true;a5.m++;return 5;}
            case 6:     if(a6.flag==1 && a6.c==false){a6.c=true;a6.m++;return 6;}
            case 7:     if(a7.flag==1 && a7.c==false){a7.c=true;a7.m++;return 7;}
            case 8:     if(a8.flag==1 && a8.c==false){a8.c=true;a8.m++;return 8;}
                        break;
            default:    return 0;
            }
            return 0;
        }
    };
    t1.start();
    t2.start();
      //sock.close();
  }
}

fbufr.java:

public class fbufr {

    byte[] arr=new byte[1024];
    int flag;
    boolean c;
    int m;
    //Timer tym= new Timer();
    public fbufr(){
        flag=0;
        c=false;
        m=0;
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您的问题出在以下方面:

    } catch (Exception e) {
        // TODO: handle exception
    }

我怀疑您正在尝试从您的文件中读取IOException或您的程序的其他部分正在抛出。您应该永远只捕获和删除异常。至少你应该打印或记录它们。在这种情况下,他们可以隐藏您的应用程序的关键问题。这是推荐阅读的好tutorial on exceptions

此外,不应使用try { ... } catch (Exception e) { ... }包含大量代码块,而应使用较小的try / catch块捕获更具体的异常类。这意味着您不会错误地掩盖重要错误。例如:

BufferedInputStream bis;
try {
   bis = new BufferedInputStream(new FileInputStream(myFile));
} catch (IOException e) {
   // print it
   e.printStackTrace(System.err);
   // or log it
   logger.error("problems reading from " + myFile, e);
}

如果这不是问题,那么我建议您阅读debugging your application in Eclipse(或您正在使用的任何IDE)。在run()方法开始时设置一个断点,然后向前走,直到看到它到达预期的位置。