-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
Description
Create your own branch and do the following.
Introducing data structure in this socket communication
In socket communication, the server and the client must have the standard of data structure to establish a good connection and manage the data properly. We can represent the data structure by using C struct, here is our data structure in this exercise:
typedef struct {
uint16_t len; // the length of data to be sent
char data[1]; // must be "struct hack"
} packet;
Create a socket server.
- With this command
./server 127.0.0.1 8000
it must be listening on address127.0.0.1
and port8000
(should work for other addresses and ports). - Must be able to accept a connection.
- If there exists a client connects to your server, accept it and read the packet from a corresponding file descriptor, then reverse received data and wrap it with
##
and write back the reversed and wrapped data to the client. You should write the received data and the length of data to stdout. Read the response example for the details. - Write your code in a file named
server.c
.
Create a socket client.
- With this command
./client 127.0.0.1 8000
it must be opening a connection to server127.0.0.1
and port8000
(should work for other addresses and ports) - The client app should ask for the input from stdin and send the given data to the server.
- After the client has sent the data, it should wait for the server response and print the received data and data length to stdout.
- Write your code in a file named
client.c
.
Response Example
- The client sends data
qwerty
(6 bytes) to the server. - The server receives data
qwerty
from the client. - The server reverses the received data to be
ytrewq
and wrap it with##
so the data will be##ytrewq##
. - The server gives a response
##ytrewq##
(10 bytes) to the client.
Consider the following good looking sample
Server init (listen and bind to address and port)
$ ./server 127.0.0.1 8000
Listening on 127.0.0.1:8000
Client connects and sends data
$ ./client 127.0.0.1 8000
Connecting to 127.0.0.1:8000...
Connection established!
Enter your data: qwerty
Sending data to the server...
xx bytes sent!
Waiting for the response...
Server receives a connection and send response
$ ./server 127.0.0.1 8000
Listening on 127.0.0.1:8000
Accepting client (<client_ip>:<client_port>)...
Waiting for client data...
Received data "qwerty" (6 bytes).
Sending response "##ytrewq##" (10 bytes) to the client.
xx bytes sent!
Client receives a response
$ ./client 127.0.0.1 8000
Connecting to 127.0.0.1:8000...
Connection established!
Enter your data: qwerty
Sending data to the server...
xx bytes sent!
Waiting for the response...
Received response "##ytrewq##" (10 bytes) from the server.