How to listen to a TCP port to detect changes in the server? - macos

I'm working on mac os x. I'm trying to build a cocoa app working on a storage server (similar to Dropbox) that does something whenever a file is added,removed..I have already a client app installed on the mac that shows all the files stored on the server and I need to listen to the port that the server is using to send changes notification to the app. I've started following some tutorials for Sockets but I get "Address already in use".
The Question: are sockets the only way to listen to a port and if yes is there a way to build a socket to listen to an already existing server/client connection?

If a process is already listening on a port then no other process can bind(2) to that port. Alternatives would include to have a proxy listen on that port that would deal with events and then pass them on elsewhere (the client app may not play well with this), or to use firewall rules to duplicate the packets to some other port that your app would then listen on, or maybe the client application issues notifications that then can be acted on.
https://github.com/thrig/lognots
Is one way to inspect the notifications available.

Remember that listening on a port is how you prepare to receive incoming connections. It is not necessary to receive data — once a connection is established, data can flow in both directions! It is almost never appropriate for a client application to listen on a port; that's usually only appropriate for server applications.
With that in mind: Your client application should connect to a port on the server, and the server will send data to the client as appropriate.

Related

Can we host a host websocket server in cloud as proxy/relay server to redirect websocket traffic from local network

[This post has been updated with a possible approach, which is added to the bottom]
I am trying to create a web socket connection between two devices((A). chrome in different computer and (B) a raspberry pi) in different networks. Thus, i can send video data over websockets. However, I cannot port forward the network of my router to expose my local IP to the network, thus, I can't send the data right away.
Figure below explains the architechture.
Thus ,I am trying to implement a logic so that Device A websockets the public ws of the cloud while, the cloud actually gets the data from Device B.So Device A websockets device B indirectly.
Device A<==>Cloud Server<==>Device B
The cloud could be something like a proxy or a relay websocket server.
The video feed needs to be sent in real time,
Please suggest how can i proceed with it.
Additional note, i have acquired an instance of digitalocean as VPS, following the blog Accessing home services from anywhere, without port forwarding! but not sure how to proceed.
Update(11th Nov,2019):
I am planning to use a websocket server on DigitalOcean instance(droplet),which would be listening to my local IP/port of my raspberry pi.And this VPS would act as a websocket server,which would redirect the traffic.
However, I am unsure how to use the same.And need your kind suggestions.

Firewall blocks connection to second WebSocket server

In short we have two separate servers for our web app. The first one is the main server that uses Websockets for handling "chat rooms", and the second server only handles WebRTC audio chat rooms via Websocket. Both servers use Express to create a HTTPS server, use secure Websocket and the port 443.
I recently encountered a problem where a corporate client's firewall blocked the wss-connection to only the WebRTC server. The error logged in the user's browser was "ERR_CONNECTION_TIMED_OUT", which means the user never connects via Websocket. This has not happened with any other clients.
The Websocket connection works normally between the user and the main server, and no rules have been added to their firewall to use our app.
Has anyone encountered something similar? What kind of a firewall setting might cause this? Could this be a cors problem, since the servers are on their own sub-domains?
The main server could be restricting the type of data sent on port 443, which will use SSL to secure that transmitted data.
Refer to this page for information on the "Well-know port numbers".
The WebRTC audio data may need to be transmitted on its own dedicated port number that has been configured on the main server for this.
The problem was that the main server WebSocket used TCP and the WebRTC server used UDP, and UDP was blocked by corporate firewall on default.
WebRTC should use TCP as a backup, but I'm assuming UDP is still needed for the handshake.

Why browsers can receive incoming connections and other soft's cant!?

my question is simple
When you send data through TCP/IP protocol with EX:firefox you can receive reply on some random port that the browser listen on, while when i try to use a port for another task like CS Gaming or anything else it don't work unless i use kind of VPN ?
PS: there r no firewall blocking connection and port forwarding from my router didn't work as well.
Browsers are client apps that make outbound connections to web servers. When connecting to a server through a router’s NAT, the NAT takes note of the source and destination IP/port pairs so messages sent back from the server on the same connection are automatically routed to the correct client IP/port.
Browsers also support the websocket protocol. This feature makes it seem like the browser is listening on a specific port. However, in reality, it is initiated on a new connection to the server, a connection which remains open all throughout the websocket communication.
What matters is which peer is behind the NAT — the server or the client. For an outbound connection from a client, it can usually use any random port that is available at the time. For an inbound connection to a server, the server's IP/port must be known ahead of time and be routable. If the server is behind a NAT, the router(s) must be configured to make the server reachable from the other side of the NAT.
The server software can make a UPnP request to ask a router to forward inbound packets to the correct IP/Port. The router, depending on its configuration, may or may not honor such a request. If not, the router has to be configured manually by a network administrator.

Socks 4 Bind Request Explanation

i was reading this topic
http://ftp.icm.edu.pl/packages/socks/socks4/SOCKS4.protocol
and what im trying to do is:
i have a client/server application, what im trying to do is to use socks 4 BIND request to bind my server to a remote socks server, and make the clients connect to that socks server and the socks server will make them connect to my server (at least thats how i understand socks BIND request)
but i don't fully understand it (my English is kinda bad), what im asking is, is it possible to do so when i dunno any of the remote IPs of the clients? since the server's BIND request package must contain the address of the remote client and i dont really have than since the clients are from unknown users retrieving status info from my server (or can i use 0 for INANY_ADDR) ?
What you are asking for is not possible with SOCKS, nor is it meant for that purpose. Read the spec again more carefully. The BIND command is meant for use with multi-connection protocols (like FTP), where a primary connection is used to communicate between a client and a server, and BIND facilitates situations where the server needs to connect a secondary connection to the client after the client tells the server where to connect. In that situation, the client would issue a BIND command to SOCKS telling it the server's IP/Port so it only accepts that connection, then send the resulting SOCKS listening IP/Port to the server to connect to.
What you are asking for is better served by using a router with Port Forwarding rules defined. Then you can open a listening port on the router that accepts any inbound connection and forwards it to your app's listening IP/Port. Most modern routers support uPNP (Universal Plug-N-Play) so you can configure the forwarding rules programmably instead of requiring admin access to the router's configuration software.

Unsolicited notifications from server to client over http

I am working on a dropbox like system and I am wondering how the client gets notified when the files change on the server side. It is my impression that both dropbox and ubuntu one operate over HTTP ports and work as follows:
1. if files change on client machine, inotify detects it and preforms a push from the client to the server. (I get this part)
2. if files change on the server a simple unsolicited notification (just a message saying "time to sync") is sent from the server to the client. Then the client initiates a sync to the server.
I dont really care which language I do this in. I am just wondering how the client gets contacted. Specifically, what if a client is behind a firewall with its own local IP addresses. How does the server locate it?
Also, what kind of messaging protocols would be used to do something like this? I was planning on doing this over HTTP or SSH, but I have no attachment do that.
I'm not sure what Dropbox is using, but it could be websockets (unlikely, it's a pretty new and not widely deployed thing) or more likely a pending Ajax request from the client to the server -- to which the server only responds when it has new stuff for the client. The latter is the common way to implement (well, OK -- "hack";-) some form of "server push" with HTTP.
It took a little research into networking to see how this would work, but it is far more trivial then I expected. I am now using standard Java sockets for this. Start up the server process which listens for a socket connection. Then start up the client which connects to the server.
Once the connection is made, messages can be sent back and fourth. This works through NAT (network address translation) which is standard method for routing packets on private networks behind a firewall.

Resources