像在 Java 中一样在 Swift 中使用套接字 [英] Using sockets in Swift like in Java

查看:21
本文介绍了像在 Java 中一样在 Swift 中使用套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想连接到服务器,在 Java 中我会打开一个 Socket 并用端口和主机地址初始化它,然后检索输入/输出流并读/写我想要的任何内容.

If I wanted to connect to a server, in Java I would open a Socket and initialize it with port and host address, then retrieve the input/output streams and read/write whatever I want.

在 Swift 中,我很难这样做,因为它不是那样构建的,我真的很想看一个简单的例子,说明如何连接到服务器、检索流并使用它们.

In Swift I'm having hard time doing so since it's not built that way and I would really like to see a simple example of how to connect to a server, retrieve the streams and use them.

这是@Grimxn 引用后经过测试的代码.

This is the tested code after what @Grimxn referenced.

var host = "http://google.com"
var readStream :CFReadStreamRef
var writeStream :CFWriteSteamRef
var socket = CFStreamCreatePairWithSocketToHost(nil, host, 80, readStream, writeStream)

主要问题:

  1. 初始化上面的两个流还需要使用CFAllocator,我对此一无所知.使用 kCFAllocatorDefault 并没有太大帮助,同样的错误.

  1. Initializing the two streams above also require the use of CFAllocator which I know nothing about. Using kCFAllocatorDefault did not quite help, same errors.

以上代码返回此错误:无法将表达式的类型Void"转换为类型 UInt32.

The above code returns this error: Cannot Convert the expression's type 'Void' to type UInt32.

例如,当我使用 UInt32(80) 构建 UInt32 时,错误是:找不到 ' 的重载init' 接受提供的参数.

When I construct a UInt32 using UInt32(80) for example, the error is: Could not find an overload for 'init' that accepts the supplied argument.

感谢您的帮助!

推荐答案

我自己已经想到了,想看解释的人请提前阅读;

I have figured it myself, for those who look for an explanation read ahead;

有多种使用套接字与本地应用程序或远程服务器进行通信的方式.

There are multiple ways of using Sockets to communicate with local application or a remote server.

原帖中描述的问题是获取输入/输出流并使它们工作.(在这篇文章的末尾,引用了我的另一篇文章,解释了如何使用这些流)

The problem described in the original post was to get the Input/Output streams and get them to work. (At the end of this post there's a reference to another post of mine explaining how to use those streams)

NSStream 类有一个名为 getStreamsToHost 的静态方法(swift 中的类函数).所有你需要准备的是一个 NSHost 对象,它用一个真实的主机地址、一个端口号、一个对 NSInputStream obj 的引用以及一个 NSOutputStream obj 初始化.然后,您可以使用此处显示的流,并在参考文章.

看看这个简单的代码;

let addr = "127.0.0.1"
let port = 4000

var host :NSHost = NSHost(address: addr)
var inp :NSInputStream?
var out :NSOutputStream?

NSStream.getStreamsToHost(host, port: port, inputStream: &inp, outputStream: &out)

let inputStream = inp!
let outputStream = out!
inputStream.open()
outputStream.open()

var readByte :UInt8 = 0
while inputStream.hasBytesAvailable {
    inputStream.read(&readByte, maxLength: 1)
}

// buffer is a UInt8 array containing bytes of the string "Jonathan Yaniv.".
outputStream.write(&buffer, maxLength: buffer.count)

在执行这个简单的代码之前,我启动了 ncat 来监听终端中的 4000 端口并输入Hello !"并保持套接字打开以进行通信.

Before executing this simple code, I launched ncat to listen on port 4000 in my terminal and typed "Hello !" and left the socket opened for communication.

Jonathans-MacBook-Air:~ johni$ ncat -l 4000
Hello !
Jonathan Yaniv.
Jonathans-MacBook-Air:~ johni$ 

启动代码后,可以看到在socket关闭之前,我已经收到了到终端的字符串Jonathan Yaniv. ".

After launching the code, you can see that I have received the string "Jonathan Yaniv. " to the terminal before the socket was closed.

我希望这能让你们中的一些人免于头疼.如果您有更多问题,请试一试,希望我能回答.

I hope this saved some headache to some of you. If you have more questions give it a shot, I hope I'll be able to answer it.

&符号在这篇文章中有解释. (参考 NSInputStream 读取使用)

这篇关于像在 Java 中一样在 Swift 中使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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