简体   繁体   English

从 Java 服务器读取 Swift 中的 InputStream

[英]Read from InputStream in Swift from Java Server

as the title says, I'm trying to read from an InputStream in Swift.正如标题所说,我正在尝试从 Swift 中的 InputStream 中读取数据。 I'm new to Swift so I'm having some troubles regarding how to read from the InputStream a message sent from the Java Server.我是 Swift 的新手,所以我在如何从 InputStream 读取从 Java 服务器发送的消息时遇到了一些麻烦。

Java Server Code: Java 服务器代码:

byte[] toSend = sec.initiateDH;

out.writeInt(toSend.length);
out.write(toSend);

Swift Client Code 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")
   
}

Basically, I'm trying to initiate a DH key exchange.基本上,我正在尝试启动 DH 密钥交换。 The Swift client sends their public key, and in return, the server is supposed to send back theirs. Swift 客户端发送他们的公钥,作为回报,服务器应该发回他们的公钥。 It does send, but I'm having trouble reading from the InputStream.它确实发送,但我无法从 InputStream 读取。 First, the server sends the size and only then sends the byte array itself.首先,服务器发送大小,然后才发送字节数组本身。 Server uses DataOutputStream for writing.服务器使用 DataOutputStream 进行写入。

Anyone can help me with this?任何人都可以帮助我吗?

Thank you very much in advance!非常感谢您!

Where is your code, what u mean about??你的代码在哪里,你的意思是什么?

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() func: 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?
           
             
           }
       }
   }

don't forget to import Alamofire at the beginning of ur code.不要忘记在您的代码开头导入 Alamofire。 and the php part "infoinputstream.php" or what ever ur php code supposed to be something like..和 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); ?>

Hope it's help..希望有帮助。。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM