[JAVA] Étude Swift Network.framework 2019 1208 «Envoyer et recevoir»

Study

Network.framework Study:Send and Receive

environnement

Client:Swift、Xcode Server:Java、NetBeans

Client Source Swift

main.swift

import Foundation
import Network

var sendAndReceive = SendAndReceive(host: "localhost", port: 7777, nWParameters: .tcp)

sendAndReceive.startConnection()

while sendAndReceive.running {
    sleep(1)
}

SendAndReceive.swift

import Foundation
import Network

class SendAndReceive {
    public var running = true
    private var host:NWEndpoint.Host
    private var port:NWEndpoint.Port
    private var nWParameters: NWParameters
    
    init(host:NWEndpoint.Host, port:NWEndpoint.Port, nWParameters: NWParameters) {
        self.host = host
        self.port = port
        self.nWParameters = nWParameters
    }

    func startConnection() {
        let myQueue = DispatchQueue(label: "ExampleNetwork")
        let connection = NWConnection(host: host, port: port, using: nWParameters)
        connection.stateUpdateHandler = { (newState) in
            switch(newState) {
            case .ready:
                print("ready")
                self.sendMessage(connection)
            case .waiting(let error):
                print("waiting")
                print(error)
            case .failed(let error):
                print("failed")
                print(error)
            default:
                print("defaults")
                break
            }
        }
        connection.start(queue: myQueue)
        self.receive(nWConnection: connection)
    }

    func sendMessage(_ connection: NWConnection) {
        let data = "Example Send Data".data(using: .utf8)
        let completion = NWConnection.SendCompletion.contentProcessed { (error: NWError?) in
            print("envoyer complètement")
        }
        connection.send(content: data, completion: completion)
    }

    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)
                if(flag == false) {
                    self.receive(nWConnection: nWConnection)
                }
            }
            else {
                print("receiveMessage data nil")
            }
        })
    }

}

Server Source Java

package example.java.network;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ExampleReceiveAndSend {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(7777);
            System.out.println("socket create");
            Socket socket = serverSocket.accept();
            System.out.println("accept");
            byte[] data = new byte[1024];
            int dataSize = socket.getInputStream().read(data);
            System.out.println(data);
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
            printWriter.println("Example Return Data");
            
            System.out.println("end");            
        }
        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 1207 «Serveur UDP»
É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