Swift 5.1 version of code from https://github.com/ZewoGraveyard/TCPIP
TCPIP is a TCP/IP library for Swift 5.1.
- No
Foundationdependency (Linux ready) - Local IP
- Remote IP
- TCP Server Socket
- TCP Client Socket
import TCPIP// local
do {
// all network interfaces
let ip1 = try IP(port: 5555, mode: .IPV4)
// specific network interface
let ip2 = try IP(networkInterface: "en0", port: 5555, mode: .IPV6)
} catch {
// something bad happened :(
}
// remote
do {
let ip3 = try IP(address: "127.0.0.1", port: 5555, mode: .IPV4)
} catch {
// something bad happened :(
}// server
do {
let ip = try IP(port: 6379) // Connecting to local IP.
let serverSocket = try TCPServerSocket(ip: ip)
let clientSocket = try serverSocket.accept()
let clientMessage = try clientSocket?.receiveString(untilDelimiter: "\n")
print("Client Message - \(clientMessage ?? "")")
} catch {
// something bad happened :(
}
// client
do {
let ip = try IP(address: "127.0.0.1", port: 5555)
let clientSocket = try TCPClientSocket(ip: ip)
// calls to send append the data to an internal
// buffer to minimize system calls
try clientSocket.sendString("yo\n")
// flush actually sends all data in the buffer
try clientSocket.flush()
} catch {
// something bad happened :(
}import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/PushpakN/-TCPIP.git")
]
)To use TCPIP in a command line application:
- Install the Swift Command Line Application Xcode template
TCPIP is released under the MIT license. See LICENSE for details.