Networking - C Server-Client Communication in Linux

in #networking7 years ago (edited)

    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!

Server-Client Model:

    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.

TCP 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:

Server:


Client:

By tweaking these Codes you can create any Server-Client TCP Program you like!


UDP Connection:

    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: 

Server:


Client:


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 :)

Bye!

Sort:  

The "mistake" was not closing the socket using close()

Coin Marketplace

STEEM 0.20
TRX 0.20
JST 0.034
BTC 89254.99
ETH 3064.10
USDT 1.00
SBD 2.92