Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create your own branch, build a simple TCP socket client and server. #1

Open
ammarfaizi2 opened this issue May 8, 2020 · 2 comments
Assignees
Labels

Comments

@ammarfaizi2
Copy link
Member

ammarfaizi2 commented May 8, 2020

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.

  1. 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).
  2. Must be able to accept a connection.
  3. 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.
  4. Write your code in a file named server.c.

Create a socket client.

  1. 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)
  2. The client app should ask for the input from stdin and send the given data to the server.
  3. After the client has sent the data, it should wait for the server response and print the received data and data length to stdout.
  4. Write your code in a file named client.c.

Response Example

  1. The client sends data qwerty (6 bytes) to the server.
  2. The server receives data qwerty from the client.
  3. The server reverses the received data to be ytrewq and wrap it with ## so the data will be ##ytrewq##.
  4. 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.
@ammarfaizi2 ammarfaizi2 added the good first issue Good for newcomers label May 8, 2020
@ammarfaizi2 ammarfaizi2 reopened this Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants
@ammarfaizi2 and others