无连接可以作出,因为目标机器积极地拒绝它127.0.0.1:3446 [英] No connection could be made because the target machine actively refused it 127.0.0.1:3446

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

问题描述

我使用的是WCF4.0模板 - REST 。我试图使该使用流上传文件的方法。

问题总是出现在

 流serverStream = request.GetRequestStream();

班流:

 命名空间LogicClass
{
    公共类StreamClass:IStreamClass
    {
        公共BOOL UploadFile(字符串文件名,流FILESTREAM)
        {
            尝试
            {
                的FileStream fileToupload =新的FileStream(文件名,FileMode.Create);
                字节[]字节组=新的字节[10000]
                INT读取动作,totalBytesRead = 0;
                做
                {
                    读取动作= fileStream.Read(字节数组,0,bytearray.Length);
                    totalBytesRead + =读取动作;
                }而(读取动作大于0);                fileToupload.Write(字节数组,0,bytearray.Length);
                fileToupload.C​​lose();
                fileToupload.Dispose();
            }
            赶上(例外前){抛出新的异常(ex.Message); }
            返回true;
        }
    }
}

REST项目:

  [WebInvoke(UriTemplate =AddStream / {文件名},方法=POST,BodyStyle = WebMessageBodyStyle.Bare)
公共BOOL AddStream(字符串文件名,FILESTREAM的System.IO.Stream)
{
    LogicClass.FileComponent休息=新LogicClass.FileComponent();
    返回rest.AddStream(文件名,文件流);
}

Windows窗体项目:用于测试

 私人无效button24_Click(对象发件人,EventArgs的发送)
{
    字节[] FILESTREAM;
    使用(的FileStream FS =新的FileStream(E:\\\\ stream.txt,FileMode.Open,FileAccess.Read,FileShare.Read))
    {
        FILESTREAM =新的字节[fs.Length]
        fs.Read(FILESTREAM,0,(INT)fs.Length);
        fs.Close();
        fs.Dispose();
    }    字符串baseAddress =HTTP://本地主机:3446 /文件/ AddStream / stream.txt
    HttpWebRequest的要求=(HttpWebRequest的)HttpWebRequest.Create(baseAddress);
    request.Method =POST;
    request.ContentType =text / plain的;
    流serverStream = request.GetRequestStream();
    serverStream.Write(FILESTREAM,0,fileStream.Length);
    serverStream.Close();
    使用(HttpWebResponse响应= request.GetResponse()作为HttpWebResponse)
    {
        INT状态code =(int)的response.Status code;
        StreamReader的读者=新的StreamReader(response.GetResponseStream());
    }
}

我已经关闭了防火墙和我的互联网连接,但错误依然存在。有测试上传方法更好的办法?

堆栈跟踪:

 在System.Net.Sockets.Socket.DoConnect(端点endPointSnapshot,SocketAddress的SocketAddress的)
   在System.Net.ServicePoint.ConnectSocketInternal(布尔connectFailure,插座S4,S6插座,插座及放大器;插座,ip地址和放大器;地址,ConnectSocketState状态,IAsyncResult的asyncResult,超时的Int32,异常和放大器;除外)


解决方案

积极地拒绝它指的是主机发送一个RST,而不是一个ACK,当你试图连接。因此,它是不是在你的code的一个问题。无论是有防火墙阻止连接或托管服务不侦听该端口的进程。这可能是因为它不是在所有的或者是因为它是一个不同的端口上侦听运行。

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

更新:在Linux上,你可能需要做 netstat的-anp 而不是

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

The problem always occur at

Stream serverStream = request.GetRequestStream();

Class for streaming:

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 project:

[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 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());
    }
}

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?

Stack trace:

 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.

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.

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

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

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