Socket programming - C - choosing connection - c

I am trying to do a web server that will provide a set of questions (poll) to the users trying to connect to it.
The only issue is that I do not want to send everyone the questions, I would like to choose which connections to accept. (for example, a simple algorithm that chooses only even numbered connections, in the order they connect). To the other ones I would send a message saying you have not been picked.
I am using select() function to handle multiple connections.
Any ideas on how I can choose this? Good to mention that I am doing this in C language.

You can not skip a connection without accepting it, as they are queued by TCP protocol stack. What you'd have to do is to accept the connection, send an error message and than close the connection.

Related

Why is there no extra channel for error or status?

I have a question on Client-Server-Computing.
Why is there only one connection from the server back to the client? In UNIX you normally have stdout and stderr.
Background:
Database-queries might take a much longer time than you expected.
Then you wonder if there is something wrong. Maybe the server is stuck in an endless loop. This can easily be the case because servers nowadays can be extended via procedures, triggers etc.
If there was an extra port for sending status messages from the server to the client the user could get the information "everything ok" e.g. via "executing node number 7 of the query execution plan".
These users who only would be puzzeld by such information could keep the message window closed.
Is there a real technical problem or need those responsible for TCP standardisation just a hint?
TCP is a generic transport protocol and does not distinguish between different semantics, like status, error, data, ... Such semantics are added by the application protocol on top of TCP.
To provide different semantics it is not necessary to have different TCP connections. One could easily define an application protocol which allow messages with different semantics to be transferred over the same TCP connection. And such protocols exist, for example TLS (handshake messages, application data, alerts ...). But one could also do multiple TCP connections, like in FTP with different TCP connections for control and data.
So the question should be instead why a specific server application does not have the capability for status updates in parallel to queries. It is definitely not because of limitations from using TCP as transport layer, but because of limitations in the application itself.

Detect SSL protocol in C

I'm writing a server which is accepting incoming TCP connections and I would like to implement a simple way in C to detect if connection is SSL just inspecting few received bytes. Any help?
It is possible since the first message after the connection is established should be a "hello" message from the client. The first field in the message is the SSL/TLS version, then a timestamp follows - depending on the application layer protocol a plain client uses, this may be enough to figure out if the client connecting uses SSL/TLS or not.
See https://www.rfc-editor.org/rfc/rfc5246#section-7.4 for more details on the message structure.
Edit: here's a very similar question with an excellent answer: https://security.stackexchange.com/questions/34780/checking-client-hello-for-https-classification

socket programming

i have server has ip and three ports , and has more sender (each sender has ip) to this server , and one reciever (has its ip). if sernder A and sender B send to server , if i want the receiver receive the data only sent from sender A (how do that by using socket programming in silverlight). i use ( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ). plz help me
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Your message is very confusing based on punctuation, lack of Capital letters and spelling problems. But anyway.
You can't prevent on sockets level connection from specific client that you have to know IP of which or have kind of handshake after connection is established to understand which client it is.
You can do block connections from specific IP's using firewall hardware / software.
In same time, I believe it is just software task you trying to do.
So you will have some kind of identification after connecting.
Lets say server is Bound, awaiting for connections, and once they are accepted, you get new client Socket that wont be authorized before it will go through some messages exchange that will do authentication connection. If that is not done by some time, server may drop connection.
This looks like very basic task and questions you are trying to go through, I would recommend to read some basics about sockets programming.

Distributed Networking Multiple Clients

I'm currently working on a distributed networking project for some networking practice and the idea is to send a file from my server to a few different clients (after breaking up the file) and the clients will find the frequency of a string and return it back.
The problem I'm running into is how to identify each client and send data to each one.
The solution I've been working on to identify each client by their port. The problem arises as to how I handle multiple connections and ports. I know I have to use send() to send the data to a port once I open a connection and etc. but I have no idea how to do this across multiple connections ( I can do this with a single client and server but not with multiple clients)
Does anyone have any suggestions from a high level standpoint? I got one suggestion from a friend who said:
Open a socket
Listen for connections
When a connection request is received, spawn a new thread to handle the connection.
The main process will go back to step 2 to listen for new connections, while the new thread
will handle all data flow with the associated client.
But I'm not really sure I understand this... I've also been referencing http://shoe.bocks.com/net/#socket
Thanks
Your friend is correct. Follow first three steps (mentioned by him) and then you need to:
After spawning thread, send data (read from file) to new socket.
Once entire file is finished, you should disconnect and exit thread. On client side, you should handle disconnect and probably exit.
NOTES:
Also, you can use sendfile() instead of send() if you wish. You can use select() if you wish to handle all connections without spawning threads.
Refer http://beej.us/guide/bgnet/ for details.
EDIT:
how to identify each client? Ans: This is classical port discovery problem but in your case its simple. Server should be listening on well known port (say 12345) and all the clients will connect to it. Once they are connected, server has all sockfds. You need to use these sockfds to send data and identify them.
If you check out networkComms.net, an open source network communication library, once you have created a connection with a client you can keep track of that specific client by looking at it's NetworkIdentifier tag, a guid unique to each client.
If you will be sending large files to all of your clients also check out the included DistributedFileSystem which is specifically designed for that purpose.

Opening Multiple TCP Connections From A Client To A Server Using Random Source Port In C

I am writing a C program where for every new request to be processed by server, I need to open a new TCP connection? That is every request from client needs to be handled by a separate TCP connection to a server listening on a particular port.
Can someone help me in code pointers?
How to maintain array of this socket identifiers (multiple sockets that needs to be opened)
How will I be able to read (Need to scroll through all open sockets and see if something interesting is to be read upon that socket)
Any code snippet will be highly useful?
You can use the select() function (assuming you are working with <sys/socket.h>), "gives you the power to monitor several sockets at the same time. It'll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions" from http://beej.us/guide/bgnet/ (here you can download a pretty good book on network programming basics).
For a server example using select check http://beej.us/guide/bgnet/examples/selectserver.c
Hope it helps
If you need to save state with the socket, put it in a struct together with the data you need, and link the structures together as a list. For checking which sockets are ready see Maluchi's answer.

Resources