C language socket programming development application example

  • 2020-04-02 01:58:24
  • OfStack


< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201312/20131205100209.jpg? 20131151048 ">
Implementation steps:

1. The Server side


#include <stdio.h>                   //For calls to functions such as printf
#include <winsock2.h>                //Function calls to the Socket
#pragma comment (lib, "ws2_32")      //When C refers to other class libraries, in addition to the.h file, the corresponding lib file should be added (this is different from C#).


int main()
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in sockaddr;
    sockaddr.sin_family=PF_INET;
    sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");   //Which IP address to bind to locally
    sockaddr.sin_port=htons(9000);                          //Ports to listen on
    bind(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));        //Bind action
    listen(s, 1);                                           //Start listening
    printf("listening on port [%d].n", 9000);
    while(TRUE)
    {
        SOCKADDR clientAddr;
        int size=sizeof(SOCKADDR);
        SOCKET clientsocket;
        clientsocket=accept(s, &clientAddr, &size);               //Blocks until there is a new TCP client connection
        printf("***SYS***    New client touched.n");
        char* msg="Hello, my client.rn";
        send(clientsocket, msg, strlen(msg)+sizeof(char), NULL);  //Note that the third argument here is a char
        printf("***SYS***    HELLO.n");
        while(TRUE)
        {
            char buffer[MAXBYTE]={0};
            recv(clientsocket, buffer, MAXBYTE, NULL);            //Always receive the send operation of the client socket
            printf("***Client***    %sn", buffer);
        }
        closesocket(clientsocket);                                //Close the socket
    }
    closesocket(s);                           //Close listening socket

    WSACleanup();                                                //uninstall
    getchar();                                                   
    exit(0);
}

Since we are still learning, we are not implementing multi-threading, so the client can actually only have 1, and the rest will be blocked out

There is still some knowledge about socket buffer, such as the end of the last to \r\n, etc., are for a reason, the recommendation to see the data.

2. The Client end


#include <stdio.h>                      //Calls to input and output functions,printf, gets
#include <winsock2.h>                   //The socket header file
#include <Windows.h>                    //To facilitate debugging, a 2-second wait to connect to the server was added, using the sleep function
#pragma comment (lib, "ws2_32")         //The socket library files


int main()
{
    Sleep(2000);                        //Sleep 2 seconds before connecting to server
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    SOCKET s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in sockaddr;
    sockaddr.sin_family=PF_INET;
    sockaddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    sockaddr.sin_port=htons(9000);
    connect(s, (SOCKADDR*)&sockaddr, sizeof(SOCKADDR));
    char buffer[MAXBYTE]={0};
    recv(s, buffer, MAXBYTE, NULL);
    printf("***SERVER***%s", buffer);
    while(TRUE)
    {
        char* mymsg=new char[100000];
        printf("You can chat with server now:n");
        gets(mymsg);
        send(s, mymsg, strlen(mymsg)+sizeof(char), NULL);
        
    }
    closesocket(s);
    WSACleanup();
    getchar();
    exit(0);
}


 


Related articles: