Study
Network.framework
Study:Server側
環境
Client:Java、NetBeans
Server:Swift、Xcode
#Client Source Java
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);) {
printWriter.println("Example Send Data");
}
catch(Exception e) {
System.out.println(e);
}
}
}
Server Source Swift
NWListerを使用する
コネクション確立後のNWConnectionに受信処理登録、開始実施
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.receiveMessage(completion: { (data, context, flag, error) in
print("receiveMessage")
let receiveData = [UInt8](data!)
print(receiveData)
})
newConnection.start(queue: myQueue)
}
nWListener.start(queue: myQueue)
print("start")
}
catch {
print(error)
}
}
}