This project is read-only.

SocketException in windows service

Aug 12, 2014 at 3:22 AM
Edited Aug 12, 2014 at 3:33 AM
Hi,

I am trying to use FtpClient in windows service on Windows XP machine. No matter what I try, I am not even able to make the connection. Here is my code
            using(FtpClient con = new FtpClient()) {
                
                con.EnableThreadSafeDataConnections = false;
                
                con.Host = "XYZ";
                
                
                con.Credentials = new System.Net.NetworkCredential("anonymous","abc@xyz.com");
                
                using(var istream = con.OpenRead("/ROOT/Testing_Drop/Dump/chile.txt.txt", FtpDataType
                                                 .ASCII))
                {
                    string jobfilename = @"C:\Documents and Settings\manesing\My Documents\Work Area\Chile.txt";
                    using(var output=new BinaryWriter(File.Open(jobfilename, FileMode.Create))) {
                        
                        
                        byte[] buf = new byte[8192];
                        int read = 0;
                        try {
                            while ((read = istream.Read(buf, 0, buf.Length)) > 0) {
                                output.Write(buf,0,read);
                            }
                        }
                        catch(Exception ex)
                        {
                            EventLog.WriteEntry("TestFTP","FTP: " +ex.Message);
                        }
                        finally {
                            
                            istream.Close();
                        }


                        
                    }
                }
            }
The windows service always crash with below exception.
Service cannot be started. System.Net.Sockets.SocketException (0x80004005): An invalid argument was supplied
   at System.Net.Sockets.DynamicWinsockMethods.LoadDynamicFunctionPointer(SafeCloseSocket socketHandle, Guid& guid)
   at System.Net.Sockets.DynamicWinsockMethods.EnsureConnectEx(SafeCloseSocket socketHandle)
   at System.Net.Sockets.DynamicWinsockMethods.GetDelegate[T](SafeCloseSocket socketHandle)
   at System.Net.Sockets.Socket.ConnectEx(SafeCloseSocket socketHandle, IntPtr socketAddress, Int32 socketAddressSize, IntPtr buffer, Int32 dataLength, Int32& bytesSent, SafeHandle overlapped)
   at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
   at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
   at System.Net.Sockets.Socket.BeginConnect(IPAddress address, Int32 port, AsyncCallback requestCallback, Object state)
   at System.Net.FtpClient.FtpSocketStream.Connect(String host, Int32 port, FtpIpVersion ...
As I could run the same code in the console application, I was suspecting the issue with user permission. However, when I tried .NET native FTPWebRequest class, it worked without any issues.
I can even connect to FTP server using sockets and it connects just fine. Windows service is running with admin user privileges.


Here is the FTPTrace output:
Disposing FtpClient object...
Disposing FtpClient object...
Disposing FtpSocketStream...
As you can see, even the connection is not made. Let me know if I am missing something in the code.
FtpClient version is 1.0.5281.14359
Aug 15, 2014 at 10:26 PM
Hi,

For anyone facing the same issue, I finally found that the Winsock2 was corrupted on my system.

http://support.microsoft.com/kb/811259

Followed the above link and was able to correct the problem. Now FtpClient works as it should.

Here is the workstation information
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 3 Build 2600
Marked as answer by mannbiher on 8/15/2014 at 2:26 PM