[JAVA] Etude Swift Network.framework 20200319 "Recevoir et envoyer"

Study

Network.framework Study:Receive and Send

environnement

Client:Java、NetBeans Server:Swift、Xcode

Server Source Swift

main.swift

import Foundation
import Network

var receiveANdSend = ReceiveAndSend()
receiveANdSend.startListener()
while receiveANdSend.running {
    sleep(1)
}

ReceiveAndSend.swift

import Foundation
import Network

class ReceiveAndSend {
    public var running = true

    func startListener() {
        let myQueue = DispatchQueue(label: "ExampleNetwork")

        do {
            let nWListener = try NWListener(using: .tcp, on: 7777)
            nWListener.newConnectionHandler = { (newConnection) in
                print("New Connection!!")
                newConnection.start(queue: myQueue)
                self.receive(nWConnection: newConnection)
            }
            nWListener.start(queue: myQueue)
            print("start")
        }
        catch {
            print(error)
        }
    }
    
    func receive(nWConnection:NWConnection) {
        nWConnection.receive(minimumIncompleteLength: 1, maximumLength: 5, completion: { (data, context, flag, error) in
            print("receiveMessage")
            if let data = data {
                let receiveData = [UInt8](data)
                print(receiveData)
                print(flag)
                self.sendMessage(nWConnection)
                if(flag == false) {
                    self.receive(nWConnection: nWConnection)
                }
            }
            else {
                print("receiveMessage data nil")
            }
        })
    }

    func sendMessage(_ connection: NWConnection) {
        let data = "Answer".data(using: .utf8)
        let completion = NWConnection.SendCompletion.contentProcessed { (error: NWError?) in
            print("Transmission de la réponse terminée")
            self.running = false
        }
        connection.send(content: data, completion: completion)
    }
}

Client Source Java

public class ExampleSendAndReceive {
   public static void main(String[] args) {
        try(Socket socket = new Socket("localhost", 7777);
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);) {

            Runnable runnable = () -> {
                System.out.println("Receive start");
                try {
                    int data = socket.getInputStream().read();
                    while(data != -1) {
                        System.out.print(data + " ");
                        data = socket.getInputStream().read();
                    }
                    System.out.println("end");
                }
                catch(Exception e) {
                    System.out.println(e);
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();
            
            for(int i = 0; i < 5; i++) {
                printWriter.println("12345");
                Thread.sleep(5000);
            }
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }    
}

Recommended Posts

Étude Swift Network.framework 2019 1208 «Envoyer et recevoir»
Etude Swift Network.framework 20200319 "Recevoir et envoyer"
Étude Swift Network.framework 20191203
Étude Swift Network.framework 2019 1204 «UDP»
Étude Swift Network.framework 2019 1210 "Bonjour Parcourir"
Étude Swift Network.framework 2019 1205 «Serveur TCP»
Étude Swift Network.framework 2019 1206 «Serveur TCP» Partie 2 Utiliser la méthode de réception
Swift Network.framework Study 2019 1207 «Server TCP» Partie 3 Amélioration de l'utilisation de la méthode de réception
Connectez le port en avant avec SSH pour envoyer et recevoir des fichiers
Créez une API pour envoyer et recevoir des données Json avec Spring
Etudier Java # 2 (\ marque et opérateur)
Utilisez un filtre et une carte rapides
Étude Swift Network.framework 2019 1204 «UDP»
Étude Swift Network.framework 2019 1205 «Serveur TCP»
Étude Swift Network.framework 20191203
Étude Swift Network.framework 2019 1210 "Bonjour Parcourir"
Étude Swift Network.framework 2019 1206 «Serveur TCP» Partie 2 Utiliser la méthode de réception
Étude Swift Network.framework 2019 1208 «Envoyer et recevoir»
Etude Swift Network.framework 20200319 "Recevoir et envoyer"
Swift Network.framework Study 2019 1207 «Server TCP» Partie 3 Amélioration de l'utilisation de la méthode de réception