Hello its me again drifter1! Steemit.com has some problems and thats why I couldn't upload yesterday. Today we will continue our previous talk about Sockets getting into Server-Client Communication implemented with TCP and UDP Clients and Servers ! So, without further do let's get started!
The Internet works using the client-server model. Some computers called servers contain the information that the clients request from the servers. When a server is available then the client will get a response. This response will be splited into packets or datagrams with the same size based on the protocol limit. Last time we already talked about 2 different ways a client and a server can connect. We can use the connectionless UDP protocol or the connection-oriented TCP protocol. I also told you the steps that are needed to create each kind of server or client and in which cases each of the protocols is more useful or has a better performance. But, I didn't showed you the code, but only the libraries, functions and datatypes that we will use!
Let's now get into how we implement a TCP (after that UDP ) server-client connection.
When using TCP we have to make a real connection as we already said last time. So, the client will setup a socket and then connect to the server before starting to send/receive data. In the same way a server will setup his socket by also binding it to a specific port and listening for connections. The server will then accept a incoming connection (if he is available) and start receiving/sending data. We will create an local internet connection by using "127.0.0.1" as the server address.
So, the basic Coding Layout for TCP looks like this:
By tweaking these Codes you can create any Server-Client TCP Program you like!
When using UDP we don't need to create a real connection, but just send to and receive from a specified address. So, the client will simply setup a socket and then send or receive from the server. The server will setup a socket by also binding to a specific port/address and then start receiving/sending. I will again use the localhost.
So, the basic Coding Layout for UDP is:
And this is actually it!
Next time we will get into Server-Client Kinds/Types! Cause, we can do a lot more stuff then simply receiving.
I also made a mistake in the codes and I already mentioned it last time. If you know what I mean write a comment with the answer, I will write the answer in some days in the comments :)