Learning swift socket programming

So, I was chatting with an app developer friend the other day (has written a hospital image recognition app, has 12 hospitals on board, etc. he’s doing it professionally) about general stuff and one of the ideas we had for a very basic app (for me to cut my teeth on with modern development tools) is a dumbed down network diagnostic.

So, this is my new project. Its been decades since I’ve done any real programming outside of scripting (last time programming in any serious language © was coding on a DIKU based MUD in the late 90s / early 00s). Back in the days with no IDE, just vim, gcc and GNU make. The only IDE i’ve seriously used was Turbo Pascal or Turbo C way back…

I’ve never done any socket programming, and never written anything in swift. Swift is the new hotness for iOS and macOS so why not kill 2 birds with one stone?

So, my objective is to write a diagnostic application, which, given an IP address a protocol and a port will attempt to give the user various connection stats, such as

  • traceroute
  • packet loss
  • ping time
  • etc.

… without using any system utilities (i.e., re-implementing ping and traceroute, etc. myself with sockets). It will also highlight areas of concern automatically with some sensible/configurable thresholds.

The idea being a “dumb end user” (i.e., not a network admin) can plug in some host information and get some automatically checked network info for diagnostic purposes that they can send to a network admin or developer.

Plan to make it command line in macOS or Linux (via open source swift - maybe C first then port) and port it to iOS later.

Currently, i’m refreshing my brain with the way IP works from a programming perspective with Beej’s famous network programming guide


I’m also reading through this swift network programming guide

My time has been kinda limited lately, but i figure i can do this in stages, and will kill two birds with one stone (sockets and swift).


That link is useful, I’m also starting to learn a little swift, it runs native on Linux (minus uikit etc) and arm as well, got it on a raspberry pi to play around with the end goal of making Some libraries for it.


So, looks like to do ICMP within your own application, you need raw sockets which means you need to run as root if i am reading things correctly. I don’t want to have to run as root at this time.

So, i’ll be doing some sort of connect scan and/or UDP trace route as that works as a non-privileged user.

I’ve followed through the first couple of examples of Beej’s guide in C. I think i have a handle on how the general connection process works now.

So now time to get my head around doing it in swift.

What is doing my head in, in swift so far is the concept of optionals. I get the concept, but how to list them as wrapped or unwrap them and the context to do so, etc. is very alien to someone who’s only ever programmed in a typed language like C or Pascal that simply has no concept of an optional.

I’m sure its just practice.

I definitely feel you on optionals. Something that really got me stumped for a while, but I started to enjoy was closure statements.

1 Like

Hah. Cheers.

The only serious coding i’ve done has been in C and Pascal (many years ago) so i guess i’m the equivalent of neanderthal man discovering the aeroplane (with modern language features and IDEs) at this point…

So basically everything is alien haha. I guess that’s a good thing though, it’s easier to “forget what you know” as people like to tell you to do

I was just looking at how optionals work, found this somewhat useful https://www.youtube.com/watch?v=OkzZ3T3lrlg it has a few different decent examples I think of the various uses

1 Like

How are you getting on with this?

been flat out with work and other stuff. need to get back into it

1 Like

I know the feeling.
I was looking at doing the 100 days of swift challenge.


is it a tutorial based thing? because i really need to cut my teeth on the language but maybe trying to run before i can walk :smiley:

like i said earlier, swift to me seems a lot harder to get stuck into than anything i’ve used before (including x86 assembly - made my own VGA graphics library for Pascal back in the day)… purely because i need to get my head around the notation of optionals etc. and just time poor.


Essentially a set of tutorials going through swift and then projects using swift on iOS split into parts to do per day.

1 Like