View Single Post
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