[JAVA] Swift Network.framework Study 20200319 "Receive and Send"

Study

Network.framework Study:Receive and Send

environment

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("Response transmission completed")
            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

Swift Network.framework Study 2019 1208 "Send and Receive"
Swift Network.framework Study 20200319 "Receive and Send"
Swift Network.framework Study 20191203
Swift Network.framework Study 2019 1204 "UDP"
Swift Network.framework Study 2019 1210 "Bonjour Browse"
Swift Network.framework Study 2019 1205 "Server TCP"
Swift Network.framework Study 2019 1206 "Server TCP" Part 2 Use receive method
Swift Network.framework Study 2019 1207 "Server TCP" Part 3 Use of receive method Improvement
Connect with port forwarding with SSH and send and receive files
[Swift] Constants and variables
Create API to send and receive Json data in Spring
Study Java # 2 (\ mark and operator)
Use swift Filter and Map
Swift Network.framework Study 2019 1204 "UDP"
Swift Network.framework Study 2019 1205 "Server TCP"
Swift Network.framework Study 20191203
Swift Network.framework Study 2019 1210 "Bonjour Browse"
Swift Network.framework Study 2019 1206 "Server TCP" Part 2 Use receive method
Swift Network.framework Study 2019 1208 "Send and Receive"
Swift Network.framework Study 20200319 "Receive and Send"
Swift Network.framework Study 2019 1207 "Server TCP" Part 3 Use of receive method Improvement