Group Group Group Group Group Group Group Group Group
raywenderlich.com Forums

TCP Server With the SwiftNIO Networking Framework

Mobile developers often work with REST APIs or other networking protocols in their applications to access data or to coordinate activities. In this tutorial you will create a Swift Server with SwiftNIO, a low-level networking framework that makes creating TCP servers easier than before.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/76-tcp-server-with-the-swiftnio-networking-framework

Today I try the QOTD-NIO-Final project (download via https://koenig-media.raywenderlich.com/uploads/2018/03/QOTD-NIO-3.zip ), but it’s not working.
Mobile app does not get the quote. In refreshQuote method, there has response, but message is empty, string is empty.

Hello,

Things to check:

  1. Are you sure the server is running on your machine?
  2. Have you put a breakpoint in the channelRegistered method in the server to check if it receives a request?

Yes, the server is running. When I tap the refresh button in the app’s navigation bar, no quote shows in the app. And I’m sure the app is running into refreshQuote method, but the message is empty.

I ran into the same issue. I found that it worked if I modified the “QuoteHandler” in the server code to use the function “channelActive(ctx: ChannelHandlerContext)” instead of “channelRegistered(ctx: ChannelHandlerContext)”. If you read the documentation on SwiftNIO, you shouldn’t try to read/write to the socket until its active and so there was probably a timing issue taking place.

2 Likes

Sorry for the delayed reply here - it looks like @psychoticidiot nailed the issue, but I am wondering if this is because of a version bump. I pulled the materials and it no longer works for me either, but NIO is now on v1.4.2, so something must have changed. However, conceptually @psychoticidiot is right due to the way these sockets should be read.

I apologize for the inconvenience - I’ll work on an update as soon as I’m able to!

Sure enough, that’s the issue.

For those reading this tutorial, until the materials are updated, please change the line in Package.swift where the NIO dependency is declared to this for the time being:

    .package(url: "https://github.com/apple/swift-nio.git", .exact("1.1.0"))

I will update the materials as soon as possible.

EDIT

The materials have been updated. I’ll edit the tutorial later to get the socket connection right, but v1.1.0 of NIO is pinned in the SPM manifest now, so everything works as it should.

1 Like

How can i get the client IPv4 address from a bluesocket connection on the channelActive function ?

@dokun1 Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi there @ffabri! If you put a breakpoint somewhere inside channelActive and send a request, try printing out ctx.remoteAddress and this should print out your address whether you configure everything for IPv4 or IPv6. Let me know if this answers your question!

1 Like

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!