Swift Network.framework Study 2019 1207 "Server TCP" Part 3 Use of receive method Improvement

Study

Network.framework Study: Server side

environment

Client:Java、NetBeans Server:Swift、Xcode

Client Source Java

Fixed to send multiple times.

package example.java.network;

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

public class ExampleClientSocket {
    public static void main(String[] args) {
        try(Socket socket = new Socket("localhost", 7777);
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);) {
            for(int i = 0; i < 5; i++) {
                printWriter.println("Example Send Data");
                Thread.sleep(5000);
            }
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}

Server Source Swift

Use NWLister Receive processing registration and start execution in NWConnection after connection establishment Use the receive method that can specify the number of received bytes. The maximum length of several minutes was acquired from one received data. In the example below, it is 5 bytes. Even if you send 5 bytes or more, it seems that reception is not called continuously after the 6th byte. You need to call the receive yourself. Call the receive method until reception is complete. Call the receive method when the received data of completion is other than nil and the completion flag is false.

main.swift

import Foundation
import Network

var networkServer = NetworkServer()

networkServer.startListener()

while networkServer.running {
    sleep(1)
}

NetworkServer.swift

import Foundation
import Network

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

Recommended Posts

Swift Network.framework Study 2019 1207 "Server TCP" Part 3 Use of receive method Improvement
Swift Network.framework Study 2019 1206 "Server TCP" Part 2 Use receive method
Swift Network.framework Study 2019 1205 "Server TCP"
Swift Network.framework Study 2019 1207 "Server UDP"
Swift Network.framework Study 2019 1208 "Send and Receive"
Swift Network.framework Study 20200319 "Receive and Send"
Swift Network.framework Study 20191203
study of dbpedia part 25
Swift Network.framework Study 2019 1204 "UDP"
Swift Network.framework Study 2019 1210 "Bonjour Browse"