質問編集履歴
2
補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -241,3 +241,9 @@
|
|
241
241
|
調べたところポインタが初期化されていないと出てしまうエラーではないかと思うのですが、改善の方法がわかりません。
|
242
242
|
|
243
243
|
どなたかご教授願いたいです。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
補足
|
248
|
+
|
249
|
+
このアプリでは入力した任意の文字列をサーバー側に送るプログラムとなっていますが、もしも入力なしで固定の文字(例えば"1")を送る方法がありましたらそちらもご教授願いたいです。
|
1
ソースコードの挿入
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,6 +6,226 @@
|
|
6
6
|
|
7
7
|
上記のサイトを参考にさせていただき進めています。
|
8
8
|
|
9
|
+
|
10
|
+
|
11
|
+
```ここに言語を入力
|
12
|
+
|
13
|
+
import UIKit
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
class ViewController: UIViewController {
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
var Connection1 = Connection()
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
@IBOutlet weak var TB_SendCommand: UITextField!
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
@IBAction func BT_Connect(sendr: AnyObject){
|
30
|
+
|
31
|
+
Connection1.connect()
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
@IBAction func BT_Send(sendr: AnyObject){
|
38
|
+
|
39
|
+
//テキストの文字を取得してsendCommand()を行う
|
40
|
+
|
41
|
+
let sendtext = TB_SendCommand.text
|
42
|
+
|
43
|
+
if ((sendtext! as NSString).length > 0) {
|
44
|
+
|
45
|
+
Connection1.sendCommand(command: sendtext!)
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
@IBAction func BT_End(sendr: AnyObject){
|
54
|
+
|
55
|
+
Connection1.sendCommand(command: "end")
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
override func viewDidLoad() {
|
62
|
+
|
63
|
+
super.viewDidLoad()
|
64
|
+
|
65
|
+
// Do any additional setup after loading the view, typically from a nib.
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
override func didReceiveMemoryWarning() {
|
72
|
+
|
73
|
+
super.didReceiveMemoryWarning()
|
74
|
+
|
75
|
+
// Dispose of any resources that can be recreated.
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
class Connection: NSObject, StreamDelegate {
|
88
|
+
|
89
|
+
let ServerAddress: CFString = NSString(string: "xxx.xxx.xxx.xxx") //IPアドレスを指定
|
90
|
+
|
91
|
+
let serverPort: UInt32 = xxxx //開放するポートを指定
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
private var inputStream : InputStream!
|
96
|
+
|
97
|
+
private var outputStream: OutputStream!
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
//**
|
102
|
+
|
103
|
+
/* @brief サーバーとの接続を確立する
|
104
|
+
|
105
|
+
*/
|
106
|
+
|
107
|
+
func connect(){
|
108
|
+
|
109
|
+
print("connecting.....")
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
var readStream : Unmanaged<CFReadStream>?
|
114
|
+
|
115
|
+
var writeStream: Unmanaged<CFWriteStream>?
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
CFStreamCreatePairWithSocketToHost(nil, self.ServerAddress, self.serverPort, &readStream, &writeStream)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
self.inputStream = readStream!.takeRetainedValue()
|
124
|
+
|
125
|
+
self.outputStream = writeStream!.takeRetainedValue()
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
self.inputStream.delegate = self
|
130
|
+
|
131
|
+
self.outputStream.delegate = self
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
self.inputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
136
|
+
|
137
|
+
self.outputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
self.inputStream.open()
|
142
|
+
|
143
|
+
self.outputStream.open()
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
print("connect success!!")
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
//**
|
154
|
+
|
155
|
+
/* @brief inputStream/outputStreamに何かしらのイベントが起きたら起動してくれる関数
|
156
|
+
|
157
|
+
* 今回の場合では、同期型なのでoutputStreamの時しか起動してくれない
|
158
|
+
|
159
|
+
*/
|
160
|
+
|
161
|
+
func stream(_ stream:Stream, handle eventCode : Stream.Event){
|
162
|
+
|
163
|
+
//print(stream)
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
//**
|
170
|
+
|
171
|
+
/* @brief サーバーにコマンド文字列を送信する関数
|
172
|
+
|
173
|
+
*/
|
174
|
+
|
175
|
+
func sendCommand(command: String){
|
176
|
+
|
177
|
+
var ccommand = command.data(using: String.Encoding.utf8, allowLossyConversion: false)!
|
178
|
+
|
179
|
+
let text = ccommand.withUnsafeMutableBytes{ bytes in return String(bytesNoCopy: bytes, length: ccommand.count, encoding: String.Encoding.utf8, freeWhenDone: false)!}
|
180
|
+
|
181
|
+
self.outputStream.write(UnsafePointer(text), maxLength: text.utf8.count)
|
182
|
+
|
183
|
+
print("Send: (command)")
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
//"end"を受信したら接続切断
|
188
|
+
|
189
|
+
if (String(describing: command) == "end") {
|
190
|
+
|
191
|
+
self.outputStream.close()
|
192
|
+
|
193
|
+
self.outputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
while(!inputStream.hasBytesAvailable){}
|
198
|
+
|
199
|
+
let bufferSize = 1024
|
200
|
+
|
201
|
+
var buffer = Array<UInt8>(repeating: 0, count: bufferSize)
|
202
|
+
|
203
|
+
let bytesRead = inputStream.read(&buffer, maxLength: bufferSize)
|
204
|
+
|
205
|
+
if (bytesRead >= 0) {
|
206
|
+
|
207
|
+
let read = String(bytes: buffer, encoding: String.Encoding.utf8)!
|
208
|
+
|
209
|
+
print("Receive: (read)")
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
self.inputStream.close()
|
214
|
+
|
215
|
+
self.inputStream.remove(from: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
```
|
226
|
+
|
227
|
+
|
228
|
+
|
9
229
|
しかし、
|
10
230
|
|
11
231
|
|