无法建立连接,因为目标机器主动拒绝它 127.0.0.1:3446 [英] No connection could be made because the target machine actively refused it 127.0.0.1:3446

查看:67
本文介绍了无法建立连接,因为目标机器主动拒绝它 127.0.0.1:3446的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WCF4.0 模板 -REST.我正在尝试制作一种使用流上传文件的方法.

I'm using the WCF4.0 template -REST. I'm trying to make a method that uploads a file using a stream.

问题总是发生在

Stream serverStream = request.GetRequestStream();

流类:

namespace LogicClass
{
    public class StreamClass : IStreamClass
    {
        public bool UploadFile(string filename, Stream fileStream)
        {
            try
            {
                FileStream fileToupload = new FileStream(filename, FileMode.Create);
                byte[] bytearray = new byte[10000];
                int bytesRead, totalBytesRead = 0;
                do
                {
                    bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                    totalBytesRead += bytesRead;
                } while (bytesRead > 0);

                fileToupload.Write(bytearray, 0, bytearray.Length);
                fileToupload.Close();
                fileToupload.Dispose();
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            return true;
        }
    }
}

REST 项目:

[WebInvoke(UriTemplate = "AddStream/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
public bool AddStream(string filename, System.IO.Stream fileStream)
{
    LogicClass.FileComponent rest = new LogicClass.FileComponent();
    return rest.AddStream(filename, fileStream);
}

Windows 窗体项目:用于测试

Windows Form project: for testing

private void button24_Click(object sender, EventArgs e)
{
    byte[] fileStream;
    using (FileStream fs = new FileStream("E:\stream.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        fileStream = new byte[fs.Length];
        fs.Read(fileStream, 0, (int)fs.Length);
        fs.Close();
        fs.Dispose();
    }

    string baseAddress = "http://localhost:3446/File/AddStream/stream.txt";
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
    request.Method = "POST";
    request.ContentType = "text/plain";
    Stream serverStream = request.GetRequestStream();
    serverStream.Write(fileStream, 0, fileStream.Length);
    serverStream.Close();
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        int statusCode = (int)response.StatusCode;
        StreamReader reader = new StreamReader(response.GetResponseStream());
    }
}

我已关闭防火墙和 Internet 连接,但错误仍然存​​在.有没有更好的方法来测试上传方法?

I've turned off the firewall and my Internet connection, but the error still exists. Is there a better way of testing the uploading method?

堆栈跟踪:

在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket&socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& 异常)

at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

推荐答案

主动拒绝它"意味着当您尝试连接时主机发送了重置而不是确认.因此,这在您的代码中不是问题.要么是防火墙阻止了连接,要么是托管服务的进程未在该端口上侦听.这可能是因为它根本没有运行,或者因为它正在侦听不同的端口.

"Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. This may be because it is not running at all or because it is listening on a different port.

一旦您开始托管您的服务的进程,请尝试 netstat -anb(需要管理员权限)以验证它是否正在运行并在预期的端口上侦听.

Once you start the process hosting your service, try netstat -anb (requires admin privileges) to verify that it is running and listening on the expected port.

更新:在 Linux 上,您可能需要改为执行 netstat -anp.

update: On Linux you may need to do netstat -anp instead.

这篇关于无法建立连接,因为目标机器主动拒绝它 127.0.0.1:3446的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆