[Answer]
The answer to my original post is marked (Kimmax's first post).
It turns out that I should have initially mentioned that this is running (for now) out of an Android emulator. I found out that the IP for connecting to a localhost through the emulator is "10.0.2.2". Having the port number doesn't seem to matter. Running the same code as bare-bones as possible returned the page text. That meant it was something in Android causing the issue. Hopefully this post will save others some time.
[Original]
I'm new to web/socket programming, so I don't know any common practices specific to it.
I'm trying to send a fairly short string (maybe 32 characters max) to a php page (I control). That php page would then process that string as needed and send a response string back to the sender. This is the extent of the connection (time-wise) needed between client and server. I've scoured the web looking for a simple answer, but can't seem to find any I'm hoping someone here can help.
For example: let's say the initial request string is a name ("Bob"). The php page on the server receives that and through handling figures out that the proper return string/value (say, an age "31"). This string/value is then sent to and received by the origional requestor to be handled as needed.
I'm only looking for the most short and basic of answers. Right now the server is a wamp localhost with the php page I'm testing with in a subdirectory (localhost/subdir/test.php).
[Edit]
Code example after using Kimmax's example:
C#
public String DownloadFrom(String url)
{
string str = ""; // For debugging only
try
{
using(WebClient client = new WebClient())
{
str = client.DownloadString(new Uri(url));
return str;
}
catch(WebException e) {
Log.Error ("Error while receiving data from server:", e.Message);
return null;
}
}
}
Value being passed to this function is "http://localhost/php/test.php?name=Bob"
test.php:
<?php
include 'logging.php';
if(isset($_GET['name']) && !empty($_GET['name']))
{
$data = $_GET['name'];
logg("test.php: name received: " . $data);
$download = "Test string.";
logg("test.php: setting data for download: " . $download);
print $download;
}
?>
The logging.php file provides the logg function which just appends the given string to a file elsewhere.
The test.php page will output $download when you enter that address into a browser, but nothing is returned through the given C# code. Even the log file doesn't get written to through the C# code. After some debugging I noticed that the WebException was thrown. The exception message is "Error: ConnectFailure (Connection refused)".
Full exception stacktrace:
e {System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
at System.Net.HttpWebRequest.GetResponse () [0x00000] in <filename unknown>:0
at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in <filename unknown>:0
at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in <filename unknown>:0
at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x00000] in <filename unknown>:0 } System.Net.WebException
I was able to get text returned during a telnet localhost 80 GET, so it seems that the connection should be viable. Am I missing some permissions somewhere?
[Edit]
Nothing has changed after completely disabling the firewall and setting up port forwarding on my router - I'm not ruling anything out at this point. I've also tried using the local IP address (127.0.0.1) instead of localhost.
Snippet from access.log:
...
192.168.1.1 - - [01/Jul/2014:11:48:18 -0500] "GET /HNAP1/ HTTP/1.1" 404 292
192.168.1.1 - - [01/Jul/2014:11:48:18 -0500] "POST /JNAP/ HTTP/1.1" 404 291
192.168.1.1 - - [01/Jul/2014:11:48:18 -0500] "GET / HTTP/1.1" 200 4821
127.0.0.1 - - [01/Jul/2014:11:50:06 -0500] "GET /php/test.php?s=Bob HTTP/1.1" 200 16
127.0.0.1 - - [01/Jul/2014:11:50:11 -0500] "GET /php/test.php?s=Foo HTTP/1.1" 200 16
127.0.0.1 - - [01/Jul/2014:11:50:15 -0500] "GET /php/test.php?s=Bar HTTP/1.1" 200 16
The last three lines are from me typing in the link into a browser (Chrome). I believe that the first three are from the C# code - oddly, I think that they were written after I stopped debugging.
Nothing seems to be dumping in the apache_error.log file for these connect attempts.
There must be some minuscule detail I'm overlooking...