[英]Read from InputStream in Swift from Java Server
正如标题所说,我正在尝试从 Swift 中的 InputStream 中读取数据。 我是 Swift 的新手,所以我在如何从 InputStream 读取从 Java 服务器发送的消息时遇到了一些麻烦。
Java 服务器代码:
byte[] toSend = sec.initiateDH;
out.writeInt(toSend.length);
out.write(toSend);
Swift 客户端代码
init(_ ipAddress: String, _ port: Int, _ textEncoding: String.Encoding) {
super.init()
Stream.getStreamsToHost(withName: ipAddress, port: port, inputStream: &inp, outputStream: &out)
inp!.open()
out!.open()
print("Connection Established")
}
基本上,我正在尝试启动 DH 密钥交换。 Swift 客户端发送他们的公钥,作为回报,服务器应该发回他们的公钥。 它确实发送,但我无法从 InputStream 读取。 首先,服务器发送大小,然后才发送字节数组本身。 服务器使用 DataOutputStream 进行写入。
任何人都可以帮助我吗?
非常感谢您!
你的代码在哪里,你的意思是什么?
Well, u not send to mach information about what ur problem but I suppose that you want read an php file from sever so there is tow part: part one reading the php file in the swift part you need to add this code in viewDidload()功能:
let URL_USER_data = "https://www.ursite.net/infoinputstream.php"
override func viewDidLoad() {
super.viewDidLoad()
//Sending http post request
Alamofire.request(URL_USER_data , method: .get).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
//converting it as NSDictionary
let jsonData = result as! NSDictionary
//displaying the message in label
self.bytetext.text = jsonData.value(forKey: "byte") as! String?
self.sizetext.text = jsonData.value(forKey: "size") as! String?
}
}
}
不要忘记在您的代码开头导入 Alamofire。 和 php 部分“infoinputstream.php”或者你的 php 代码应该是什么。
<?php require_once 'connect.php'; $query1="SELECT* FROM data"; $result1 = mysqli_query($conn, $query1); while( $row1 = mysqli_fetch_assoc($result1) ){ $$response=$row1['byte']; $$response=$row1['size']; } echo json_encode($response); ?>
希望有帮助。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.