A simple network programming project

1, Main functions of the project:

(1) Multi person chat can be carried out through the client and server

(2) The client and server have self checking function to check whether the connection between the client and server is successful

(3) The client can customize the nickname in the chat room

(4) When users enter the chat room, they will be reminded of the entry time

(5) After the user successfully sends the text, a successful withdrawal is returned

 

2, Main technical route:

The information exchange between client and server is realized by calling WS2tcpip, winsock2 and other header files

The time display is realized by calling the time header file

The purpose of writing the chat room this time is to create the socket keyword to realize the communication between networks. More specifically, I summarize the content of my client as follows:

(1) Opening a communication channel and connecting to a specific port of the host where the server is located;

(2) Send a service request message to the server, wait and receive a response; Continue to make requests

(3) After the request is completed, close the communication channel and terminate.

And in theory, the server process is generally started first. As long as the system is running, the service process exists until normal or forced termination.

In fact, after understanding the idea of socket programming, the general framework is very simple. To sum up, it is to reserve interfaces and realize functions with specific functions.

After WSAStartup starts the corresponding version,

use

serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

Create socket,

use

inet_pton(AF_INET, "127.0.0.1", (void*)& addr.sin_addr.S_un.S_addr)

Initialization address,

use

int r = connect(serverSocket, (SOCKADDR*)& addr, sizeof addr)

Connect to the server.  

WSAStartup corresponds to WSACleanup, and whether the socket protocol is started is determined by the return value of WSACleanup;

int r = connect(serverSocket, (SOCKADDR*)& addr, sizeof addr)

In fact, it is similar to the first handshake of the three handshakes in TCP/IP protocol. After the first handshake,

use

 r=send(serverSocket, name, strlen(name), NULL)

Sending data is simple in nature.            

 

3, General framework of implementation:

(1) Load the version number of socket library and check the initialization of the overall version

(2) Perform the requested version check

(3) Create socket, define protocol domain, type and protocol, and check

(4) Initialization address, port

(5) Bind socket, bind the address port and judge whether it is successful

(6) For the server, check whether the listening is successful

(7) For the client, you need to judge whether the connection is successful

(8) Data interaction

 

4, Implementation code

#include <iostream>
#include<WS2tcpip.h>
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
#include "string"
#pragma comment (lib, "ws2_32.lib")


using namespace std;

SOCKET serverSocket;//The server


void recvAndShow()
{
    int r, i = 0;
    char buff[256];
    while (1)
    {
        memset(buff, 0, 256);
        r = recv(serverSocket, buff, 255, NULL);
        if (r > 0)
        {
            i++;
        }
    }
}


int main()
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        cout << "Overall version application initialization error" << endl;
    }
    //cout << "The overall version application was initialized successfully" << endl;
    if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
    {
        cout << "Failed to request version!\n" << endl;
        return -1;
    }
    //cout << "The requested version succeeded!\n" << endl;


    //establish socket
    serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (serverSocket == INVALID_SOCKET)
    {
        cout << "establish socket Failed!\n" << endl;
        return -1;
    }
    //cout << "establish socket success!\n" << endl;


    //Address family
    SOCKADDR_IN addr = { 0 };


    //Initialization address
    inet_pton(AF_INET, "127.0.0.1", (void*)& addr.sin_addr.S_un.S_addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(10001);


    //Connect to server
    int r = connect(serverSocket, (SOCKADDR*)& addr, sizeof addr);
    if (r == -1)
    {
        cout << "Failed to connect to the server!\n" << endl;
        return -1;
    }
    cout << "###################################################Successfully connected to the server#######################################################" << endl;


    CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvAndShow, NULL, NULL, NULL);

    char name[20];
    cout << "Please enter your chat room user name:" << endl;
    cin >> name;
    r = send(serverSocket, name, strlen(name), NULL);
    if (r > 0)
    {
        cout << "########################################################################################################################" << endl;
        cout << "success" << endl;
    }

    char buff[256];
    while (1)
    {
        memset(buff, 0, 256);
        cout << "########################################################################################################################" << endl;
        cout << "Please enter what you want to enter:" << endl;
        cin >> buff;
        r = send(serverSocket, buff, strlen(buff), NULL);
        if (r < 0)
        {
            cout << "########################################################################################################################" << endl;
            cout <<"fail in send" << endl;
            cout << "########################################################################################################################" << endl;
        }
    }
    return 0;
}
#include <iostream>
#include<WS2tcpip.h>
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
#include "string"
#pragma comment (lib, "ws2_32.lib")


using namespace std;

SOCKET serverSocket;//The server


void recvAndShow()
{
    int r, i = 0;
    char buff[256];
    while (1)
    {
        memset(buff, 0, 256);
        r = recv(serverSocket, buff, 255, NULL);
        if (r > 0)
        {
            i++;
        }
    }
}


int main()
{
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        cout << "Overall version application initialization error" << endl;
    }
    //cout << "The overall version application was initialized successfully" << endl;
    if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
    {
        cout << "Failed to request version!\n" << endl;
        return -1;
    }
    //cout << "The requested version succeeded!\n" << endl;


    //establish socket
    serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (serverSocket == INVALID_SOCKET)
    {
        cout << "establish socket Failed!\n" << endl;
        return -1;
    }
    //cout << "establish socket success!\n" << endl;


    //Address family
    SOCKADDR_IN addr = { 0 };


    //Initialization address
    inet_pton(AF_INET, "127.0.0.1", (void*)& addr.sin_addr.S_un.S_addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(10001);


    //Connect to server
    int r = connect(serverSocket, (SOCKADDR*)& addr, sizeof addr);
    if (r == -1)
    {
        cout << "Failed to connect to the server!\n" << endl;
        return -1;
    }
    cout << "###################################################Successfully connected to the server#######################################################" << endl;


    CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvAndShow, NULL, NULL, NULL);

    char name[20];
    cout << "Please enter your chat room user name:" << endl;
    cin >> name;
    r = send(serverSocket, name, strlen(name), NULL);
    if (r > 0)
    {
        cout << "########################################################################################################################" << endl;
        cout << "success" << endl;
    }

    char buff[256];
    while (1)
    {
        memset(buff, 0, 256);
        cout << "########################################################################################################################" << endl;
        cout << "Please enter what you want to enter:" << endl;
        cin >> buff;
        r = send(serverSocket, buff, strlen(buff), NULL);
        if (r < 0)
        {
            cout << "########################################################################################################################" << endl;
            cout <<"fail in send" << endl;
            cout << "########################################################################################################################" << endl;
        }
    }
    return 0;
}

 

Keywords: Project

Added by crazy_carl on Sat, 08 Jan 2022 18:47:26 +0200