通过套接字发送/接收ByteArray时,应用程序崩溃

时间:2013-01-22 09:33:34

标签: android sockets bytearray transfer datainputstream

我正在尝试使用套接字通信制作一个简单的聊天应用。 我的目标是成功发送和接收文本和图像(来自智能手机库)。 文本部分成功,但我遇到图像问题。

我使用dataInput / OutputStream编写了一个代码,下面是代码,它使用bytearray建立套接字连接以进行图像传输(应用程序使用不同的文本和图像端口)。

    class image_connection_thread extends Thread{
    public boolean flag=true;
    public void run(){
        try{
            socket2 = new Socket(Ip,port_img); 
            //Imgage Streams
            img_output= new DataOutputStream(socket2.getOutputStream());
            img_input= new DataInputStream(socket2.getInputStream());

            while(flag)
            {   
                ///////////////////
                img_input.readFully(byte_input);
                **//////   This line makes problem(App dies). But no exception message occurs.  ////** 

                if(byte_input==null)
                    break;
                image_received=BitmapFactory.decodeByteArray(byte_input, 0, byte_input.length);
                Message Msg = new Message();
                Msg.obj=image_received;
                handler_img.sendMessage(Msg);           
            }
            socket2.close();

        }catch(Exception e)
        {
            tv_Text.append(e.getMessage()+"connection failed3\n");
        }
    }       
} 

while循环是等待来自服务器的输入bytearray。处理程序在显示屏上显示解码的bytearray。

- > img_input.readFully(byte_input);我认为这条线会产生问题。我检查了那个处理程序是否运行良好,并且在使用空时,App没有死。

会出现什么问题?

服务器端代码如下所示。(省略消息线程)

public class server 
{
public static void main(String[] args) {
    Thread thread1=new port1_thread();
    Thread thread2=new port2_thread();
    thread1.start();        
    thread2.start();        
}
}

class port2_thread extends Thread
{
ServerSocket serversocket = null;
public void run()
{
    try
    {
        serversocket = new ServerSocket(9003);
        while(true)
        {
            Socket socket = serversocket.accept();              
            Thread thread= new image_thread(socket);            
            thread.start(); 
        }

    }catch(Exception e){System.out.println("1-2"+e.getMessage());}
}

}


class image_thread extends Thread
{
static ArrayList<DataOutputStream> list2 = new ArrayList<DataOutputStream>();
Socket socket;  
DataOutputStream output2 = null;

image_thread(Socket socket)//constructor
{
    this.socket=socket;
    try  //data output stream
    {
         output2 = new DataOutputStream(socket.getOutputStream());
         list2.add(output2);
    }
    catch (Exception e)
    {
         System.out.println("22"+e.getMessage());
    }
}

public void run()
{
    try
    {
        //output2= new DataOutputStream(socket.getOutputStream());
        DataInputStream input2=new DataInputStream(socket.getInputStream());
        while(true)
        {
            for (DataOutputStream output2 : list2) 
            {
                byte[] b = new byte[1024];  
                input2.readFully(b);
                output2.write(b);
                output2.flush();                

            }               
        }

    }catch(Exception e){System.out.println("33"+e.getMessage());}

    finally
    {
        list2.remove(output2);
        try
        {
            socket.close();
        }catch(Exception ignored){}
    }
}
}

0 个答案:

没有答案
相关问题