Skip to content

koliyo/swift-unisocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

macOS Linux Apache 2 Build Status

UniSocket

Let your swift application talk to others via TCP, UDP or unix sockets.

Usage

Check if there is sshd running on the system:

import UniSocket

do {
	let socket = try UniSocket(type: .tcp, peer: "localhost", port: 22)
	try socket.attach()
	let data = try socket.recv()
	let string = String(data: data, encoding: .utf8)
	print("server responded with:")
	print(string)
	try socket.close()
} catch UniSocketError.error(let detail) {
	print(detail)
}

Send HTTP request and wait for response of a minimal required size:

import UniSocket

do {
	let socket = try UniSocket(type: .tcp, peer: "2a02:598:2::1053", port: 80)
	try socket.attach()
	let request = "HEAD / HTTP/1.0\r\n\r\n"
	let dataOut = request.data(using: .utf8)
	try socket.send(dataOut!)
	let dataIn = try socket.recv(min: 16)
	let string = String(data: dataIn, encoding: .utf8)
	print("server responded with:")
	print(string)
	try socket.close()
} catch UniSocketError.error(let detail) {
	print(detail)
}

Query DNS server over UDP using custom timeout values:

import UniSocket
import DNS // https://github.com/Bouke/DNS

do {
	let timeout: UniSocketTimeout = (connect: 2, read: 2, write: 1)
	let socket = try UniSocket(type: .udp, peer: "8.8.8.8", port: 53, timeout: timeout)
	try socket.attach() // NOTE: due to .udp, the call doesn't make a connection, just prepares socket and resolves hostname
	let request = Message(type: .query, recursionDesired: true, questions: [Question(name: "www.apple.com.", type: .host)])
	let requestData = try request.serialize()
	try socket.send(requestData)
	let responseData = try socket.recv()
	let response = try Message.init(deserialize: responseData)
	print("server responded with:")
	print(response)
	try socket.close()
} catch UniSocketError.error(let detail) {
	print(detail)
}

Check if local MySQL server is running:

import UniSocket

do {
	let socket = try UniSocket(type: .local, peer: "/tmp/mysql.sock")
	try socket.attach()
	let data = try socket.recv()
	print("server responded with:")
	print("\(data.map { String(format: "%c", $0) }.joined())")
	try socket.close()
} catch UniSocketError.error(let detail) {
	print(detail)
}

Credits

Written by Daniel Fojt, copyright Seznam.cz, licensed under the terms of the Apache License 2.0.

About

Sockets for Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%