├── README.md ├── SocketConnectionTest.xcodeproj └── project.pbxproj ├── SocketConnectionTest ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── Model │ └── DataSocket.swift ├── PresenterProtocol.swift ├── SocketDataManager.swift └── ViewController.swift └── SocketConnectionTestTests ├── Info.plist └── SocketConnectionTestTests.swift /README.md: -------------------------------------------------------------------------------- 1 | # SocketSwiftSample 2 | Sample application to create socket connection and send/ receive data. I am using https://packetsender.com/ for testing the same. 3 | 4 | PS: Not much error handling done, this is my trial. 5 | 6 | ![socketvideo](https://user-images.githubusercontent.com/6782228/38165408-65aef524-3530-11e8-9302-afe4fbf3add7.gif) 7 | 8 | 9 | Packet sender has a quick time out, so please make sure the `Persistent TCP` box is checked 10 | 11 | screenshot 2019-02-14 at 6 46 58 pm 12 | -------------------------------------------------------------------------------- /SocketConnectionTest.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | EA6C50EF206E3DC200FF70D4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C50EE206E3DC200FF70D4 /* AppDelegate.swift */; }; 11 | EA6C50F1206E3DC200FF70D4 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C50F0206E3DC200FF70D4 /* ViewController.swift */; }; 12 | EA6C50F4206E3DC200FF70D4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EA6C50F2206E3DC200FF70D4 /* Main.storyboard */; }; 13 | EA6C50F6206E3DC200FF70D4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = EA6C50F5206E3DC200FF70D4 /* Assets.xcassets */; }; 14 | EA6C50F9206E3DC200FF70D4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EA6C50F7206E3DC200FF70D4 /* LaunchScreen.storyboard */; }; 15 | EA6C5104206E3DC200FF70D4 /* SocketConnectionTestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C5103206E3DC200FF70D4 /* SocketConnectionTestTests.swift */; }; 16 | EA6C5110206E3E4100FF70D4 /* DataSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C510F206E3E4100FF70D4 /* DataSocket.swift */; }; 17 | EA6C5112206E3F8C00FF70D4 /* SocketDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C5111206E3F8C00FF70D4 /* SocketDataManager.swift */; }; 18 | EA6C5114206EB00F00FF70D4 /* PresenterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA6C5113206EB00F00FF70D4 /* PresenterProtocol.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | EA6C5100206E3DC200FF70D4 /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = EA6C50E3206E3DC100FF70D4 /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = EA6C50EA206E3DC200FF70D4; 27 | remoteInfo = SocketConnectionTest; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | EA6C50EB206E3DC200FF70D4 /* SocketConnectionTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SocketConnectionTest.app; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | EA6C50EE206E3DC200FF70D4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 34 | EA6C50F0206E3DC200FF70D4 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 35 | EA6C50F3206E3DC200FF70D4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 36 | EA6C50F5206E3DC200FF70D4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 37 | EA6C50F8206E3DC200FF70D4 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 38 | EA6C50FA206E3DC200FF70D4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 39 | EA6C50FF206E3DC200FF70D4 /* SocketConnectionTestTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SocketConnectionTestTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | EA6C5103206E3DC200FF70D4 /* SocketConnectionTestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketConnectionTestTests.swift; sourceTree = ""; }; 41 | EA6C5105206E3DC200FF70D4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 42 | EA6C510F206E3E4100FF70D4 /* DataSocket.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataSocket.swift; sourceTree = ""; }; 43 | EA6C5111206E3F8C00FF70D4 /* SocketDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketDataManager.swift; sourceTree = ""; }; 44 | EA6C5113206EB00F00FF70D4 /* PresenterProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresenterProtocol.swift; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | EA6C50E8206E3DC200FF70D4 /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | EA6C50FC206E3DC200FF70D4 /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | ); 60 | runOnlyForDeploymentPostprocessing = 0; 61 | }; 62 | /* End PBXFrameworksBuildPhase section */ 63 | 64 | /* Begin PBXGroup section */ 65 | EA6C50E2206E3DC100FF70D4 = { 66 | isa = PBXGroup; 67 | children = ( 68 | EA6C50ED206E3DC200FF70D4 /* SocketConnectionTest */, 69 | EA6C5102206E3DC200FF70D4 /* SocketConnectionTestTests */, 70 | EA6C50EC206E3DC200FF70D4 /* Products */, 71 | ); 72 | sourceTree = ""; 73 | }; 74 | EA6C50EC206E3DC200FF70D4 /* Products */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | EA6C50EB206E3DC200FF70D4 /* SocketConnectionTest.app */, 78 | EA6C50FF206E3DC200FF70D4 /* SocketConnectionTestTests.xctest */, 79 | ); 80 | name = Products; 81 | sourceTree = ""; 82 | }; 83 | EA6C50ED206E3DC200FF70D4 /* SocketConnectionTest */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | EA6C510E206E3E3200FF70D4 /* Model */, 87 | EA6C50EE206E3DC200FF70D4 /* AppDelegate.swift */, 88 | EA6C50F0206E3DC200FF70D4 /* ViewController.swift */, 89 | EA6C50F2206E3DC200FF70D4 /* Main.storyboard */, 90 | EA6C50F5206E3DC200FF70D4 /* Assets.xcassets */, 91 | EA6C50F7206E3DC200FF70D4 /* LaunchScreen.storyboard */, 92 | EA6C50FA206E3DC200FF70D4 /* Info.plist */, 93 | EA6C5111206E3F8C00FF70D4 /* SocketDataManager.swift */, 94 | EA6C5113206EB00F00FF70D4 /* PresenterProtocol.swift */, 95 | ); 96 | path = SocketConnectionTest; 97 | sourceTree = ""; 98 | }; 99 | EA6C5102206E3DC200FF70D4 /* SocketConnectionTestTests */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | EA6C5103206E3DC200FF70D4 /* SocketConnectionTestTests.swift */, 103 | EA6C5105206E3DC200FF70D4 /* Info.plist */, 104 | ); 105 | path = SocketConnectionTestTests; 106 | sourceTree = ""; 107 | }; 108 | EA6C510E206E3E3200FF70D4 /* Model */ = { 109 | isa = PBXGroup; 110 | children = ( 111 | EA6C510F206E3E4100FF70D4 /* DataSocket.swift */, 112 | ); 113 | path = Model; 114 | sourceTree = ""; 115 | }; 116 | /* End PBXGroup section */ 117 | 118 | /* Begin PBXNativeTarget section */ 119 | EA6C50EA206E3DC200FF70D4 /* SocketConnectionTest */ = { 120 | isa = PBXNativeTarget; 121 | buildConfigurationList = EA6C5108206E3DC200FF70D4 /* Build configuration list for PBXNativeTarget "SocketConnectionTest" */; 122 | buildPhases = ( 123 | EA6C50E7206E3DC200FF70D4 /* Sources */, 124 | EA6C50E8206E3DC200FF70D4 /* Frameworks */, 125 | EA6C50E9206E3DC200FF70D4 /* Resources */, 126 | ); 127 | buildRules = ( 128 | ); 129 | dependencies = ( 130 | ); 131 | name = SocketConnectionTest; 132 | productName = SocketConnectionTest; 133 | productReference = EA6C50EB206E3DC200FF70D4 /* SocketConnectionTest.app */; 134 | productType = "com.apple.product-type.application"; 135 | }; 136 | EA6C50FE206E3DC200FF70D4 /* SocketConnectionTestTests */ = { 137 | isa = PBXNativeTarget; 138 | buildConfigurationList = EA6C510B206E3DC200FF70D4 /* Build configuration list for PBXNativeTarget "SocketConnectionTestTests" */; 139 | buildPhases = ( 140 | EA6C50FB206E3DC200FF70D4 /* Sources */, 141 | EA6C50FC206E3DC200FF70D4 /* Frameworks */, 142 | EA6C50FD206E3DC200FF70D4 /* Resources */, 143 | ); 144 | buildRules = ( 145 | ); 146 | dependencies = ( 147 | EA6C5101206E3DC200FF70D4 /* PBXTargetDependency */, 148 | ); 149 | name = SocketConnectionTestTests; 150 | productName = SocketConnectionTestTests; 151 | productReference = EA6C50FF206E3DC200FF70D4 /* SocketConnectionTestTests.xctest */; 152 | productType = "com.apple.product-type.bundle.unit-test"; 153 | }; 154 | /* End PBXNativeTarget section */ 155 | 156 | /* Begin PBXProject section */ 157 | EA6C50E3206E3DC100FF70D4 /* Project object */ = { 158 | isa = PBXProject; 159 | attributes = { 160 | LastSwiftUpdateCheck = 0910; 161 | LastUpgradeCheck = 1010; 162 | ORGANIZATIONNAME = com.anoopm; 163 | TargetAttributes = { 164 | EA6C50EA206E3DC200FF70D4 = { 165 | CreatedOnToolsVersion = 9.1; 166 | LastSwiftMigration = 1010; 167 | ProvisioningStyle = Automatic; 168 | }; 169 | EA6C50FE206E3DC200FF70D4 = { 170 | CreatedOnToolsVersion = 9.1; 171 | LastSwiftMigration = 1010; 172 | ProvisioningStyle = Automatic; 173 | TestTargetID = EA6C50EA206E3DC200FF70D4; 174 | }; 175 | }; 176 | }; 177 | buildConfigurationList = EA6C50E6206E3DC100FF70D4 /* Build configuration list for PBXProject "SocketConnectionTest" */; 178 | compatibilityVersion = "Xcode 8.0"; 179 | developmentRegion = en; 180 | hasScannedForEncodings = 0; 181 | knownRegions = ( 182 | en, 183 | Base, 184 | ); 185 | mainGroup = EA6C50E2206E3DC100FF70D4; 186 | productRefGroup = EA6C50EC206E3DC200FF70D4 /* Products */; 187 | projectDirPath = ""; 188 | projectRoot = ""; 189 | targets = ( 190 | EA6C50EA206E3DC200FF70D4 /* SocketConnectionTest */, 191 | EA6C50FE206E3DC200FF70D4 /* SocketConnectionTestTests */, 192 | ); 193 | }; 194 | /* End PBXProject section */ 195 | 196 | /* Begin PBXResourcesBuildPhase section */ 197 | EA6C50E9206E3DC200FF70D4 /* Resources */ = { 198 | isa = PBXResourcesBuildPhase; 199 | buildActionMask = 2147483647; 200 | files = ( 201 | EA6C50F9206E3DC200FF70D4 /* LaunchScreen.storyboard in Resources */, 202 | EA6C50F6206E3DC200FF70D4 /* Assets.xcassets in Resources */, 203 | EA6C50F4206E3DC200FF70D4 /* Main.storyboard in Resources */, 204 | ); 205 | runOnlyForDeploymentPostprocessing = 0; 206 | }; 207 | EA6C50FD206E3DC200FF70D4 /* Resources */ = { 208 | isa = PBXResourcesBuildPhase; 209 | buildActionMask = 2147483647; 210 | files = ( 211 | ); 212 | runOnlyForDeploymentPostprocessing = 0; 213 | }; 214 | /* End PBXResourcesBuildPhase section */ 215 | 216 | /* Begin PBXSourcesBuildPhase section */ 217 | EA6C50E7206E3DC200FF70D4 /* Sources */ = { 218 | isa = PBXSourcesBuildPhase; 219 | buildActionMask = 2147483647; 220 | files = ( 221 | EA6C5112206E3F8C00FF70D4 /* SocketDataManager.swift in Sources */, 222 | EA6C50F1206E3DC200FF70D4 /* ViewController.swift in Sources */, 223 | EA6C5110206E3E4100FF70D4 /* DataSocket.swift in Sources */, 224 | EA6C50EF206E3DC200FF70D4 /* AppDelegate.swift in Sources */, 225 | EA6C5114206EB00F00FF70D4 /* PresenterProtocol.swift in Sources */, 226 | ); 227 | runOnlyForDeploymentPostprocessing = 0; 228 | }; 229 | EA6C50FB206E3DC200FF70D4 /* Sources */ = { 230 | isa = PBXSourcesBuildPhase; 231 | buildActionMask = 2147483647; 232 | files = ( 233 | EA6C5104206E3DC200FF70D4 /* SocketConnectionTestTests.swift in Sources */, 234 | ); 235 | runOnlyForDeploymentPostprocessing = 0; 236 | }; 237 | /* End PBXSourcesBuildPhase section */ 238 | 239 | /* Begin PBXTargetDependency section */ 240 | EA6C5101206E3DC200FF70D4 /* PBXTargetDependency */ = { 241 | isa = PBXTargetDependency; 242 | target = EA6C50EA206E3DC200FF70D4 /* SocketConnectionTest */; 243 | targetProxy = EA6C5100206E3DC200FF70D4 /* PBXContainerItemProxy */; 244 | }; 245 | /* End PBXTargetDependency section */ 246 | 247 | /* Begin PBXVariantGroup section */ 248 | EA6C50F2206E3DC200FF70D4 /* Main.storyboard */ = { 249 | isa = PBXVariantGroup; 250 | children = ( 251 | EA6C50F3206E3DC200FF70D4 /* Base */, 252 | ); 253 | name = Main.storyboard; 254 | sourceTree = ""; 255 | }; 256 | EA6C50F7206E3DC200FF70D4 /* LaunchScreen.storyboard */ = { 257 | isa = PBXVariantGroup; 258 | children = ( 259 | EA6C50F8206E3DC200FF70D4 /* Base */, 260 | ); 261 | name = LaunchScreen.storyboard; 262 | sourceTree = ""; 263 | }; 264 | /* End PBXVariantGroup section */ 265 | 266 | /* Begin XCBuildConfiguration section */ 267 | EA6C5106206E3DC200FF70D4 /* Debug */ = { 268 | isa = XCBuildConfiguration; 269 | buildSettings = { 270 | ALWAYS_SEARCH_USER_PATHS = NO; 271 | CLANG_ANALYZER_NONNULL = YES; 272 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 273 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 274 | CLANG_CXX_LIBRARY = "libc++"; 275 | CLANG_ENABLE_MODULES = YES; 276 | CLANG_ENABLE_OBJC_ARC = YES; 277 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 278 | CLANG_WARN_BOOL_CONVERSION = YES; 279 | CLANG_WARN_COMMA = YES; 280 | CLANG_WARN_CONSTANT_CONVERSION = YES; 281 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 282 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 283 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 284 | CLANG_WARN_EMPTY_BODY = YES; 285 | CLANG_WARN_ENUM_CONVERSION = YES; 286 | CLANG_WARN_INFINITE_RECURSION = YES; 287 | CLANG_WARN_INT_CONVERSION = YES; 288 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 289 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 290 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 291 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 292 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 293 | CLANG_WARN_STRICT_PROTOTYPES = YES; 294 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 295 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 296 | CLANG_WARN_UNREACHABLE_CODE = YES; 297 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 298 | CODE_SIGN_IDENTITY = "iPhone Developer"; 299 | COPY_PHASE_STRIP = NO; 300 | DEBUG_INFORMATION_FORMAT = dwarf; 301 | ENABLE_STRICT_OBJC_MSGSEND = YES; 302 | ENABLE_TESTABILITY = YES; 303 | GCC_C_LANGUAGE_STANDARD = gnu11; 304 | GCC_DYNAMIC_NO_PIC = NO; 305 | GCC_NO_COMMON_BLOCKS = YES; 306 | GCC_OPTIMIZATION_LEVEL = 0; 307 | GCC_PREPROCESSOR_DEFINITIONS = ( 308 | "DEBUG=1", 309 | "$(inherited)", 310 | ); 311 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 312 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 313 | GCC_WARN_UNDECLARED_SELECTOR = YES; 314 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 315 | GCC_WARN_UNUSED_FUNCTION = YES; 316 | GCC_WARN_UNUSED_VARIABLE = YES; 317 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 318 | MTL_ENABLE_DEBUG_INFO = YES; 319 | ONLY_ACTIVE_ARCH = YES; 320 | SDKROOT = iphoneos; 321 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 322 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 323 | }; 324 | name = Debug; 325 | }; 326 | EA6C5107206E3DC200FF70D4 /* Release */ = { 327 | isa = XCBuildConfiguration; 328 | buildSettings = { 329 | ALWAYS_SEARCH_USER_PATHS = NO; 330 | CLANG_ANALYZER_NONNULL = YES; 331 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 332 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 333 | CLANG_CXX_LIBRARY = "libc++"; 334 | CLANG_ENABLE_MODULES = YES; 335 | CLANG_ENABLE_OBJC_ARC = YES; 336 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 337 | CLANG_WARN_BOOL_CONVERSION = YES; 338 | CLANG_WARN_COMMA = YES; 339 | CLANG_WARN_CONSTANT_CONVERSION = YES; 340 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 341 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 342 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 343 | CLANG_WARN_EMPTY_BODY = YES; 344 | CLANG_WARN_ENUM_CONVERSION = YES; 345 | CLANG_WARN_INFINITE_RECURSION = YES; 346 | CLANG_WARN_INT_CONVERSION = YES; 347 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 348 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 349 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 350 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 351 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 352 | CLANG_WARN_STRICT_PROTOTYPES = YES; 353 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 354 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 355 | CLANG_WARN_UNREACHABLE_CODE = YES; 356 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 357 | CODE_SIGN_IDENTITY = "iPhone Developer"; 358 | COPY_PHASE_STRIP = NO; 359 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 360 | ENABLE_NS_ASSERTIONS = NO; 361 | ENABLE_STRICT_OBJC_MSGSEND = YES; 362 | GCC_C_LANGUAGE_STANDARD = gnu11; 363 | GCC_NO_COMMON_BLOCKS = YES; 364 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 365 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 366 | GCC_WARN_UNDECLARED_SELECTOR = YES; 367 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 368 | GCC_WARN_UNUSED_FUNCTION = YES; 369 | GCC_WARN_UNUSED_VARIABLE = YES; 370 | IPHONEOS_DEPLOYMENT_TARGET = 11.1; 371 | MTL_ENABLE_DEBUG_INFO = NO; 372 | SDKROOT = iphoneos; 373 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 374 | VALIDATE_PRODUCT = YES; 375 | }; 376 | name = Release; 377 | }; 378 | EA6C5109206E3DC200FF70D4 /* Debug */ = { 379 | isa = XCBuildConfiguration; 380 | buildSettings = { 381 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 382 | CODE_SIGN_STYLE = Automatic; 383 | DEVELOPMENT_TEAM = 6UT7M6DT43; 384 | INFOPLIST_FILE = SocketConnectionTest/Info.plist; 385 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 386 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 387 | PRODUCT_BUNDLE_IDENTIFIER = com.anoopm.SocketConnectionTest; 388 | PRODUCT_NAME = "$(TARGET_NAME)"; 389 | SWIFT_VERSION = 4.2; 390 | TARGETED_DEVICE_FAMILY = "1,2"; 391 | }; 392 | name = Debug; 393 | }; 394 | EA6C510A206E3DC200FF70D4 /* Release */ = { 395 | isa = XCBuildConfiguration; 396 | buildSettings = { 397 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 398 | CODE_SIGN_STYLE = Automatic; 399 | DEVELOPMENT_TEAM = 6UT7M6DT43; 400 | INFOPLIST_FILE = SocketConnectionTest/Info.plist; 401 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 402 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 403 | PRODUCT_BUNDLE_IDENTIFIER = com.anoopm.SocketConnectionTest; 404 | PRODUCT_NAME = "$(TARGET_NAME)"; 405 | SWIFT_VERSION = 4.2; 406 | TARGETED_DEVICE_FAMILY = "1,2"; 407 | }; 408 | name = Release; 409 | }; 410 | EA6C510C206E3DC200FF70D4 /* Debug */ = { 411 | isa = XCBuildConfiguration; 412 | buildSettings = { 413 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 414 | BUNDLE_LOADER = "$(TEST_HOST)"; 415 | CODE_SIGN_STYLE = Automatic; 416 | INFOPLIST_FILE = SocketConnectionTestTests/Info.plist; 417 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 418 | PRODUCT_BUNDLE_IDENTIFIER = com.anoopm.SocketConnectionTestTests; 419 | PRODUCT_NAME = "$(TARGET_NAME)"; 420 | SWIFT_VERSION = 4.2; 421 | TARGETED_DEVICE_FAMILY = "1,2"; 422 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SocketConnectionTest.app/SocketConnectionTest"; 423 | }; 424 | name = Debug; 425 | }; 426 | EA6C510D206E3DC200FF70D4 /* Release */ = { 427 | isa = XCBuildConfiguration; 428 | buildSettings = { 429 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 430 | BUNDLE_LOADER = "$(TEST_HOST)"; 431 | CODE_SIGN_STYLE = Automatic; 432 | INFOPLIST_FILE = SocketConnectionTestTests/Info.plist; 433 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 434 | PRODUCT_BUNDLE_IDENTIFIER = com.anoopm.SocketConnectionTestTests; 435 | PRODUCT_NAME = "$(TARGET_NAME)"; 436 | SWIFT_VERSION = 4.2; 437 | TARGETED_DEVICE_FAMILY = "1,2"; 438 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SocketConnectionTest.app/SocketConnectionTest"; 439 | }; 440 | name = Release; 441 | }; 442 | /* End XCBuildConfiguration section */ 443 | 444 | /* Begin XCConfigurationList section */ 445 | EA6C50E6206E3DC100FF70D4 /* Build configuration list for PBXProject "SocketConnectionTest" */ = { 446 | isa = XCConfigurationList; 447 | buildConfigurations = ( 448 | EA6C5106206E3DC200FF70D4 /* Debug */, 449 | EA6C5107206E3DC200FF70D4 /* Release */, 450 | ); 451 | defaultConfigurationIsVisible = 0; 452 | defaultConfigurationName = Release; 453 | }; 454 | EA6C5108206E3DC200FF70D4 /* Build configuration list for PBXNativeTarget "SocketConnectionTest" */ = { 455 | isa = XCConfigurationList; 456 | buildConfigurations = ( 457 | EA6C5109206E3DC200FF70D4 /* Debug */, 458 | EA6C510A206E3DC200FF70D4 /* Release */, 459 | ); 460 | defaultConfigurationIsVisible = 0; 461 | defaultConfigurationName = Release; 462 | }; 463 | EA6C510B206E3DC200FF70D4 /* Build configuration list for PBXNativeTarget "SocketConnectionTestTests" */ = { 464 | isa = XCConfigurationList; 465 | buildConfigurations = ( 466 | EA6C510C206E3DC200FF70D4 /* Debug */, 467 | EA6C510D206E3DC200FF70D4 /* Release */, 468 | ); 469 | defaultConfigurationIsVisible = 0; 470 | defaultConfigurationName = Release; 471 | }; 472 | /* End XCConfigurationList section */ 473 | }; 474 | rootObject = EA6C50E3206E3DC100FF70D4 /* Project object */; 475 | } 476 | -------------------------------------------------------------------------------- /SocketConnectionTest/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SocketConnectionTest 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /SocketConnectionTest/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /SocketConnectionTest/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /SocketConnectionTest/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 81 | 82 | 83 | 84 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /SocketConnectionTest/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UIRequiresFullScreen 32 | 33 | UIStatusBarHidden 34 | 35 | UIStatusBarStyle 36 | UIStatusBarStyleLightContent 37 | UISupportedInterfaceOrientations 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationLandscapeLeft 41 | UIInterfaceOrientationLandscapeRight 42 | 43 | UISupportedInterfaceOrientations~ipad 44 | 45 | UIInterfaceOrientationPortrait 46 | UIInterfaceOrientationPortraitUpsideDown 47 | UIInterfaceOrientationLandscapeLeft 48 | UIInterfaceOrientationLandscapeRight 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /SocketConnectionTest/Model/DataSocket.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DataSocket.swift 3 | // SocketConnectionTest 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct DataSocket { 12 | 13 | let ipAddress: String! 14 | let port: Int! 15 | 16 | init(ip: String, port: String){ 17 | self.ipAddress = ip 18 | self.port = Int(port) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /SocketConnectionTest/PresenterProtocol.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PresenterProtocol.swift 3 | // SocketConnectionTest 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol PresenterProtocol: class { 12 | 13 | func resetUIWithConnection(status: Bool) 14 | func updateStatusViewWith(status: String) 15 | func update(message: String) 16 | } 17 | -------------------------------------------------------------------------------- /SocketConnectionTest/SocketDataManager.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SocketDataManager.swift 3 | // SocketConnectionTest 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | class SocketDataManager: NSObject, StreamDelegate { 12 | 13 | var readStream: Unmanaged? 14 | var writeStream: Unmanaged? 15 | var inputStream: InputStream? 16 | var outputStream: OutputStream? 17 | var messages = [AnyHashable]() 18 | weak var uiPresenter :PresenterProtocol! 19 | 20 | init(with presenter:PresenterProtocol){ 21 | 22 | self.uiPresenter = presenter 23 | } 24 | func connectWith(socket: DataSocket) { 25 | 26 | CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (socket.ipAddress! as CFString), UInt32(socket.port), &readStream, &writeStream) 27 | messages = [AnyHashable]() 28 | open() 29 | } 30 | 31 | func disconnect(){ 32 | 33 | close() 34 | } 35 | 36 | func open() { 37 | print("Opening streams.") 38 | outputStream = writeStream?.takeRetainedValue() 39 | inputStream = readStream?.takeRetainedValue() 40 | outputStream?.delegate = self 41 | inputStream?.delegate = self 42 | outputStream?.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default) 43 | inputStream?.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default) 44 | outputStream?.open() 45 | inputStream?.open() 46 | } 47 | 48 | func close() { 49 | print("Closing streams.") 50 | uiPresenter?.resetUIWithConnection(status: false) 51 | inputStream?.close() 52 | outputStream?.close() 53 | inputStream?.remove(from: RunLoop.current, forMode: RunLoop.Mode.default) 54 | outputStream?.remove(from: RunLoop.current, forMode: RunLoop.Mode.default) 55 | inputStream?.delegate = nil 56 | outputStream?.delegate = nil 57 | inputStream = nil 58 | outputStream = nil 59 | } 60 | 61 | func stream(_ aStream: Stream, handle eventCode: Stream.Event) { 62 | print("stream event \(eventCode)") 63 | switch eventCode { 64 | case .openCompleted: 65 | uiPresenter?.resetUIWithConnection(status: true) 66 | print("Stream opened") 67 | case .hasBytesAvailable: 68 | if aStream == inputStream { 69 | var dataBuffer = Array(repeating: 0, count: 1024) 70 | var len: Int 71 | while (inputStream?.hasBytesAvailable)! { 72 | len = (inputStream?.read(&dataBuffer, maxLength: 1024))! 73 | if len > 0 { 74 | let output = String(bytes: dataBuffer, encoding: .ascii) 75 | if nil != output { 76 | print("server said: \(output ?? "")") 77 | messageReceived(message: output!) 78 | } 79 | } 80 | } 81 | } 82 | case .hasSpaceAvailable: 83 | print("Stream has space available now") 84 | case .errorOccurred: 85 | print("\(aStream.streamError?.localizedDescription ?? "")") 86 | case .endEncountered: 87 | aStream.close() 88 | aStream.remove(from: RunLoop.current, forMode: RunLoop.Mode.default) 89 | print("close stream") 90 | uiPresenter?.resetUIWithConnection(status: false) 91 | default: 92 | print("Unknown event") 93 | } 94 | } 95 | 96 | func messageReceived(message: String){ 97 | 98 | uiPresenter?.update(message: "server said: \(message)") 99 | print(message) 100 | } 101 | 102 | func send(message: String){ 103 | 104 | let response = "msg:\(message)" 105 | let buff = [UInt8](message.utf8) 106 | if let _ = response.data(using: .ascii) { 107 | outputStream?.write(buff, maxLength: buff.count) 108 | } 109 | 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /SocketConnectionTest/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // SocketConnectionTest 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | var socketConnector:SocketDataManager! 14 | @IBOutlet weak var ipAddressField: UITextField! 15 | @IBOutlet weak var portField: UITextField! 16 | @IBOutlet weak var messageField: UITextField! 17 | @IBOutlet weak var messageHistoryView: UITextView! 18 | @IBOutlet weak var connectBtn: UIButton! 19 | @IBOutlet weak var sendBtn: UIButton! 20 | @IBOutlet weak var statusView: UIView! 21 | @IBOutlet weak var statusLabl: UILabel! 22 | override func viewDidLoad() { 23 | super.viewDidLoad() 24 | socketConnector = SocketDataManager(with: self) 25 | resetUIWithConnection(status: false) 26 | // Do any additional setup after loading the view, typically from a nib. 27 | } 28 | 29 | override func didReceiveMemoryWarning() { 30 | super.didReceiveMemoryWarning() 31 | // Dispose of any resources that can be recreated. 32 | } 33 | 34 | 35 | @IBAction func connect(){ 36 | //http://localhost:50694/ 37 | guard let ipAddr = ipAddressField.text, let portVal = portField.text else { 38 | return 39 | } 40 | let soc = DataSocket(ip: ipAddr, port: portVal) 41 | socketConnector.connectWith(socket: soc) 42 | 43 | } 44 | @IBAction func send(){ 45 | guard let msg = messageField.text else { 46 | return 47 | } 48 | send(message: msg) 49 | messageField.text = "" 50 | } 51 | func send(message: String){ 52 | 53 | socketConnector.send(message: message) 54 | update(message: "me:\(message)") 55 | } 56 | } 57 | 58 | extension ViewController: PresenterProtocol{ 59 | 60 | func resetUIWithConnection(status: Bool){ 61 | 62 | ipAddressField.isEnabled = !status 63 | portField.isEnabled = !status 64 | messageField.isEnabled = status 65 | connectBtn.isEnabled = !status 66 | sendBtn.isEnabled = status 67 | 68 | if (status){ 69 | updateStatusViewWith(status: "Connected") 70 | }else{ 71 | updateStatusViewWith(status: "Disconnected") 72 | } 73 | } 74 | func updateStatusViewWith(status: String){ 75 | 76 | statusLabl.text = status 77 | } 78 | 79 | func update(message: String){ 80 | 81 | if let text = messageHistoryView.text{ 82 | let newText = """ 83 | \(text) 84 | \(message) 85 | """ 86 | messageHistoryView.text = newText 87 | }else{ 88 | let newText = """ 89 | \(message) 90 | """ 91 | messageHistoryView.text = newText 92 | } 93 | 94 | let myRange=NSMakeRange(messageHistoryView.text.count-1, 0); 95 | messageHistoryView.scrollRangeToVisible(myRange) 96 | 97 | 98 | } 99 | 100 | 101 | } 102 | 103 | -------------------------------------------------------------------------------- /SocketConnectionTestTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIStatusBarHidden 6 | 7 | UIViewControllerBasedStatusBarAppearance 8 | 9 | CFBundleDevelopmentRegion 10 | $(DEVELOPMENT_LANGUAGE) 11 | CFBundleExecutable 12 | $(EXECUTABLE_NAME) 13 | CFBundleIdentifier 14 | $(PRODUCT_BUNDLE_IDENTIFIER) 15 | CFBundleInfoDictionaryVersion 16 | 6.0 17 | CFBundleName 18 | $(PRODUCT_NAME) 19 | CFBundlePackageType 20 | BNDL 21 | CFBundleShortVersionString 22 | 1.0 23 | CFBundleVersion 24 | 1 25 | 26 | 27 | -------------------------------------------------------------------------------- /SocketConnectionTestTests/SocketConnectionTestTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SocketConnectionTestTests.swift 3 | // SocketConnectionTestTests 4 | // 5 | // Created by anoop mohanan on 30/03/18. 6 | // Copyright © 2018 com.anoopm. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import SocketConnectionTest 11 | 12 | class SocketConnectionTestTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | --------------------------------------------------------------------------------