Matthew Cawley
Matthew Cawley

Reputation: 2998

Socket connection with different ports

I'm trying to run a socket connection from my iOS app to a bespoke server.

The iOS communicates via UDP to the server on port 12345. The request needs to come from socket 54321 on the device.

How do I open up a socket connection on iOS that communicates to a server on one port but listens via a different port.

I have followed the example on: https://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server But this does not cover the local port

I have also looked at the documentation on https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html but I am finding it unclear due to my limited knowledge of socket connections.

I am currently attempting to use https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Reference_GCDAsyncSocket But I don't seem to be able to control the local port.

Thanks in advance

Upvotes: 0

Views: 261

Answers (1)

Paulw11
Paulw11

Reputation: 114836

Normally you don't care about the sending socket, but when you create your AsyncUDPSocket you can use bindToPort: to set its port. Then use sendData:toHost:port:timeout:tag: to send the data

Upvotes: 1

Related Questions