issamansur / ChatOnBaseSocket

Simple c++ project for testing sockets with ws2_32.lib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ChatOnBaseSocket

The principle of operation:

  1. Initialize WSA - WSAStartup()
  2. Create a socket - socket()
  3. Bind the socket - bind()
  4. Listen on the socket - listen()
  5. Accept a connection - accept()/connect()
  6. Send and receive data - recv(), recvfrom/send(), sendto()
  7. Disconnect - closesocket()

From abstraction to realization:

Preparing

Before you develop Windows Sockets programs within Visual Studio, you must include the ws2_32.lib library within your Project.

Create a new C++ Console Project and follow the steps belows:

  • Select the Project menu
  • Select the Properties (by default Alt+F7)
  • In opened window in left menu select Linker
  • In submenu select Input
  • In main view select Additional Dependencies and Edit...
  • In opened subwindow in input print ws2_32.lib and click OK
  • Click Apply and then OK
  • Done!

Tasks

Main

  • Connect Client with Server

  • Add args for cli (Command line)

  • Client can send message with size more than bufferSize

  • Client can send more than one message

  • Server can respond on Client's message

  • Server can accept and listen more than one Client

  • Server can receive message from one Client and MAYBE send this message to others Clients

Additional

  • Change splitting: vector -> char* (for optimize)
  • Add maximum count of Clients
  • Every Client must have unique username
  • Add command /exit to leave chat
  • Add command /private [username] to send private message
  • Add formatted output (BETA)

Example

Server

IP ADDRESS: 127.0.0.1
PORT: 55555

The Winsock dll found!
The status: Running
socket() is OK!
bind() is OK!
Start listening on port: 55555
Accepted connection 1/10
Accepted connection 2/10
[2023-11-08 22:10:19] user2: hi, user1
[2023-11-08 22:10:32] user1: hello, user2
[2023-11-08 22:10:50] user1: how are u?
Client has terminated the connection
Client's socket was closed 1/10
Client has terminated the connection
Client's socket was closed 0/10

Client1

IP ADDRESS: 127.0.0.1
PORT: 55555

The Winsock dll found!
The status: Running
socket() is OK!
Client connect() is OK!
Enter your username (5-20 symbols): user1
[2023-11-08 22:10:19] user2: hi, user1
[2023-11-08 22:10:32] user1: hello, user2
[2023-11-08 22:10:50] user1: how are u?

...

user1 (YOU) > /exit
Client closed the connection.

Client1

IP ADDRESS: 127.0.0.1
PORT: 55555

The Winsock dll found!
The status: Running
socket() is OK!
Client connect() is OK!
Enter your username (5-20 symbols): user2
[2023-11-08 22:10:19] user2: hi, user1
[2023-11-08 22:10:32] user1: hello, user2
[2023-11-08 22:10:50] user1: how are u?

...

user1 (YOU) > /exit
Client closed the connection.

Interesting links for updates

About

Simple c++ project for testing sockets with ws2_32.lib

License:MIT License


Languages

Language:C++ 100.0%