| | |
Problem with remote applications interaction using URL
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
please help with server code to catch this connection?
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)
URL url = new URL ("http://" + host + ":" + port); URLConnection c = url.openConnection(); c.setDoOutput(true); c.connect(); OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream()); out.write(URLEncoder.encode(request,"UTF-8") ); out.flush(); out.close();
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
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
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
here ya go, just needs slight modification on response and your listening ports
http://www.codeproject.com/KB/IP/tcpclientserver.aspx
http://www.codeproject.com/KB/IP/tcpclientserver.aspx
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
sorry, forgot it was java, here's a sample java tcplistener
http://www.koders.com/java/fid857615...7AE75C4F5.aspx
http://www.koders.com/java/fid857615...7AE75C4F5.aspx
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
Yeah ... THANKS... IT WORKS (almost). So I have another problem:
server catches the client connections, but when I'm trying to read from InputStream
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
like waiting for reading something else from stream.
What he is waiting for? Help
server catches the client connections, but when I'm trying to read from InputStream
java Syntax (Toggle Plain Text)
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); //.... String line; while ((line = is.readLine()) != null) { request += "\n" + line; }
"
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)
while ((line = is.readLine()) != null)
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
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
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
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
www.houseshark.net
I already tried -1 - same. And I took "null" from java help
Can you tell what you mean as period?
•
•
•
•
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
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
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
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
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
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
www.houseshark.net
Server:
Client:
There was no case in which server send response and client recieves it. So It can be mistakes there...
java Syntax (Toggle Plain Text)
private Socket socket = null; private BufferedReader is = null; private PrintWriter os = null; private String request = ""; private ServerSocket ss = null; private int port = 2604; public void run() { try { String line; while ((line = is.readLine()) != null) { request += "\n" + line; } System.out.println("Request from ctient: " + request); System.out.println("Processing..."); String msgToReport = AnalyzeEntryRequest(request); System.out.println("Sending: " + msgToReport); os.println(msgToReport); os.flush(); } catch (IOException e) { System.out.println("Ошибка ввода-вывода. " + e); } finally { try { is.close(); os.close(); socket.close(); System.out.println("Клиент " + socket + " отсоединен."); } catch (IOException e) { System.out.println("Ошибка закрытия сокетов. " + e); } } } private boolean InitServer() { boolean res = true; try { is = new BufferedReader(new InputStreamReader(socket.getInputStream())); os = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); } catch (IOException e) { System.out.println("Ошибка создания потоков. " + e); } new Thread (this).start(); System.out.println("Запуск сервера."); return res; }
Client:
java Syntax (Toggle Plain Text)
private String sendRequest(String request) { String result = ""; this.host = jTextField7.getText(); InputStream in = null; OutputStreamWriter out = null; try { URL url = new URL ("http://" + host + ":" + port); URLConnection c = url.openConnection(); c.setDoOutput(true); c.connect(); out = new OutputStreamWriter(c.getOutputStream()); String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode(request, "UTF-8") + "\r\n\r\n"; out.write(data); out.flush(); out.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(c.getInputStream())); String line; while ((line = rd.readLine()) != null) { result += "\n" + line; } in.close(); } catch (Exception e) { JOptionPane.showMessageDialog(jTextPane1,"Ошибка передачи данных. " + e); } return result; }
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
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
![]() |
Similar Threads
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Listing questions
- Next Thread: need working code in java of SMTP and pop3
Views: 1316 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth chat class classes client code columns component data database draw eclipse error event exception expand file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jme jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string swing test testautomation threads time transfer tree windows






