In socket programming communication between client and server.
when the client sends a message to the server.
Can the server know or determine the hostname of the client?
Does it has to be passed manually by the client?
The normal way to pass message to server from client is write(fd, clientMessage, sizeof(clientMessage));.
Suppose there is some header information to be shared also like say hostname.
Does that has to be passed like this only? Or there is some better alternative?
Can the server know or determine the hostname of the client?
No. The server has only the clients IP address. While it can try a reverse DNS lookup the information returned by this are unreliable, i.e. there might be no DNS PTR record setup in the first place or the PTR record might just claim an arbitrary mapping between IP and hostname, like claiming to be google.com.
Does it has to be passed manually by the client?
There is no automatic way in TCP to do this. But like with a PTR record - the information is not reliable since the client can claim to be anything.
The normal way to pass message to server from client is write(fd, clientMessage, sizeof(clientMessage));. ... Does that has to be passed like this only ...
It does not matter how it gets passed. All what matters is that client and server agree on the same way, so that the server can actually extract the information from the data.
Related
We are optimizing our Web System and I then had a thought.
In the web system's Web.config, should the connection string include an IP Address to the local SQL Server or rather an Instance? Will there be a performance difference?
I was thinking that with an IP address a formal IP or TCP connection would be established and perhaps with a local Instance reference a different protocol is executed?
I couldn't find related information on the web.
Using an instance name such as SomeMachine\SomeInstance means the client needs to look up the port using SQL Browser.
For best performance it would probably be best to use an IP address and port number. You must set a static port for the instance, otherwise it won't connect.
Ideally use a DNS name rather than an IP address, otherwise you keep having to change it. So you want something like SomeMachineDnsName.YourDomain.com,1433.
Say I have a public server on the Internet, written in C, that is connected to two independent clients. Both clients initiated the connection. One client is (e.g.) an iOS app, the other is a native Windows app.
The purpose of my server is to allow these clients to send text messages to each other. Client 1 sends a message, the server receives it, and then forwards it onto Client 2. The same thing happens in reverse when Client 2 sends a message.
This feels inefficient. What I would really like is for both of these connections to contact the server and then for the server to connect these two clients directly to each other - after which my server can forget both clients as they are responsible for communicating with each other. My server is then free to connect up other clients in the same way.
My question is: is this even possible (with TCP and/or UDP)? Neither client necessarily has a public IP address, which is why they have to initiate the connection. Once the connection is established however, my server knows the connection address of both clients. Is there a way to connect them together? A syscall that can do this sort of thing, perhaps?
No, you can not join two existing TCP connections to one.
There are 2 options:
1) Keep your server in the middle as it already is. It should not be problem, if you don't have thousands of clients under one server.
2) Server could send control messages to clients, and order those open new TCP connection between each other. Server should also make decision which of the two clients must be initiator of the new TCP connection.
Option 2 is problematic, because client may have firewall rule that prevents in coming connection to the wanted port. Also NAT would cause problems.
Background
I am using a SparkCore wireless arduino board to connect to a local Node.js server. The server includes a local intranet TCP server that a TCP client programmed onto the SparkCore connects to.
Problem
If I run the server on a different network, the server has a different local IP address. When I do this, I have to reprogram the SparkCore arduino to tell it the new local IP address of the server to connect its TCP client to. This is not ideal for a variety of reasons.
Question
Is there a way to have the client dynamically search for the TCP server or alternatively have the server broadcast to TCP clients in a way that would inform the client of the local IP address to use for the server without initially hardcoding it? I would love to do this in way that did not involve iterating through a bunch of IPs on a specific port to see if a connection is made. That being said, if that's the only way to do this, then so be it.
How is the arduino booting? If it's booting using DHCP, one method would be to provide a customer DHCP option that provided the address of the node.js server. ntp, for instance, can configure itself in a similar way. This has the advantage that the arduino need not be on the same local subnet as the node.js server.
An alternative (slightly disgusting) would be to use an A record within your domain (let's say nodejs.example.com. Configure the local DNS recursive server to explicitly return this value (I am presuming you might have lots of different deployments with lots of different nodejs servers).
A third possibility would be to send out some form of discovery packet, either by broadcast, or better by multicast UDP. Assuming it's on the same LAN, the nodejs server could then reply. Clearly you might need to concern yourself with a rogue server impersonating your nodejs server, and therefore might need to add some security (e.g. use a shared secret, send a random nonce plus the nonce hashed with the shared secret to the server, the server checks the hash, and replies with the answer, the nonce, plus the answer hashed with the shared secret and the nonce, each of which the client then checks).
Hi I really new to tcp socket programming I want to connect multiple client to one server using thread and once each client connect the server. we able to view that who(client) is connected to this server.
I was thinking to create the struct to hold client record on both client.c and server.c and once client is connected we will use send(sock , (void *)&package , sizeof(struct USER) , 0 to passed structure to server.c and in server.c we also create same struct once server receive data recv(sock, (void *)&package, sizeof(struct USER), 0);server will store those data into struct but HOW? I try the result is when I connected multiple client one called User1 and other one called User2 then I try to print out the strut result is..
display on client 1
USER[0]: User1
USER[1]: (NULL)
display on client 2
USER[0]: (NULL)
USER[1]: User2
can anyone please help
Thank you so much
It's typical for multiple clients to connect to a single server. I'm not sure what information you're wanting to pass from each client to the server, but if it's something like the client's ip address or network settings, this is already provided by the client to the server when attempting to connect. A more in depth study of the TCP/IP protocol may be a good idea. There are a number of good tutorials and examples available.
If you're trying to share some other information over the network, once a connection is established, you can pass whatever you want over the sockets. This is all dependent on what your application is doing, which isn't specified.
I am working on udp server/client applicataion. Since for communicating with the server, all the clients must know the ip address and port number of the server. For this purpose, I have hard coded the ip and port number of my server to the clients so that everytime, the client connects to same ip and port number. (found the ip address of the server machine using ipconfig command.)
But now, the problem is that I am working on DHCP network, and there is a chance that everytime sever machine is restarted, a new ip address may be assigned to it (different from the ip address known by the clients at which they will connect.)
So, I always want the ip address hard coded at client side to be assigned to the server machine, everytime it logs in.
Is there any way to do it? I have no idea about it. Searched internet but couldn't find anything relevant.
Looking forward to help :(
Assuming that your clients are local to the server, why not abandon the hard-coded server IP address, and borrow a page from DHCP and use some kind of service discovery method:
Your clients broadcast "where is the server" message when they first come online. The server responds with "I am at IP address X.X.X.X"
When the server comes up, it broadcasts "Server is now at IP address Y.Y.Y.Y" so that if the server crashed, the clients start using the new server.
Presuming you are working on a LAN, that's how I'd do it.
Presuming your DHCP server is configurable enough:
Assign a static map MAC address/IP address in the dhcp server, so
that the same machine always get the same IP (just for the server,
not for every client).
Most entry level all in one devices with DHCP have this functionality, if not it should be quite cheap to buy a new one that has it.
If your DHCP server is a real computer, you can surely configure it to do so.
Additionally you might want to tell your clients to use a local DNS and in this local DNS server define a name for your server, so you won't have to hardcode an IP address in your clients. But the address should be located in some configuration file rather than hardcoded in any case.
I have used dnsmasq to serve as both DNS server with local names, and as DHCP server, giving the servers always the same address and pointing all the DNS requests towards itself.
This questions could be useful to find a windows alternative for dnsmasq: https://stackoverflow.com/questions/7709744/is-there-something-like-dnsmasq-for-windows
By adding a reservation field in the DHCP server we can attain this. If you are using Windows DHCP server, there is a section named 'Reservations', there we can give the MAC address of your pc and the desired IP address. Then the server will provide the mentioned IP for you.
With the narrow focus of a developer a DHCP reservation might be the logical step. But using a nameserver is far better. If the network itself changes or maybe the server is moved to another subnet or maybe even into anoher zone, using an IP address from a DHCP reservation fails, because the server's address changes.
You don't have any of these problems if you use a nameserver. That is what DNS is meant to be doing. Think of it as a "serviceprovider finding service" that detaches your service from the host it is running on.
And, like already suggested, you should never hardcode an IP address or DNS name or anything else that might change (even if you think it will not change) unless it is a design goal that things aren't working anymore if something changes (=not configurable).