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

查看:79
本文介绍了在Java中像在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.

原始帖子中描述的问题是获取Input/Output流并使它们工作. (本文结尾处引用了我的另一篇文章,解释了如何使用这些流)

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 的静态方法(快速的类函数). 您需要准备的是使用实际主机地址,端口号,对NSInputStream obj的引用以及对NSOutputStream obj进行初始化的NSHost对象. 然后,您可以使用此处显示的流,并在参考文章中进行解释a>.

The NSStream class has a static method (class function in swift) called getStreamsToHost. All you have to prepare is a NSHost objected initialized with a real host address, a port number, a reference to a NSInputStream obj and also for a NSOutputStream obj. Then, you can use the streams as shown here and explained in the reference post.

看看这个简单的代码;

look at this simple code;

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$ 

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

After launching the code, you can see that I have received the string "Jonathan Yaniv.\n" 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中像在Java中一样在Swift中使用套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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