HAN Pilot Platform
Demonstration Manual
57
www.terasic.com
September 6, 2019
Internet, an address consists of a port number on the host machine.
Listen for connections with the
listen()
system call
Accept a connection with the
accept()
system call. This call typically blocks until a client
connects with the server.
Send and receive data. There are a number of ways to do this, but the simplest is to use the
read()
and
write()
system calls.
Example Code Explanation
The example design contains two projects. One is socket server project, and one is socket client
project. The SOCK_STREAM socket type is used in the design. The Linux Socket Library is used
to provide socket functions, so remember to include the socket API header file – socket.h.
The major function of socket server program is to create a socket server based on the given port
number and waiting a client to request to establish a connection. When a connection is established,
the server is waiting for an incoming text message. When a message is received, it will show the
receiver message on the console terminal, then send the message “I got your message” to the client
socket, and then close the server program.
shows the socket relative code statement. In
the program,
socket
API is used to create a SOCK_STREAM socket,
bind
API is used to bind the
socket to any incoming address and a specified port number. For connection,
listen
API is used to
make the socket as a passive socket that is, as a socket that will be used to accept the incoming
connection, and
accept
API is used to accept the incoming connection. The
accept
blocks until a
client connects with the server. Data receiving and sending is implemented by the
read
and
write
API, and
close
is used to close the socket.
Figure 3-20 Socket Server Code
The major function of the socket client program is to create a connection based on given hostname
(or IP address) and host port. When a connection is established, it will show “Please enter the
message:” message on console terminal to ask users to input a message. After get user’s input
Содержание HAN Pilot Platform
Страница 1: ......
Страница 92: ...HAN Pilot Platform Demonstration Manual 91 www terasic com September 6 2019...