私はswift4でのソケット通信を実現させようとしています。
そこで
https://qiita.com/sensuikan1973/items/76c0bd861564859ebe4c
上記のサイトを参考にさせていただき進めています。
import UIKit class ViewController: UIViewController { var Connection1 = Connection() @IBOutlet weak var TB_SendCommand: UITextField! @IBAction func BT_Connect(sendr: AnyObject){ Connection1.connect() } @IBAction func BT_Send(sendr: AnyObject){ //テキストの文字を取得してsendCommand()を行う let sendtext = TB_SendCommand.text if ((sendtext! as NSString).length > 0) { Connection1.sendCommand(command: sendtext!) } } @IBAction func BT_End(sendr: AnyObject){ Connection1.sendCommand(command: "end") } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } class Connection: NSObject, StreamDelegate { let ServerAddress: CFString = NSString(string: "xxx.xxx.xxx.xxx") //IPアドレスを指定 let serverPort: UInt32 = xxxx //開放するポートを指定 private var inputStream : InputStream! private var outputStream: OutputStream! //** /* @brief サーバーとの接続を確立する */ func connect(){ print("connecting.....") var readStream : Unmanaged<CFReadStream>? var writeStream: Unmanaged<CFWriteStream>? CFStreamCreatePairWithSocketToHost(nil, self.ServerAddress, self.serverPort, &readStream, &writeStream) self.inputStream = readStream!.takeRetainedValue() self.outputStream = writeStream!.takeRetainedValue() self.inputStream.delegate = self self.outputStream.delegate = self self.inputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode) self.outputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode) self.inputStream.open() self.outputStream.open() print("connect success!!") } //** /* @brief inputStream/outputStreamに何かしらのイベントが起きたら起動してくれる関数 * 今回の場合では、同期型なのでoutputStreamの時しか起動してくれない */ func stream(_ stream:Stream, handle eventCode : Stream.Event){ //print(stream) } //** /* @brief サーバーにコマンド文字列を送信する関数 */ func sendCommand(command: String){ var ccommand = command.data(using: String.Encoding.utf8, allowLossyConversion: false)! let text = ccommand.withUnsafeMutableBytes{ bytes in return String(bytesNoCopy: bytes, length: ccommand.count, encoding: String.Encoding.utf8, freeWhenDone: false)!} self.outputStream.write(UnsafePointer(text), maxLength: text.utf8.count) print("Send: (command)") //"end"を受信したら接続切断 if (String(describing: command) == "end") { self.outputStream.close() self.outputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode) while(!inputStream.hasBytesAvailable){} let bufferSize = 1024 var buffer = Array<UInt8>(repeating: 0, count: bufferSize) let bytesRead = inputStream.read(&buffer, maxLength: bufferSize) if (bytesRead >= 0) { let read = String(bytes: buffer, encoding: String.Encoding.utf8)! print("Receive: (read)") } self.inputStream.close() self.inputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode) } } }
しかし、
RangeText="self.outputStream.write(UnsafePointer(text), maxLength: text.utf8.count)"
error: Segmentation fault: 11
このようなエラーが出てしまい、直せなくて困っています。
調べたところポインタが初期化されていないと出てしまうエラーではないかと思うのですが、改善の方法がわかりません。
どなたかご教授願いたいです。
補足
このアプリでは入力した任意の文字列をサーバー側に送るプログラムとなっていますが、もしも入力なしで固定の文字(例えば"1")を送る方法がありましたらそちらもご教授願いたいです。