为什么我无法从服务器获取数据到客户端?

时间:2017-05-01 05:59:02

标签: c# networking server udp client

我从控制台向服务器发送文本,服务器将文件发送给我。

        static void Main(string[] args)
    {
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint ipserv = new IPEndPoint(IPAddress.Parse("46.174.49.48"), 46666);
        client.Connect(ipserv);
        byte[] data = Encoding.UTF8.GetBytes(Console.ReadLine());
        client.Send(data);
        client.Receive(data); //here is the error
        Console.WriteLine(Encoding.UTF8.GetString(data));
        Console.ReadLine();
    }

错误:远程主机强行破坏现有连接

服务器工作

1 个答案:

答案 0 :(得分:0)

您应该尝试使用try-catch语句以便更好地理解。由于以下一个或多个原因,它正在变化: 1)您正在向应用程序发送格式错误的数据或(2)客户端和服务器之间的网络链接因某种原因而关闭......(3)您已触发第三方应用程序中导致的错误崩溃(4)第三方应用程序耗尽了系统资源我可能会说第一个是数字1。

实施例:::

private MailMessage mail = new MailMessage();

void SendEmails()
{
    mail.From = new MailAddress("GIVE_YOUR_EMAIL_HERE");
    mail.To.Add("GIVE_YOUR_DESTINATION_HERE");

    SmtpClient smtpServer = new SmtpClient("GIVE_SMTP_INFO_HERE");
    smtpServer.Port = 587;//GIVE CORRECT PORT HERE
    mail.Subject = "WHATEVER_YOU_WANT_TEXT";
    mail.Body = "WHATEVER_YOU_WANT_TEXT";
    smtpServer.Credentials = new System.Net.NetworkCredential("GIVE_SMTP_INFO_HERE", "GIVE_YOUR_EMAIL_PASSWORD_HERE") as ICredentialsByHost;
    smtpServer.EnableSsl = true;
    ServicePointManager.ServerCertificateValidationCallback =
    delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    { return true; };
    smtpServer.Send(mail);
    //smtpServer.SendAsync(mail)
    Debug.Log("success");
}