C# Socket.Listen(MAX_CONNECTIONS); - sockets

i need some help with socket.listen.
my max_connections is set to 1. but even after a client is connected if another client tries to connect, on the client side it says it has connected although the server does not report anything new.
my app is between one server and one client. and if any other client tries to connect while there is already a connection i want that connect to be refused.
please help with some ideas.
thank you very much.

You didn't supply any code, but the title of your post references Socket.Listen. The parameter given to Socket.Listen is not the maximum number of connections, rather it is the size of the "backlog" of incoming connections.
What that means is that when someone tries to connect, but your server hasn't Accept()ed the connection yet, those clients are in the "backlog" queue. You've set the size to 1, so only 1 client can be waiting to connect at a time.
This parameter does not have an effect on the total number of connections allowed to your application.

You can use the IsConnected property on your TCPClient to check if a connection already exists and decide upon that.

I propose you to accept new client connection but acquire the semaphor immediately before access to wrapped server and release it immediately after access. This approach allows you to control how many clients are using wrapped server concurrently.

Related

client/server socket reconnection

I developed a client/server application based on sockets.
The client side is in Delphi. The server side is on an IBM I (as400)
Sometimes, the client and the server get disconnected. I'm not really sure why, but I think it's because of a machine between them (a proxy, a router, a firewall) sending a RST packet.
Anyway, I'm trying to reconnect the client with the same process on the server. (not another one, the same, that's important).
To do that, I create a new connection from the client. So, I have two processes on the server. I'll call them the "LostProcess" and the "HelperProcess".
The LostProcess is waiting for data in a data queue.
The client tells the HelperProcess that it was connected to the LostProcess.
The HelperProcess sends data to the LostProcess (via the data queue).
The HelperProcess makes a giveDescriptor, and the LostProcess makes a takeDescriptor.
Then the HelperProcess stops and the LostProcess sends data to the client (to say “I'm back”).
So far, it works, but when the client sends data , the LostProcess (we can call it the RebornProcess now) never receives them (I tried not to stop the HelperProcess, and that he is who receives the data).
With Wireshark, I could see that the client sends data with a different local port, so I guess that's why the RebornProcess does not receive them.
I tried to force the local port of the new client socket to be the same as the first one, but then the new client socket cannot connect for a while, and if I wait long enough, I have the same problem as before.
Does somebody have an idea how to make the reconnection work?
What you are doing is generally not possible. Once a TCP connection has been lost, it is gone forever. Both apps must close their respective sockets for the lost connection, and the client app must create a new socket connection to continue exchanging data with the server.
If the client app wants to reuse the same local port via bind() (which is generally not advisable in most cases), but does not want to wait for the OS to release the port first, then the client can enable the SO_REUSEADDR option via setsockopt() on the new socket before calling bind() and connect().
Pretty sure the answer is you can't.
There'd be all kinds of security issues if TCP/IP allowed a new connection to reconnect to an existing processes connection.
You should have the lost process terminate and just use the new process instead.

Strophe + Openfire connection disconnected repeatedly

I am using Openfire 3.9.3 and had created web chat application with strope.js. When the less number of user on openfire were there the connection was stable. but there are lots of users registered on that app.
Users are not online still my connection disconnected repeatedly I again connect on_disconnected(). The connection is made but again it get disconnected with
NetworkError: 404 Invalid SID. - http://127.0.0.1:7070/http-bind/
is it client side defect or server side defect?
How to solve this?
Openfire generally pings the client on a regular interval to check if the client is alive. When the client fails to respond within the specified timeout, the server infers the client has disconnected.
The error you are getting is page not found error which basically means that either there is some problem with your listening port or network.
However since you are using loopback address, the network possibility is ruled out. Check your server port as I (even though the code is not given for reference) feel that your client may not have any problems. Just make sure the binding address is up and right.
I had the same problem in my code (I'm using my version of Candy Chat), and the problem was I wasn't calling the BOSH _proto version of _doDisconnect in time.
_doDisconnect: function ()
{
this.sid = null;
this.rid = Math.floor(Math.random() * 4294967295);
window.sessionStorage.removeItem('strophe-bosh-session');
},
You must make sure this is called before your code calls the Strophe _changeConnectStatus function for a disconnect. Or at least set the current connection this.sid = null.
In other words, you have to make sure Strophe nulls the current "sid" value so the next ping sends no "sid" or a new one (I think none, but might be a new one). If not, your client side next "send" ping has the old value in it even though the server now disconnecting. Then the server gives the not very informative 404 error, which is actually letting you know that that connection is no longer valid (which of course is what I wanted in the first place). Sort of a which came first, the chicken or the egg problem.
Hope that helps a little with one version of why that error comes up. The standard version of Candy Chat resets everything before every connection attempt, so it doesn't have this problem.
update keepalive value at client side. default is 120 seconds.

When the Client application is manualy closed how to know it in server side, using C#

In a client server communcation, When the Client application is manualy closed how to know it in server side, using C#.
I have disconnect chances in two ways.
1. Manualy closing the client application
2. network cable is unpluging.
If any one have a solution please help me.
Thank you.
You know that by two ways:
recv/read returns 0. That means that the client gracefully ended sending data. The connection might still be open but the client can no longer send data. You can now close the connection if you wish.
You get an error telling you that the connection was interrupted.

What is the number of persistent connections to APNS allowed?

I am trying to write a server side code for sending push notifications for my applications. As per Apple recommendation, I am planning to retain the connection and send push notification as required.
Apple also allows opening and retaining multiple parallel connections for sending push notifications.
"You may establish multiple, parallel connections to the same gateway or to multiple gateway instances."
For this purpose I would like to maintain a connections pool.
My question is what is the limitation of connections pool, or the number of persistent connections with APNS can I maintain?
Thanks for anticipated help.
Don't know if you're going to get a precise answer to this one. As large and dynamic a system as APNS is, it behooves Apple to be ambiguous about such a number; it gives them liberty to change it at will. I found a similar vagueness here.
From this discussion it appears a rule of thumb is 15 connections max
One suggestion is to have an open-ended pool where new connections can be created until they start being refused. Just an idea.
I agree with #paislee, I don't think you'll get a precise number. I'm opening over 20 distinct connections simultaneously and there are ok.
In order to help you with your test, use TcpView, where it is possible to see every opened connection.
Regards

Better understand Read Timeout During SOAP Request/Response

I would like someone to clarify something for me:
There are two kinds of timeouts that exist during SOAP requests/responses:
1- Connection Timeout
2- Read Timeout
This applies at least to Axis1/Axis2, which I'm currently using.
The connection timeout happens when the client couldn't connect to the web service in question within the set Connection Timeout value, and which would eventually result in throwing the following exception :
Could not connect to host within a timeout of "value".
As for the Read Timeout, I'm really not sure about it, and I don't know which assumption is true. Let's take a scenario for example, in which a client is sending data to a web service, which will in turn process the data, checks for their sanity, inserts them into the database when they are, and then the web service will send some data back to the client. Bottom line, we have a significant amount of processing time on the server, and significant data that's being sent back and forth between the client and the web service.
What I'm unable to understand is when is a read timeout exception thrown by the client?
1- Could it happen when the client is still in the process of marshaling the objects that are being sent to the web service?
2- Could it happen during the process when the web service has already started writing its response to the open socket?
I could really appreciate clear answers on this. Thanks a lot in advance.
It's much clearer now thanks to the efforts I did to research this. A "Read Timeout" is basically when the client hasn't gotten anything byte of date still. So let's take a scenario where a server needs to reply back to a client with 4 MBs of data. Read Timeout will be reset with every byte of data the client is receiving from the server.