1

I'm trying to make a simple chat server. It works fine on the iphone, but not all the chats are going through on the android.

My idear was to use the http protocol so I can use the standard sfkd on the iphone and android to talk to my server

I'm using the URLConnection connection class on the android. When I was tacing the code on my server, I noticed that I was getting the length of the post data sent in the header. I'm not setting any length value on the URLConnection class. I figured since I was not setting the size in the header that is why its not working. I could not find any info about this.

code that reads in haeder on my server

I have my server code and android code below,

public int ReadHeader()
     {
        // read in one line
         int PageId=0;

    //   try{
            request = new StringBuffer(1000);
            System.out.println("get connection reading in header \r");
            //InputStream in = new BufferedInputStream( connection.getInputStream() );
            StopFlag=false;

            String out;
            // the first line shouls have the page
            out=ReadLine();

            int p;
            p=out.lastIndexOf("stop");
            if (p!=-1)
                PageId=1;
            p=out.lastIndexOf("enter");
            if (p!=-1)
                PageId=2;
            p=out.lastIndexOf("add");
            if (p!=-1)
                PageId=3;
            p=out.lastIndexOf("exit");
            if (p!=-1)
                PageId=4;
            p=out.lastIndexOf("update");
            if (p!=-1)
                PageId=5;

            int MessageSize=0;
            do{
                out=ReadLine();

                // check for content size
                // GET SIZR OF DATA SENT

                if (out.contains("Length"))
                {
                    System.out.println("found length \r");
                    int pes=out.indexOf(' ');
                    String Length=out.substring(pes+1);
                    System.out.println("found size");
                    System.out.println(Length); 
                    //MessageSize=(int)Length;
                    MessageSize= Integer.parseInt( Length) ;
                                      //MessageSize=36;     
                }
            } while(out!="finish header" && out!="");

            System.out.println("finsih header \r"); 
            ClientMessage=ReadSize(MessageSize); 
            /*
         } catch(IOException ec)
            {
                System.out.println(ec.getMessage());    
            }
        */
       return PageId;
     }

// CODE ON ANDROID 
           String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode( cGlobals.UserName, "UTF-8"); data += "&" + URLEncoder.encode("comment", "UTF-8") + "=" + URLEncoder.encode( s, "UTF-8");
                           cGlobals.Message=""; // THIS IS NOT TKING IN ANY INFO ABOUT THE LENGTH OF data                           URLConnection conn = url.openConnection();
                           // set timeouts to 5 seconds
           //              conn.setConnectTimeout(1000*5);
           //              conn.setReadTimeout(5*1000);
                           conn.setDoOutput(true);


                           OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());


                      wr.write(data);
                       wr.flush();
                       wr.close();
4

0 に答える 0