You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
typedefstruct {
uint16_tlen; // the length of data to be sentchardata[1]; // must be "struct hack"
} packet;
Create a socket server.
With this command ./server 127.0.0.1 8000 it must be listening on address 127.0.0.1 and port 8000 (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 server 127.0.0.1 and port 8000 (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.
The text was updated successfully, but these errors were encountered:
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:
Create a socket server.
./server 127.0.0.1 8000
it must be listening on address127.0.0.1
and port8000
(should work for other addresses and ports).##
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.server.c
.Create a socket client.
./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)client.c
.Response Example
qwerty
(6 bytes) to the server.qwerty
from the client.ytrewq
and wrap it with##
so the data will be##ytrewq##
.##ytrewq##
(10 bytes) to the client.Consider the following good looking sample
Server init (listen and bind to address and port)
Client connects and sends data
Server receives a connection and send response
Client receives a response
The text was updated successfully, but these errors were encountered: