Giter Site home page Giter Site logo

biatoms / socket.swift Goto Github PK

View Code? Open in Web Editor NEW
64.0 7.0 15.0 68 KB

A POSIX socket wrapper written in swift.

License: MIT License

Swift 95.93% Ruby 2.34% Objective-C 1.73%
posix posix-compliant socket wrapper swift posix-socket-wrapper linux tvos macos cocoapods

socket.swift's Introduction

Platform Linux Platform iOS macOS tvOS Cocoapods Compatible Carthage Compatible License Build Status - Master

Socket.swift

A POSIX socket wrapper written in swift.

Features

  • TLS/SSL support
  • Linux, iOS, macOS and tvOS support
  • Clean and understanable code

If you consider something needs to be implemented, just open an issue or make a PR

Example

let server = try Socket(.inet, type: .stream, protocol: .tcp) // create server socket
try server.set(option: .reuseAddress, true) // set SO_REUSEADDR to 1
try server.bind(port: 8090, address: nil) // bind 'localhost:8090' address to the socket
try server.listen() // allow incoming connections

let client = try Socket(.inet, type: .stream, protocol: .tcp) // create client socket
try client.connect(port: 8090) // connect to localhost:8090

let clientAtServerside = try server.accept() // accept client connection
 
let helloBytes = ([UInt8])("Hello World".utf8)
try clientAtServerside.write(helloBytes) // sending bytes to the client
clientAtServerside.close()

var buffer = [UInt8](repeating: 0, count: helloBytes.count) // allocate buffer
let numberOfReadBytes = try client.read(&buffer, size: helloBytes.count)
print(numberOfReadBytes == helloBytes.count) // true
print(buffer == helloBytes) // true

client.close()
server.close()

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Socket.swift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
target '<Your Target Name>' do
pod 'Socket.swift', '~> 2.4.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Socket.swift into your Xcode project using Carthage, specify it in your Cartfile:

github "BiAtoms/Socket.swift" ~> 2.4.0

Run carthage update to build the framework and drag the built SocketSwift.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but Socket.swift does support its use on supported platforms.

Once you have your Swift package set up, adding Socket.swift as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/BiAtoms/Socket.swift.git", from: "2.4.0")
]

Manually

Just drag and drop the files in the Sources folder.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

socket.swift's People

Contributors

orkhanalikhanov avatar raymondjavaxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

socket.swift's Issues

Generating the TLS certificate at runtime

Hi, Thank you very much for the libraries Socket.swift and HTTP.swift.

I need to generate the certificate used by the HTTP server (and thus the TLS.swift file used in Socket.swift) at runtime. That is to say I need to generate a new certificate everytime the app is launched and I start up the server.

Do you know of any way that this can be achieved on iOS?

I have searched far and wide and it seems most people end up shipping a copy of OpenSSL in their app to do this. I am guessing that if I ended up doing this, then I would need export compliance and sign off from the US government / Apple to do this.

SocketError 4: Interrupted system call

Until now it worked perfectly for me, but since the iOS 15.4 version I can't connect to the TCP server.

The error is: SocketError 4: Interrupted system call

(If I try with an iPhone with iOS 15.2, it works)

cellular radio activitation

apple documentation reads that POSIX sockets will not activate the cellular radio... but I read things on stackoverflow saying it does (users had succesfully accomplished it)... then another user said the radio died after a little bit, with others being surprised.

do you have any final word on this, given this library exists? thanks!

use it in app extension failed!

Extension com.ribencong.blockchainPipe.TunProxy died unexpectedly
[TunProxy:75295] SyscallError: setpriority(PRIO_DARWIN_ROLE, 75295, 3): No such process
[TunProxy:75295] Remove assertion: <BKProcessAssertion: 0x100d7d6c0; "com.apple.extension.session" (extension:inf); id:โ€ฆ85104422BD2F>
-CMSessionMgr- CMSessionMgrHandleApplicationStateChange: CMSession: Client com.ribencong.blockchainPipe.TunProxy with pid '75295' is now Background Running. Background entitlement: NO LongFormVideoApp: NO
1177: pid 75295(TunProxy)

Getting the remote socket address

Thanks for this great package. I'm using this on Linux.

What's the best way to get the remote socket address after the accept returns?

cannot compile in Ubuntu 16.04

Hello,

the project seem to not be able to compile in ubuntu 16.04, using Swift 4.1
libressl cannot be installed.
swift build report several error around CLibreSSL

TestFlight Upload Failure with Xcode 15 & Fastlane Due to Missing CFBundleVersion in SocketSwift.xcframework

Description

I've encountered an upload issue to TestFlight using Fastlane's pilot tool in combination with Xcode 15. The error occurs specifically with the SocketSwift.xcframework included in my application. During the upload process, asset validation fails with an error indicating that the Info.plist within SocketSwift.xcframework is missing the required CFBundleVersion key.

Detailed Error Output

Asset validation failed (90056) This bundle Payload/<my-app.app>/Frameworks/SocketSwift.framework is invalid. 
The Info.plist file is missing the required key: CFBundleVersion. 
Please find more information about CFBundleVersion at 
https://developer.apple.com/documentation/bundleresources/information_property_list/cfbundleversion (ID: 5f353f23-4add-481b-8e2c-2c443a55dc26)

Listen for client message

Hi and thanks for your great job.
I have a nodejs client which sends messages to the server part. I use your work to create the server part. I need to listen incoming messages in my swift app. With delegate? Thanks a lot in advance!

Getting connection error using swift 4

Hello,

I implemented the code in my demo project but it seems that I am making some mistake and hence I am getting the connection error saying that connection refused. I tried with the port and the IP address. Could you please suggest me how can I solve this? Below I am sending you the code which I have implemented.

Client Side:
do {
let client = try Socket(.inet, type: .stream, protocol: .tcp) // create client socket
do {
try client.connect(port: 8080, address: "192.168.1.50")
}
catch {
print(error)
}
var buffer = [UInt8](repeating: 0, count: helloBytes.count) // allocate buffer
let numberOfReadBytes = try client.read(&buffer, size: helloBytes.count)
print(numberOfReadBytes == helloBytes.count) // true
print(buffer == helloBytes) // true
client.close()
}
catch {
print(error)
}
Server Side
do {
let server = try Socket(.inet, type: .stream, protocol: .tcp) // create server socket
do {
try server.set(option: .reuseAddress, true) // set SO_REUSEADDR to 1
}
catch {
print(error)
}

        do {
            try server.bind(port: 8080, address:"192.168.1.88" ) // bind 'localhost:8090' address to the socket
        }
        catch {
            print(error)
        }
        
        do {
            try server.listen() // allow incoming connections
        }
        catch {
            print(error)
        }

// server.close()
let clientAtServerside = try server.accept() // accept client connection
try clientAtServerside.write(helloBytes) // sending bytes to the client
clientAtServerside.close()
}
catch {
print(error)
}

Please kindly provide the solution for this.

Ubuntu 16.04 does not build

i get a warning if i try to build my project

warning: you may be able to install libtls using your system-packager:
     apt-get install libressl

installing apt-get install libressl does not find the package

errors on building
.build/checkouts/CLibreSSL/clibressl.h:4:10: error: 'tls.h' file not found
.build/checkouts/Socket.swift/Sources/TLS.swift:12:12: error: could not build C module 'CLibreSSL'

not getting adress?

how to take socket connected to server socket adress?

func echoService(client: TCPClient) {
    print("Newclient from:\(client.address)[\(client.port)]")    // I need that Like code ! 
    var d = client.read(1024*10)
    client.send(data: d!)
    client.close()
}

func testServer() {
    let server = TCPServer(address: "127.0.0.1", port: 8080)
    switch server.listen() {
      case .success:
        while true {
            if var client = server.accept() {
                echoService(client: client)
            } else {
                print("accept error")
            }
        }
      case .failure(let error):
        print(error)
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.