Bound Local Port for SignalR Connection - sockets

Is there any way to determine which Local Port was used by the Client (C#) to Establish a connection to a SignalR Hub? I have looked at properties of both the HubCallerContext and HubConnection, but it does not seem to be available.

The short answer the question: yes, using netstat or similar you can look at all the connections your client initiated to the SignalR. There you can identify the TCP source port number.
If for some reason you want to get this from inside the application you can either try to find your way via Get underlying tcp connection from HttpWebRequest/Response
or by following this answer.

Related

Cant we read data from an established port using C# or python?

I used netstat to find which ports are established and I tried to read from an established connection using ip and port. I am getting the following error.
Connection actively refused by the machine
No - you can only create a connection to a listening port. "Established" means there is already a connection between two sockets. You can't add a third socket to an existing connection.
If you want to eavesdrop on the connection, there are ways to do that - on Linux, you can create a SOCK_RAW or AF_PACKET socket to receive all packets. On Windows, you need to install the WinPcap or Npcap driver and use libpcap.
If you are just curious and don't need to intercept the connection from a program, you can also use the open source tool Wireshark to see what data your computer is sending and receiving.

TCP connection between client and server gone wrong

I establish a TCP connection between my server and client which runs on the same host. We gather and read from the server or say source in our case continuously.
We read data on say 3 different ports.
Once the source stops publishing data or gets restarted , the server/source is not able to publish data again on the same port saying port is already bind. The reason given is that client still has established connection on those ports.
I wanted to know what could be the probable reasons of this ? Can there be issue since client is already listening on these ports and trying to reconnect again and again because we try this reconnection mechanism. I am more looking for reason on source side as the same code in client sides when source and client are on different host and not the same host works perfectly fine for us.
Edit:-
I found this while going through various article .
On the question of using SO_LINGER to send a RST on close to avoid the TIME_WAIT state: I've been having some problems with router access servers (names withheld to protect the guilty) that have problems dealing with back-to-back connections on a modem dedicated to a specific channel. What they do is let go of the connection, accept another call, attempt to connect to a well-known socket on a host, and the host refuses the connection because there is a connection in TIME_WAIT state involving the well-known socket. (Stevens' book TCP Illustrated, Vol 1 discusses this problem in more detail.) In order to avoid the connection-refused problem, I've had to install an option to do reset-on-close in the server when the server initiates the disconnection.
Link to source:- http://developerweb.net/viewtopic.php?id=2941
I guess i am facing the same problem: 'attempt to connect to a well-known socket on a host, and the host refuses the connection'. Probable fix mention is 'option to do reset-on-close in the server when the server initiates the disconnection'. Now how do I do that ?
Set the SO_REUSEADDR option on the server socket before you bind it and call listen().
EDIT The suggestion to fiddle around with SO_LINGER option is worthless and dangerous to your data in flight. Just use SO_RESUSEADDR.
You need to close the socket bound to that port before you restart/shutdown the server!
http://www.gnu.org/software/libc/manual/html_node/Closing-a-Socket.html
Also, there's a timeout time, which I think is 4 minutes, so if you created a TCP socket and close it, you may still have to wait 4 minutes until it closes.
You can use netstat to see all the bound ports on your system. If you shut down your server, or close your server after forking on connect, you may have zombie processes which are bound to certain ports that do not close and remain active, and thus, you can't rebind to the same port. Show some code.

How to find the port number in ubuntu 12.04?

I need to write my first socket program involving TCP connections. In the program I have created there is a client and server, both of which are the machine I am coding on.However,it requires that I pass the port number as a command line argument. How do I accomplish this?
The answer is simple : Make sure your server and your client agree on the port to use. As long as the port is available and can be used, set up the connected so that the client and server use that same port.
Here's a link that explain the different ranges available for TCP and UDP ports.
As an exemple, the port 3074 is used by microsoft for its Xbox live service. Making an application using this port might interfere with the service.
The port used will be defined either in a configuration file or hard-coded in the source code of both the server and the client. You should easily be able to find it with a quick look at the code or the directory which contains the application.

Does Photon Server supports multiple protocol connections?

I need to connect Unity3D client to Photon Server using both UDP and TCP connections. Is it possible? Where can I read about it?
P.S. I want to use TCP to send large amount of data.
Photon server supports multiple protocols simultaneusly. If you downloaded the server sdk
look for the PhotonServer.config:
It contains entries like this
<UDPListeners>
<UDPListener
IPAddress="0.0.0.0"
Port="5055">
</UDPListener>
</UDPListeners>
and
<TCPListeners>
<TCPListener
IPAddress="0.0.0.0"
Port="4530"
PolicyFile="Policy\assets\socket-policy.xml"
InactivityTimeout="10000"
>
</TCPListener>
</TCPListeners>
Your clients can connect per udp or tcp and interact with each other no mater what protocol thy have chosen.
For the full set of configuration options you can look here: http://doc.exitgames.com/en/onpremise/current/reference/server-config-settings
When a client connects you can query in your server side application how the client connected like this:
public class YourApplication : ApplicationBase
{
if (initRequest.LocalPort == 5055)
{
//
}
if (initRequest.PhotonPeer.GetListenerType() == ListenerType.TCPListener)
{
//
}
Note: UDPListener in the config are represented as ListenerType.ENetListener in code.
You can find the server sdk documentation in the downloaded {sdk}\doc\Photon.SocketServer.chm or online here http://doc-api.exitgames.com/en/onpremise/current/server/doc/annotated.html
Simple answer: No. A photon server cannot have more than 1 type of connection.
However, there is a way to do this depending on your definition of a 'server.' For the basis of this explanation, lets call a server the object instance running on a machine. The machine the server is running on, we'll call the machine. You can have multiple servers running from a single machine where they can have different types of connections. For instance, you could have the unity client connect to the physics server using a UDP connection and connect the client to whatever else you needed using a TCP connection.
Photon server handle connect object called Peerbase. Each peer is each client connection. In client peer connection you only choose protocol is UDP or TCP.
Solution is create two peers, one is UDP and one is TCP but hard to handle what UDP and TCP peer is in one client to find player info and send data

Get ServiceDefinition.csdef from an Azure instance

I need to get the original endpoint configurations from the .csdef file, how can I do it?
I cannot use RoleEnvironment.CurrentRoleInstance.InstanceEndpoints, because it gives me the internal/local port in the virtual IP. But I need to know which is the port is being shown to the public.
For example I have a FTP server with a tcp 21 end point, but when I check at runrime to that collection it can has something like tcp 5100. When I bind the FTP server to tcp 5100 and access the server through, for example, 127.0.0.1:21 it works perfectly.
But for example, when I need to establish the PASV mode, I have to send back the IP and port where the client should connect. I'll work out the IP, but how could I know the port that has been configured for PASV? the port in the previous collection is not ensured to be the same that is exposed in the public interface.
So basically, how may I get the original .csdef configuration?
Cheers.
I'm not sure if this works with FTP but when using HTTP, it will returned the inner address and port if using HttpContext.Requst.Url. I was using Header["Host"] instead. Hope this helps.