什么“应用程序没有调用WSAStartup,或WSAStartup失败”是什么意思?

时间:2012-09-22 08:48:08

标签: c# sockets networking dongle wsastartup

我正在尝试开发通过网络连接到Web服务器的软件;一切正常,直到尝试使用加密狗来保护我的软件。我的加密狗有一些网络功能,它的API工作在网络基础设施下。当我将加密狗检查代码添加到我的程序时,我收到了这个错误:

"Either the application has not called WSAStartup, or WSAStartup failed"  

我把抛出异常的代码块。我得到例外的情景是;我登录到程序(一切正常)然后插上加密狗然后程序停止并要求加密狗我再次插入加密狗并尝试登录但我在线上有例外

  

response =(HttpWebResponse)request.GetResponse();

DongleService unikey = new DongleService();

                checkDongle = unikey.isConnectedNow();

                if (checkDongle)
                {
                    isPass = true;
                    this.username = txtbxUser.Text;
                    this.pass = txtbxPass.Text;
                    this.IP = combobxServer.Text;
                    string uri = @"https://" + combobxServer.Text + ":5002num_events=1";
                    request = (HttpWebRequest)WebRequest.Create(uri);
                    request.Proxy = null;
                    request.Credentials = new NetworkCredential(this.username, this.pass);
                    ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
                    response = (HttpWebResponse)request.GetResponse();

                    Properties.Settings.Default.User = txtbxUser.Text;
                    int index = _servers.FindIndex(p => p == combobxServer.Text);
                    if (index == -1)
                    {
                        _servers.Add(combobxServer.Text);
                        Config_Save.SaveServers(_servers);
                        _servers = Config_Save.LoadServers();
                    }

                    Properties.Settings.Default.Server = combobxServer.Text;
                    // also save the password
                    if (checkBox1.CheckState.ToString() == "Checked")
                        Properties.Settings.Default.Pass = txtbxPass.Text;

                    Properties.Settings.Default.settingLoginUsername = this.username;
                    Properties.Settings.Default.settingLoginPassword = this.pass;
                    Properties.Settings.Default.settingLoginPort = "5002";
                    Properties.Settings.Default.settingLoginIP = this.IP;
                    Properties.Settings.Default.isLogin = "guest";

                    Properties.Settings.Default.Save();
                    response.Close();
                    request.Abort();
                    this.isPass = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Please Insert Correct Dongle!", "Dongle Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }

1 个答案:

答案 0 :(得分:0)

WSAStartup是套接字库中的第一个函数,它在开始使用net时执行。您可以导入

 [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError=true)]
 static extern Int32 WSAGetLastError();

当引发异常时,执行WSAGetLastError并查看来自here的错误代码。希望它会对你有所帮助。在您获得Win32Exception的特定情况下,您可以使用@Patrick中的注释编写的异常NativeErrorCode。

相关问题