[英]Connect with a Swift client to a Java server using sockets
How do I connect a Swift client to a Java server using sockets? 如何使用套接字将Swift客户端连接到Java服务器? How can I send different data types between the client and the server?
如何在客户端和服务器之间发送不同的数据类型?
ServerSocket serverSocket = new ServerSocket(12345);
while(true) {
Socket socket = serverSocket.accept();
System.out.println(new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine());
}
You can use the SocketRocket ObjC framework bridged to Swift 您可以使用桥接到Swift的SocketRocket ObjC框架
https://github.com/square/SocketRocket https://github.com/square/SocketRocket
Then you just create an instance of the class and use it in Swift, ie: 然后,您只需创建该类的实例并在Swift中使用它,即:
var socket = SRWebSocket(...)
socket.open()
socket.send()
socket.close()
Example: 例:
You create a NSURLRequest: 您创建一个NSURLRequest:
var urlRequest = NSURLRequest(URL: NSURL(string: "http://host.com"))
then init the socket with it 然后用它初始化套接字
socket = SRWebSocket(URLRequest: urlRequest)
No need to link the framework, just import the files 无需链接框架,只需导入文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.