简体   繁体   English

无法通过套接字接收图像

[英]Trouble Receiving image over socket

im tryin to send an image over socket , the sender part -(android)- looks short and ok, but the receiver part - which is written by java - is supposed to rename the image and save it in the C:/... . 我试图通过套接字发送图像,发送器部分-(android)-看起来又短又好,但是接收器部分-由Java编写-应该重命名该图像并将其保存在C:/ ... 。 but i get nothing there and i cant find any problem with it .. 但是我什么都没有,我找不到任何问题..

here is my server code: 这是我的服务器代码:

public void start() throws InterruptedException {
    keepGoing = true;
    try 
    {

       // the socket used by the server
       ServerSocket serverSocket = new ServerSocket(port);

       // infinite loop to wait for connections
       while(keepGoing) 
       {
           // format message saying we are waiting
           display("Server waiting for Clients on port " + port + ".");

           Socket socket = serverSocket.accept(); // accept connection
           // if I was asked to stop

           if(!keepGoing)
            break;
           ClientThread t = new ClientThread(socket); // make a hread of it
           jobdone=false;
           al.add(t); // save it in the ArrayList
           t.start();
        }
        // I was asked to stop
        try {
            serverSocket.close();
            for(int i = 0; i < al.size(); ++i) {
                ClientThread tc = al.get(i);
                try {
                tc.sInput.close();
                tc.sOutput.close();
                tc.socket.close();
                }
                catch(IOException ioE) {
                    // not much I can do
                }
            }
        }
        catch(Exception e) {
            display("Exception closing the server and clients: " + e);
        }
    }
    // something went bad
    catch (IOException e) {
                 String msg = sdf.format(new Date()) + 
                    " Exception on new ServerSocket: " + e + 
                    "\n";
         display(msg);
    }
}       
/*
 * For the GUI to stop the server
 */
protected void stop() {
    keepGoing = false;
    // connect to myself as Client to exit statement 
    // Socket socket = serverSocket.accept();
    try {
        new Socket("192.168.1.2", 1500);
    }
    catch(Exception e) {
        // nothing I can really do
    }
}
/*
 * Display an event (not a message) to the console or the GUI
 */
private void display(String msg) {
    String time = sdf.format(new Date()) + " " + msg;
    if(sg == null)
        System.out.println(time);
    else
        sg.appendEvent(time + "\n");
}

    // create a server object and start it



 public static void shutdown() {
jobdone = true;


 }
/** One instance of this thread will run for each client */

   class ClientThread extends Thread {
    // the socket where to listen/talk
    String Type;
    Socket socket;
    InputStream sInput;
    ObjectOutputStream sOutput;
    // my unique id (easier for deconnection)
    int id;



    // Constructore
    ClientThread(Socket socket) throws InterruptedException {
       // a unique id
       id = ++uniqueId;
       this.socket = socket;
       /* Creating both Data Stream */
       System.out.println("Thread trying to create Object I/O Streams");

       // create output first
       int bytesRead = 0;
               int current = 0;
               int filesize=65383; 
               byte [] mybytearray2  = new byte [filesize];
               InputStream is = null;
        try {
            is = socket.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("C:/IMG-20130112-WA0011.jpeg");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // destination path and name of file
        //FileOutputStream fos = new FileOutputStream("C:/");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        try {
            bytesRead = is.read(mybytearray2,0,mybytearray2.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        current = bytesRead;


        do {
           try {
       bytesRead =
       is.read(mybytearray2, current, (mybytearray2.length-current));
    } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
            if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
        try {
            bos.write(mybytearray2, 0 , current);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            bos.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        //System.out.println(end-start);
        try {
            bos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

am i doing any thing wrong guys ? 我做错什么了吗? thanks for reading in advance 感谢您提前阅读

文件路径应为C://而不是C:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM