getRequestStream() throwing SocketException

Hello,

I posted this in another forum but I believe it may have been the wrong forum to find the answer I am looking for.

I am still very much a novice when it comes to .NET and programming in general so please excuse my naivete.

I have a Windows Mobile 5.0 app written in Visual C# - .NET CF 2.0

I had a very simple login program that would query a MySQL database on a server and return some info about the user logging in. This code was working fine a couple weeks ago, and I got sidetracked to another project. Well, today I went to do some more work and I noticed that I could no longer login. My database was accessible and my login info was correctly entered. So I did some debugging and noticed that a line of code I had was throwing a WebException:

System.IO.Stream os = req.GetRequestStream();

Since WebException wouldn't give me many details I checked on the InnerException property and found it was actually a System.Net.Sockets.SocketException. I was unable to catch a SocketException so i threw the WebException's InnerException to access to error code. The code I received was 10054, saying "An existing connection was forcibly closed by the remote host"

I'm not really sure why this is happening, as I haven't had this problem before, but it doesn't seem to be a problem with my app so much as it is a problem with the server. It is throwing the exception before it is even able to return the Stream, so thats about as far as I can get debugging.

Here is my code, thanks for your help:

private Driver getDriverFromLogin(string username, string password, string truck)
{
Driver d = null;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST"; //Set request method to POST

//Assemble POST
string post = "username=" + username + "&password=" + password + "&truck=" + truck;

byte[] bytes = Encoding.ASCII.GetBytes(post); //Usually about Dim:37
req.ContentLength = bytes.Length;

System.IO.Stream os = req.GetRequestStream();
//Throws WebException with
//InnerException = SocketException Error Code: 10054

//Nothing after this point is executed because of caught exception

os.Write(bytes, 0, bytes.Length);
os.Close();

string response = "";

WebResponse resp = req.GetResponse();
if (resp == null)
return null;

System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
response += sr.ReadToEnd().Trim();

d = this.parseResponse(response);

//MessageBox.Show(response, "Response");

return d;
}
catch (NotImplementedException e)
{
MessageBox.Show("NotImplementedException" + e.Message + "\n" + e.StackTrace);
return null;
}
catch (System.Net.WebException e)
{
try
{
MessageBox.Show("WebException: " + e.Message + "\n" + e.Status + "\n" + e.InnerException);
throw e.InnerException;
return null;
}
catch (System.Net.Sockets.SocketException sE)
{
MessageBox.Show(sE.ErrorCode.ToString());
return null;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + " " + e.InnerException);
return null;
}
}



Answer this question

getRequestStream() throwing SocketException

  • getRequestStream() throwing SocketException