How do I establish a UDP connection after a TCP connection? - sockets

Hi guys I'm currently developing a C project in which we basically need to connect N clients to a server through a proxy. I'm doing the first connecting using the TCP protocol. After this, the client may request a file download using UDP. Do I need to have another socket listening to another port and connect the client again to this port or is this an incorrect way of thinking?

TCP and UDP communicate in very different ways. You'll need a separate socket for each
one.
You'll probably want to use the TCP socket to communicate the UDP port to use.

Related

How to program a UDP webserver since HTTP normally uses TCP?

Here is my understanding of how a client-server HTTP server works.
The client creates a TCP socket connection to connect to the server and sends data.
The server creates a TCP socket connection to listen for incoming requests.
So it looks like both the client and the server need to agree on the use of the Transport protocol to use (in this case TCP). But if we want a website to work over UDP/QUIC protocol then we need both the client and the server to create a UDP socket connection. But some websites use TCP and others use UDP...
So does it mean it would need to look like this? To know beforehand which protocol a website uses?
if (URI == 'https://www.google.com') {
// Website that works over UDP
client.create.UDP.socket
client.sendData
server.create.UDP.socket
server.receive.data
} else {
// Website that works over TCP
client.create.TCP.socket
client.sendData
server.create.TCP.socket
server.receive.data
}
So the client needs to keep a record of which website uses TCP and which websites use UDP/QUIC and create that kind of socket to communicate with it?
If a protocol supports both TCP and UDP, the server listens both on the TCP port and the UDP port. The port number is generally the same, e.g. DNS uses TCP port 53 and UDP port 53.
Usually the client has a preference. Let's say it prefers TCP. The client will first try to connect with TCP. If the server does not respond, the client will retry with UDP. Alternatively, the server can respond over TCP but ask the client to switch to UDP. The client can then decide to continue with TCP or switch to UDP.
For QUIC 1 2, the browser will first connect using HTTP over TCP. The server will respond with a message stating that it also supports QUIC. If the browser also supports this protocol, the browser will reconnect to the server using QUIC.

Can a server which acts both as RMI and UDP server listen to requests on the same port number?

I'm working on a program in which a UDP server keeps listening to incoming requests.
If a particluar kind of request is received, it tries to create a RMI server using the same port on which the UDP server was listening.
So, is this possible?
or I need to have different ports?
Yes it's possible. TCP and UDP port numbers are in different namespaces.

Changing 2 client sockets for a communication peer to peer without the server?

Is it possible this Scenario without using webrtc?
Client A connects to Server
Client B connects to Server
Server send information to B and to A with each others information
A and B start talking directly using the same connection (by transforming their sockets info with the new port/ip address for example)
note: Client A and B doesn't have any socket server. They are only clients
Thank you very much.
It's definitely possible to set up a direct TCP connection, if at least one of the clients can accept an incoming TCP connection. In that case the server can just tell the other client what IP address and port to connect to, and then the two clients can communicate directly over the new TCP connection.
If both clients are behind firewalls or NAT, on the other hand, things get a lot more iffy -- in some cases you can use TCP hole punching techniques to get a direct TCP connection started; in other cases you're just out of luck.
In no case (AFAIK) can you modify an existing TCP connection to have new endpoints; a TCP connection's endpoints are fixed, and to get new endpoints you have to set up a new TCP connection.

p2p sockets table

I'm creating a socket table from a network composed of 2 hosts. They have a p2p connection and they're working with tcp protocol. Do I have to create a welcoming socket in both of the hosts (as they act like servers)? And a receiving socket as well as a sending socket for each host? Or just one for sending/receiving would be ok?
Each host will need a listening socket on which it will accept new connections. It will also need one socket for each inbound TCP connection (returned from the accept call when it accepts an inbound connection) and one for each outbound TCP connection (returned from the socket call before calling connect to make the outbound connection).

Can TCP and UDP sockets use the same port?

First of all, is there any problem with using both UDP and TCP on the same server?
Secondly, can I use the same port number?
Yes, you can use the same port number for both TCP and UDP. Many protocols already do this, for example DNS works on udp/53 and tcp/53.
Technically the port pools for each protocol are completely independent, but for higher level protocols that can use either TCP or UDP it's convention that they default to the same port number.
When writing your server, bear in mind that the sequence of events for a TCP socket is much harder than for a UDP socket, since as well as the normal socket and bind calls you also have to listen and accept.
Furthermore that accept call will return a new socket and it's that socket that you'll then have to also poll for receive events. Your server should be prepared to continue accepting connections on the original socket whilst simultaneously servicing multiple clients each of which will be triggering receive events on their own sockets.
Firstly,there is no problem using both tcp and udp on the server.
Secondly,we can have both UDP and TCP requests on same port ,because each request is identified by a quintuple contained by source IP ,Destination IP, Source Port, Destination Port, PROTOCOL(as protocol can be TCP or UDP).