943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1727
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 16th, 2008
0

Problem with remote applications interaction using URL

Expand Post »
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:
java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

Please, at least somebody!
Any help at all would be greatly appreciated!
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

here ya go, just needs slight modification on response and your listening ports

http://www.codeproject.com/KB/IP/tcpclientserver.aspx
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 17th, 2008
1

Re: Problem with remote applications interaction using URL

sorry, forgot it was java, here's a sample java tcplistener

http://www.koders.com/java/fid857615...7AE75C4F5.aspx
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

Yeah ... THANKS... IT WORKS (almost). So I have another problem:
server catches the client connections, but when I'm trying to read from InputStream
java Syntax (Toggle Plain Text)
  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
java Syntax (Toggle Plain Text)
  1. while ((line = is.readLine()) != null)
like waiting for reading something else from stream.
What he is waiting for? Help
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

I already tried -1 - same. And I took "null" from java help
Quote ...
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?
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

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
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

Server:
java Syntax (Toggle Plain Text)
  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:
java Syntax (Toggle Plain Text)
  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...
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 17th, 2008
0

Re: Problem with remote applications interaction using URL

oops.. sorry for russian messages. It is only messages like "Server started" or "IO error". If it necessary I can translate it...
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Listing questions
Next Thread in Java Forum Timeline: need working code in java of SMTP and pop3





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC