我无法获得客户端脚本来连接到本地主机服务器,TCP连接永远不会发生 [英] I can't get the client script to connect to the localhost server, the TCP connection never happens

查看:141
本文介绍了我无法获得客户端脚本来连接到本地主机服务器,TCP连接永远不会发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的客户端程序,该程序使用创建一个套接字

I wrote a simple client side program that creates a socket using

CFSteamCreatePairWithSocketToHost function

并连接到在端口8080上的本地主机上运行的服务器.它创建的套接字很好,但从未连接到服务器.我用C语言编写了服务器.它没有用,给了我一个

and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C. It didn't work and gave me a

kCFErrorDomainCFNetwork error 72000

,唯一可以中继的信息是,显然无法建立TCP连接,但不知道为什么.因此,我也尝试用C编写客户端脚本,并将其添加到我的Swift项目桥接头文件中,但所有脚本仍未连接.它可以很好地创建套接字,但无法连接到服务器,我也不知道为什么.

and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So I tried to write the client side script in C too and added it to my Swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why.

但是当我使用clang编译并运行它时,同一个C客户端脚本可以工作,但是当我用Xcode中的swift项目运行它时却没有连接.我的Mac是否阻止了库建立TCP连接?

But the same C client script worked when I compiled it using clang and ran it but didn't connect when I ran it with my swift project in Xcode. Is my mac blocking the libraries from making a TCP connection or something?

我什至都不知道要搜索什么.我发现的唯一问题是Github库中一个名为 starscream 的问题错误,我什至没有使用该库,并且答复是我们唯一可以从此错误中辨别的是TCP连接失败".

I don't even know what to search for. The only thing I found was an issue on a Github library called starscream which had the same errors I had and I'm not even using that library and the reply there was "the only thing we can discern from this error is that the TCP connection was unsuccessful".

这是我使用Swift 4连接到服务器的代码.服务器在本地主机的8080端口上运行.

Here's the code I used to connect to the server using Swift 4. The server is running on port 8080 on localhost.

class client:NSObject {
      var inputstream = InputStream!
      var outputstream = OutputStream!

      func setupNetworkCom() {
          var readstream = Unmanaged<CFReadStream>?
          var writestream = Unmanaged<CFWriteStream>?

          CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)

          inputstream = readstream!.takeRetainedValue()
          outputstream = writestream!.takeReatainedValue()

          inputstream.schedule(in: .current, forMode: .common)
          outputstream.schedule(in: .current, forMode: .common)

          inputstream.open()
          outputstream.open()
      }
}

我还尝试将"localhost"替换为"127.0.0.1",这是我为要在其上运行的服务器指定的IP,但仍然无法正常工作.

I've also tried replacing "localhost" with "127.0.0.1" which is the IP I specified for the server to run on but it still doesn't work.

推荐答案

单击您的项目设置,然后转到功能,您将看到应用程序沙箱.确保已打开它,然后启用传入连接和传出连接.

click on your project settings and go to capabilities there you'll see the app sandbox. make sure it's turned on and then enable incoming connections and outgoing connections.

这篇关于我无法获得客户端脚本来连接到本地主机服务器,TCP连接永远不会发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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