Problem with remote applications interaction using URL

Thread Solved
Reply

Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Problem with remote applications interaction using URL

 
0
  #1
Nov 16th, 2008
Hello everyone, beforehand, sorry for my english...
I have to do client - server application. They must interact, using classes URL, URLConnection. I understand, how to send data to server. But I can not realize server work.
The common algorithm of server is:
1. Start.
2. Wait for connection.
3. After receiving connection it must process request.
4. Or back for "2" or exit.

as I understand, client code to send request is:
  1. URL url = new URL ("http://" + host + ":" + port);
  2. URLConnection c = url.openConnection();
  3. c.setDoOutput(true);
  4. c.connect();
  5.  
  6. OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
  7. out.write(URLEncoder.encode(request,"UTF-8") );
  8. out.flush();
  9. out.close();
please help with server code to catch this connection?
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Problem with remote applications interaction using URL

 
0
  #2
Nov 17th, 2008
Please, at least somebody!
Any help at all would be greatly appreciated!
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Problem with remote applications interaction using URL

 
0
  #3
Nov 17th, 2008
here ya go, just needs slight modification on response and your listening ports

http://www.codeproject.com/KB/IP/tcpclientserver.aspx
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Problem with remote applications interaction using URL

 
1
  #4
Nov 17th, 2008
sorry, forgot it was java, here's a sample java tcplistener

http://www.koders.com/java/fid857615...7AE75C4F5.aspx
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Problem with remote applications interaction using URL

 
0
  #5
Nov 17th, 2008
Yeah ... THANKS... IT WORKS (almost). So I have another problem:
server catches the client connections, but when I'm trying to read from InputStream
  1. BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  2.  
  3. //....
  4.  
  5. String line;
  6. while ((line = is.readLine()) != null)
  7. {
  8.  
  9. request += "\n" + line;
  10. }
it reads information:
"
POST / HTTP/1.1
Connection: close
Keep-Alive: 300
User-Agent: Java/1.6.0_07
Host: localhost:2604
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-type: application/x-www-form-urlencoded
Content-Length: 38

key1=search%09StudentRecBook%09123"
and after that stops at
  1. while ((line = is.readLine()) != null)
like waiting for reading something else from stream.
What he is waiting for? Help
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Problem with remote applications interaction using URL

 
0
  #6
Nov 17th, 2008
i don't think it will return null, until the socket gets closed

not for sure if this is the terminator, but did you check for "." (period) with crlf?

also check -1
Last edited by dickersonka; Nov 17th, 2008 at 3:51 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Problem with remote applications interaction using URL

 
0
  #7
Nov 17th, 2008
I already tried -1 - same. And I took "null" from java help
java.​io.​BufferedReader
public String readLine() throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Can you tell what you mean as period?
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Problem with remote applications interaction using URL

 
0
  #8
Nov 17th, 2008
nm, think i was thinking of something else

can you please post the server and client code, at least the guts part where its sending and receiving
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Problem with remote applications interaction using URL

 
0
  #9
Nov 17th, 2008
Server:
  1. private Socket socket = null;
  2. private BufferedReader is = null;
  3. private PrintWriter os = null;
  4. private String request = "";
  5. private ServerSocket ss = null;
  6. private int port = 2604;
  7.  
  8. public void run()
  9. {
  10. try
  11. {
  12.  
  13. String line;
  14. while ((line = is.readLine()) != null) {
  15. request += "\n" + line;
  16. }
  17.  
  18. System.out.println("Request from ctient: " + request);
  19. System.out.println("Processing...");
  20. String msgToReport = AnalyzeEntryRequest(request);
  21. System.out.println("Sending: " + msgToReport);
  22. os.println(msgToReport);
  23. os.flush();
  24. }
  25. catch (IOException e)
  26. {
  27. System.out.println("Ошибка ввода-вывода. " + e);
  28. }
  29. finally
  30. {
  31. try
  32. {
  33. is.close();
  34. os.close();
  35. socket.close();
  36. System.out.println("Клиент " + socket + " отсоединен.");
  37. }
  38. catch (IOException e)
  39. {
  40. System.out.println("Ошибка закрытия сокетов. " + e);
  41. }
  42. }
  43. }
  44. private boolean InitServer()
  45. {
  46. boolean res = true;
  47.  
  48. try
  49. {
  50. is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  51. os = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
  52. }
  53. catch (IOException e)
  54. {
  55. System.out.println("Ошибка создания потоков. " + e);
  56. }
  57. new Thread (this).start();
  58. System.out.println("Запуск сервера.");
  59.  
  60. return res;
  61. }

Client:
  1. private String sendRequest(String request)
  2. {
  3. String result = "";
  4. this.host = jTextField7.getText();
  5. InputStream in = null;
  6. OutputStreamWriter out = null;
  7. try
  8. {
  9. URL url = new URL ("http://" + host + ":" + port);
  10. URLConnection c = url.openConnection();
  11. c.setDoOutput(true);
  12. c.connect();
  13.  
  14. out = new OutputStreamWriter(c.getOutputStream());
  15.  
  16. String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode(request, "UTF-8") + "\r\n\r\n";
  17.  
  18. out.write(data);
  19. out.flush();
  20.  
  21. out.close();
  22.  
  23. BufferedReader rd = new BufferedReader(new InputStreamReader(c.getInputStream()));
  24. String line;
  25. while ((line = rd.readLine()) != null)
  26. {
  27.  
  28. result += "\n" + line;
  29. }
  30.  
  31. in.close();
  32. }
  33. catch (Exception e)
  34. {
  35. JOptionPane.showMessageDialog(jTextPane1,"Ошибка передачи данных. " + e);
  36. }
  37.  
  38. return result;
  39. }
There was no case in which server send response and client recieves it. So It can be mistakes there...
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Problem with remote applications interaction using URL

 
0
  #10
Nov 17th, 2008
oops.. sorry for russian messages. It is only messages like "Server started" or "IO error". If it necessary I can translate it...
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC