0:11
welcome to controllers tech
0:14
this is yet another video in the stm32
0:17
ethernet series and today we will see
0:19
how to create a tcp server
0:22
as mentioned in the poll i will cover
0:24
the raw api first and this is why this
0:27
video will cover the tcp server with raw
0:32
i have already covered the udp server
0:35
and client parts and you can watch them
0:37
in the ethernet playlist
0:39
so let's start the cube id and create a
0:47
stm32f750 discovery board
0:50
give some name to the project and click
0:56
i am skipping the setup part and you can
0:59
watch the previous videos for the same
1:02
check the ethernet playlist and watch
1:04
the connection videos
1:07
so this is the main file
1:09
first of all i am going to test the ping
1:12
just like the udp videos
1:15
let's build and run this
1:40
so the ping test is successful and we
1:43
are good to go for the tcp server
1:50
here are the two files for the tcp
1:52
server and let's copy them in our
2:03
let's take a look at the tcp server
2:07
i took these files from the examples
2:09
provided by the st and modified them a
2:13
so some of the functions should be kept
2:16
i will explain the ones which we can
2:20
here are the steps given to create the
2:24
you can check out this website for more
2:27
first step is to create the tcp block
2:31
we can do that by calling tcp new
2:40
then we need to bind the tcp block to
2:44
here we will do that by first converting
2:46
the i p address into the integral format
2:49
and then by calling the tcp bind
2:57
let's change the server port to 10.
3:09
the next step is to listen for the
3:13
here we will put the server in the
3:19
to do that we will call the t c p listen
3:27
at last we will initialize the accept
3:29
callback function which will be called
3:31
when the connection will be accepted by
3:37
tcp accept initializes the callback tcp
3:41
server accept and now we will take a
3:43
look at this function
3:45
this function will be called when the
3:47
connection from the client is accepted
3:50
here it will initialize few more
3:52
callbacks and we will keep them as it is
3:55
except this one tcp receive callback
3:59
this function initializes the receive
4:01
callback which will be called whenever
4:03
the server will receive the data from
4:09
tcp server receive is where we will
4:11
write our program to handle that data
4:20
here few things are predefined
4:23
like if the server receives the empty
4:25
frame we will close the connection
4:28
the code is commented pretty well you
4:30
can read about the rest
4:37
i will just get to the main part which
4:40
when we will receive the data for the
4:42
first time the state will be set to
4:46
here we will change the state to
4:48
received and also initialize the tcp
4:57
after initializing the callback we will
5:00
call the server handle function which
5:02
will handle the received data
5:06
the tcp send callback should be
5:08
initialized only once and this is why
5:11
the accepted state must execute after
5:13
the first receive only
5:15
from the second receive onwards the
5:17
state will be set to received and this
5:20
particular part will execute
5:22
here we will simply call the server
5:24
handle function to process the received
5:38
let's take a look at the server handle
5:41
this is where you will modify the
5:43
program according to your requirement
5:46
first of all we will get the client's ip
5:51
this is just in case if you want to make
6:00
the ip address is in the integral format
6:03
so we will use this function to change
6:12
then i am copying this data into the
6:14
buffer which i will later send to the
6:17
it's basically the incoming data
6:20
followed by this string
6:22
the incoming data is stored in the
6:24
payload of the p buff
6:26
then we will copy the data into the p
6:28
buff update the length to be transmitted
6:31
and send the data using the tcp send
6:43
let's write the main code now
6:48
first we will include the header file
6:51
then inside the main function initialize
7:00
like i mentioned earlier it will
7:02
initialize the rest of the things
7:05
this is it let's build it once to check
7:09
we have some warnings but they are due
7:11
to the unused variables so it's all
7:15
let's debug the program now
7:18
i am using the hercules for the tcp
7:36
let's see the calling pattern for the
7:40
i am setting a break point inside the
7:42
tcp server accept callback
7:49
another one inside the receive callback
7:56
one more inside the tcp sent callback
8:05
and the last one inside the connection
8:11
let's run it now and see the order of
8:25
in the hercules we will connect to the
8:27
server i p address and the port
8:39
and here we got the first hit in the
8:43
this callback is called when the
8:45
connection between the server and the
8:49
and as i mentioned it initializes the
8:51
rest of the callbacks
8:54
here we got the message about the
8:57
now let's send some data to the server
9:03
and we got a hit in the receive callback
9:06
here we process the data and send
9:08
something back to the client
9:11
once the client receives the data it
9:13
sends the acknowledgement and we got the
9:15
hit in the send callback
9:18
also note the data it sends the received
9:20
data along with some additional data
9:32
here we have again the break point is
9:34
called in the receive function and now
9:37
we will send the data to the client
9:46
let me disable the breakpoints
9:55
here we have the data we sent along with
10:02
this time it got corrupted
10:09
well you can take care of it when it
10:13
i think we should free the buffer after
10:17
you can try it if the data gets
10:21
let me remove these breakpoints
10:24
i will leave one here inside the tcp
10:26
server handle function
10:32
here we can check the i p address of the
10:35
this address is in the integer format
10:39
the client port is as shown here
10:46
after using this function to convert the
10:48
address to the string we can see the
10:58
if we do the ip config on the windows
11:00
machine we can see the same ip here
11:03
so we are able to fetch the client ip
11:06
its port the data client sent and also
11:09
we were able to send some data back to
11:12
this is it for this video
11:15
i hope you understood it
11:17
i myself don't know much about the
11:19
protocols yet so this is best i can do
11:23
in future after learning about these
11:25
protocols i will release a better
11:27
version where i will make a practical
11:29
application using these protocols
11:32
you can check out the previous videos
11:34
for the connection process and the udp
11:37
client and the server
11:39
the next video will cover the tcp client
11:44
you can download the code from the link
11:48
leave comments in case of any doubt
11:51
keep watching and have a nice day ahead