Access to server without writing port after address - c

I'm creating my custom http server in C. sockaddr_in looks like this:
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(8080);
my_addr.sin_addr.s_addr = htol(INADDR_ANY);
And my question is how I can send my request without writing port after address.
For example 192.168.1.100 instead of 192.168.1.100:8080 or mydomain.loc instead of mydomain.loc:8080

how I can send my [HTTP] request without writing port after address[?]
You cannot communicate with a TCP endpoint without designating a specific port. Various kinds of services have conventional (default) ports, however, and oftentimes client software will use a service's conventional port if the user does not explicitly specify one.
The conventional port for the HTTP protocol is 80. If your server runs on that port, then it is likely that HTTP user agents such as web browsers will not require you to specify that port; instead they will silently insert it for you. You will need root / administrator privilege on the server machine to run the server software on port 80, or on any other port less than 1024.

If you want to access your custom server with any web browser then you must have to specify the listening port of your custom server because every browser will consider a server listening on port 80 on the address (URL) you provided.
how I can send my [HTTP] request without writing port after address[?]
And if you want to provide only the host IP address and using the 8080 port as default listening port of your server, you can make a simple custom client. Pass the host URL i.e. 192.168.1.100 to that client. In the body of client, connect the socket to 192.168.1.100 and 8080 address. Client will send query to your custom server and will save the reply in .html file. You can then open this file with browser.

Related

How does a client know which port to send data after accept()

Let's say we have a server that can accept multiple clients. First, it has to create a socket, then bind it with a port and an IP and finally listen to requests for connection from clients. After accept()ing a connection with a client, the server creates a new socket to communicate with the specific client. My question is whether or not the client is going to send its data to the same port it sent its initial request to, and if not how does it know where to send it?
A socket connection is uniquely identified by a tuple of [Protocol, Local IP, Local Port, Peer IP, Peer Port].
A TCP server creates a listening socket with a tuple of [TCP, Listen IP, Listen Port, 0, 0]. When a client requests to connect to a server, the network routes the request to the specified IP/Port. The receiving device then routes the request to a matching listening socket, performs a 3way handshake with the client, and puts it into a queue. Later, when accept() is called, it extracts the next pending client from the queue and returns a new socket identified with a tuple of [TCP, Listen IP, Listen Port, Client IP, Client Port]. Because of this, a single listening socket can accept multiple Clients from different Client IP/Port combinations.
A TCP client creates a connecting socket with a tuple of [TCP, Local IP, Local Port, 0, 0]. When the 3way handshake is complete, the socket's tuple is updated to [TCP, Local IP, Local Port, Server IP, Server Port]. Because of this, a Client can connect separate sockets to different Servers at differing Server IP/Port combinations.
All subsequent data exchanges use these tuples.
Data sent out from a Client's connecting socket will be sent to the associated Server IP/Port and stored in the buffer of the accepted Server socket whose tuple matches both the Client and Server.
Data sent out from a Server's listening socket will be ignored, since there is no associated Client.
Data sent out from an accepted Server socket will be sent to the associated Client IP/Port and stored in the buffer of the connected Client socket whose tuple matches the Client and Server.
Generally There is always a default port allotted for each kind of communication.Operating System may kept it open or close ,it can be checked .
Let's say for FTP connection, There is a separate port allotted for handshake,It don't matter how many new FTP connection are being requested, all new connection will go to that same port , Once handshake is completed data exchange is done via another port, Even if we don't specify port. If Network manager has Port List entries earlier it will request to the same port.
Example for SSH
if you request for
ssh -X <IP>
Even if you don't mention port , Your system know which port to request for and at server side there is always some port open who will be listening to your request and based on data you send while handshake it will continue listening or block you.
Bonus is you can open your custom port at server side who will be listening to your request. TCP implementation by default declare which port will be used for what kind of communication.
The client connects with a source IP and port to a server with a destination IP and port. After accept exactly the same IP and port on both sides are continued to be used for data exchange as for the establishment of the connection.

WSAConnect - the remote server responds on both port 80 and 443

I use WSAConnect to connect to a server whose site has the prefix http: \ That is, it seems like the remote server should connect only on port 80. But, in fact, I see that for some pages of this http site - WSAConnect completes successfully on port 443.
Is it okay? Is this allowed?
WSAConnect is for low-level socket communication, for example low-level TCP/IP sockets.
Web servers use HTTP for communication with clients, such as browsers. HTTP is an application protocol on top of TCP/IP.
By default web servers use port 80 for plain HTTP communication. Webservers can also support HTTP over a secure connection (HTTPS). The default port for this is 443.
So it is perfectly fine for a web server to be reachable on port 80 and 443.
In general, a single server can be reachable on any number of ports. Different ports are used for different protocols.

Get IP address of the client in C sockets

How can I get the ip address of the client of which my server is connected to?
Here is how I accept incoming clients:
newsockfd = accept(sockfd, (struct sockaddr*)&clt_addr, &addrlen);
I tried the following:
printf("ip is: %s\n", inet_ntoa(clt_addr.sin_addr));
But I am getting 127.0.0.1 which is not my ip address. Could it have something to do with me using localhost to test my client and server?
Could it have something to do with me using localhost to test my client and server?
Yes. If you connect to a server on localhost the client will also be localhost. This connection is not happening over the Internet, there is no network hardware involved, the client packets will come from localhost. This is known as loopback and occurs entirely in software.
If the client were to connect to the server using the server's external IP address it will have to do a connection via external routing and thus require a routable IP address. For example, I can connect to a server on localhost, but I cannot using my routable IP address because my router blocks incoming connections.

Getting the port number of a server through a browser

I have a server.c I can connect to it if I know the port that I assign to the server; lets say 6000. I go to FireFox and type 127.0.0.1:6000 and I can connect perfectly.
How do I build my server that a client will type 127.0.0.1 on a browser and then the server will assign the port or notify the user on the browser to type the port?
Perhaps I don't fully understand servers but it seems to me that the client must know the port from the get-go.
You need a port to connect. HTTP connects to port 80 by default and that's why you can get away by typing 127.0.0.1 into your browser.
The default standard port is 80 for an IP in a browser. Maybe your program (server.c) waits a request with the default port 80, and transfer the request into another user-defined port.

sent data to paticular socket from server when have multiple client

I have 4 different application running.so every application can send data to server. now i want to send data to paticular socket (server and client are done through socket programming)
from server side.
When you create a socket, first you establish a connection between a server and a client (using connect system call on the client side, and bind, listen, and accept system calls on the server side). You can have many such connections, from a server to different clients. The server can send data on any of these established connections.
The Sockets Tutorial can assist you in this case.
If you want multiple reads/writes by the server to happen at the same time, you have to use non-blocking sockets or multiple threads.
(Assuming you're using TCP/IP, or UDP).
A socket endpoint is the IP address and port number combined. So, on your host you would have your various servers listen on different port numbers. For example a web server may listen on port 80, ssh on a different port etc.

Resources