質問編集履歴
2
補足
title
CHANGED
File without changes
|
body
CHANGED
@@ -119,4 +119,7 @@
|
|
119
119
|
|
120
120
|
このようなエラーが出てしまい、直せなくて困っています。
|
121
121
|
調べたところポインタが初期化されていないと出てしまうエラーではないかと思うのですが、改善の方法がわかりません。
|
122
|
-
どなたかご教授願いたいです。
|
122
|
+
どなたかご教授願いたいです。
|
123
|
+
|
124
|
+
補足
|
125
|
+
このアプリでは入力した任意の文字列をサーバー側に送るプログラムとなっていますが、もしも入力なしで固定の文字(例えば"1")を送る方法がありましたらそちらもご教授願いたいです。
|
1
ソースコードの挿入
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,6 +2,116 @@
|
|
2
2
|
そこで
|
3
3
|
[https://qiita.com/sensuikan1973/items/76c0bd861564859ebe4c](https://qiita.com/sensuikan1973/items/76c0bd861564859ebe4c)
|
4
4
|
上記のサイトを参考にさせていただき進めています。
|
5
|
+
|
6
|
+
```ここに言語を入力
|
7
|
+
import UIKit
|
8
|
+
|
9
|
+
class ViewController: UIViewController {
|
10
|
+
|
11
|
+
var Connection1 = Connection()
|
12
|
+
|
13
|
+
@IBOutlet weak var TB_SendCommand: UITextField!
|
14
|
+
|
15
|
+
@IBAction func BT_Connect(sendr: AnyObject){
|
16
|
+
Connection1.connect()
|
17
|
+
}
|
18
|
+
|
19
|
+
@IBAction func BT_Send(sendr: AnyObject){
|
20
|
+
//テキストの文字を取得してsendCommand()を行う
|
21
|
+
let sendtext = TB_SendCommand.text
|
22
|
+
if ((sendtext! as NSString).length > 0) {
|
23
|
+
Connection1.sendCommand(command: sendtext!)
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
@IBAction func BT_End(sendr: AnyObject){
|
28
|
+
Connection1.sendCommand(command: "end")
|
29
|
+
}
|
30
|
+
|
31
|
+
override func viewDidLoad() {
|
32
|
+
super.viewDidLoad()
|
33
|
+
// Do any additional setup after loading the view, typically from a nib.
|
34
|
+
}
|
35
|
+
|
36
|
+
override func didReceiveMemoryWarning() {
|
37
|
+
super.didReceiveMemoryWarning()
|
38
|
+
// Dispose of any resources that can be recreated.
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
}
|
43
|
+
|
44
|
+
class Connection: NSObject, StreamDelegate {
|
45
|
+
let ServerAddress: CFString = NSString(string: "xxx.xxx.xxx.xxx") //IPアドレスを指定
|
46
|
+
let serverPort: UInt32 = xxxx //開放するポートを指定
|
47
|
+
|
48
|
+
private var inputStream : InputStream!
|
49
|
+
private var outputStream: OutputStream!
|
50
|
+
|
51
|
+
//**
|
52
|
+
/* @brief サーバーとの接続を確立する
|
53
|
+
*/
|
54
|
+
func connect(){
|
55
|
+
print("connecting.....")
|
56
|
+
|
57
|
+
var readStream : Unmanaged<CFReadStream>?
|
58
|
+
var writeStream: Unmanaged<CFWriteStream>?
|
59
|
+
|
60
|
+
CFStreamCreatePairWithSocketToHost(nil, self.ServerAddress, self.serverPort, &readStream, &writeStream)
|
61
|
+
|
62
|
+
self.inputStream = readStream!.takeRetainedValue()
|
63
|
+
self.outputStream = writeStream!.takeRetainedValue()
|
64
|
+
|
65
|
+
self.inputStream.delegate = self
|
66
|
+
self.outputStream.delegate = self
|
67
|
+
|
68
|
+
self.inputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
69
|
+
self.outputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
70
|
+
|
71
|
+
self.inputStream.open()
|
72
|
+
self.outputStream.open()
|
73
|
+
|
74
|
+
print("connect success!!")
|
75
|
+
}
|
76
|
+
|
77
|
+
//**
|
78
|
+
/* @brief inputStream/outputStreamに何かしらのイベントが起きたら起動してくれる関数
|
79
|
+
* 今回の場合では、同期型なのでoutputStreamの時しか起動してくれない
|
80
|
+
*/
|
81
|
+
func stream(_ stream:Stream, handle eventCode : Stream.Event){
|
82
|
+
//print(stream)
|
83
|
+
}
|
84
|
+
|
85
|
+
//**
|
86
|
+
/* @brief サーバーにコマンド文字列を送信する関数
|
87
|
+
*/
|
88
|
+
func sendCommand(command: String){
|
89
|
+
var ccommand = command.data(using: String.Encoding.utf8, allowLossyConversion: false)!
|
90
|
+
let text = ccommand.withUnsafeMutableBytes{ bytes in return String(bytesNoCopy: bytes, length: ccommand.count, encoding: String.Encoding.utf8, freeWhenDone: false)!}
|
91
|
+
self.outputStream.write(UnsafePointer(text), maxLength: text.utf8.count)
|
92
|
+
print("Send: (command)")
|
93
|
+
|
94
|
+
//"end"を受信したら接続切断
|
95
|
+
if (String(describing: command) == "end") {
|
96
|
+
self.outputStream.close()
|
97
|
+
self.outputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
98
|
+
|
99
|
+
while(!inputStream.hasBytesAvailable){}
|
100
|
+
let bufferSize = 1024
|
101
|
+
var buffer = Array<UInt8>(repeating: 0, count: bufferSize)
|
102
|
+
let bytesRead = inputStream.read(&buffer, maxLength: bufferSize)
|
103
|
+
if (bytesRead >= 0) {
|
104
|
+
let read = String(bytes: buffer, encoding: String.Encoding.utf8)!
|
105
|
+
print("Receive: (read)")
|
106
|
+
}
|
107
|
+
self.inputStream.close()
|
108
|
+
self.inputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
}
|
113
|
+
```
|
114
|
+
|
5
115
|
しかし、
|
6
116
|
|
7
117
|
RangeText="self.outputStream.write(UnsafePointer(text), maxLength: text.utf8.count)"
|