Home arrow static arrow Java Programming [Archive] - Client communication through proxies
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Client communication through proxies
This topic has 9 replies on 1 page.

Posts:30
Registered: 2/1/02
Client communication through proxies  
Aug 3, 2004 4:56 AM



 
Currently i am working on a client/server chat system. Using http url connection for communication to by pass proxies and firewalls as much as possible..Everything went smoothly until I tried to use this patch to retrieve data from the server

    public String MsgFromServer()    {        String str;         try        {            URL url = new URL (origUrl, encode("getNextMsg " + uniqNum++));                  //------------The real problem seem to start from here, thus not getting any message from server-------------            DataInputStream dataIn = new DataInputStream(new BufferedInputStream(url.openConnection().getInputStream()));            str = dataIn.readLine();             // Verify that the message is valid            if (!str.startsWith (PREFIX))                return null}        catch (Exception e)        {            System.out.println ("Error connecting to read from " + origUrl);            return null;        }         str = str.substring (PREFIX.length());        return str;    }


would really appreciate any help in this regard
 

Posts:8,813
Registered: 10/4/00
Re: Client communication through proxies  
Aug 3, 2004 5:01 AM (reply 1 of 9)



 
Does it compile? Are you getting an error? Does it hang?
 

Posts:19,725
Registered: 9/26/01
Re: Client communication through proxies  
Aug 3, 2004 5:04 AM (reply 2 of 9)



 
So do some debugging. Print url.toString(). Print str after you call readLine(), etc.
 

Posts:30
Registered: 2/1/02
Re: Client communication through proxies  
Aug 3, 2004 5:09 AM (reply 3 of 9)



 
it does complie and gave me no error while compilation ...

the system doesnt go further down from where i pointed... it just bounces to the catch block!
 

Posts:19,725
Registered: 9/26/01
Re: Client communication through proxies  
Aug 3, 2004 5:11 AM (reply 4 of 9)



 
So do some debugging. Oh gee, I already said that, huh?
 

Posts:30
Registered: 2/1/02
Re: Client communication through proxies  
Aug 3, 2004 5:15 AM (reply 5 of 9)



 
I did!!!!

sumhow i got to the main source of the problem.. now i only want to know if thats the best way to communicate through url connection..?!?! if not i would really appreciate any help from the 'o more experienced ones' ... i hope m more clear this time... danke!
 

Posts:19,725
Registered: 9/26/01
Re: Client communication through proxies  
Aug 3, 2004 5:16 AM (reply 6 of 9)



 
So do some debugging. Oh gee, I already said that,
huh?

(sigh) To spell it out for you, break this:
URL url = new URL (origUrl, encode("getNextMsg " + uniqNum++));
DataInputStream dataIn = new DataInputStream(new BufferedInputStream(url.openConnection().getInputStream()));

down to something like this:
String suffix = encode("getNextMsg " + uniqNum++);
System.out.println("suffix = " + suffix);
URL url = new URL(origUrl, suffix);
System.out.println("url = " + url);
UrlConnection conn = url.openConnection();
if (conn == null) {
System.out.println("oops! no connection here!");
}
InputStream inpStr = conn.getInputStream();
// etc etc etc
 

Posts:8,813
Registered: 10/4/00
Re: Client communication through proxies  
Aug 3, 2004 5:17 AM (reply 7 of 9)



 
Your first exception! Congratulations! Try seeing what the exception is. Use e.printStackTrace();
 

Posts:30
Registered: 2/1/02
Re: Client communication through proxies  
Aug 3, 2004 5:27 AM (reply 8 of 9)



 
well.. thanks for letting me kno wut debugging really is. .. :-) .. anyway... it seems like it makes the connection and all.. just cant seem to receive the datastream from the server.. or mayb i am not using the right function for it ?!?!
 

Posts:19,725
Registered: 9/26/01
Re: Client communication through proxies  
Aug 3, 2004 5:31 AM (reply 9 of 9)



 
DataInputStream is a binary stream - will only work if the server is using DataOutputStream to write its data. You're probably using the wrong class. If you want text line-oriented streams (such as if the server wrote using PrintWriter or such), then use BufferedReader, which has a readLine method to read the "line".
 
This topic has 9 replies on 1 page.