K
K
Kirill2020-05-07 02:28:12
C++ / C#
Kirill, 2020-05-07 02:28:12

How to work with sockets in c++?

Good afternoon, I need advice.

I'm not much confused with network programming.

I wrote a chat

server

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>

void error(const char *msg) 
{
    perror(msg); 
    exit(1);
}

int main(int argc, char const *argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "Port didd't provided. Program terminated\n");
        exit(1);
    }

    int sockfd, newsockfd, portnumber, temp;
    char buffer[255];
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_len;

    sockfd = socket(AF_INET, SOCK_STREAM, 0); // 0 = tcp    
    if (sockfd < 0)
    {
        error("Error opening socket");
    }

    memset(&server_addr, 0, sizeof server_addr);
    portnumber = atoi(argv[1]); 
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(portnumber);

    if (bind(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)
    {
        error("Binding failed");
    }
    
    listen(sockfd, 5);
    client_len = sizeof(client_addr);

    newsockfd = accept(sockfd, (struct sockaddr*) &client_addr, &client_len);

    if (newsockfd < 0)
    {
        error("Error on accept");
    }
    
    while (1)
    {
        memset(buffer, 0, 255);
        temp = read(newsockfd, buffer, 255); 
        if (temp < 0)
        {
            error("Error on reading");
        }        
        printf("client : %s\n", buffer);

        memset(buffer, 0, 255);
        std::cin.getline(buffer, 255);
        temp = write(newsockfd, buffer, strlen
        (buffer));
        if (temp < 0)
        {
            error("Error on writting");
        }
        if (int i = strncmp("bye", buffer, 3) == 0)
        {
            break;
        }
    }
    close(newsockfd);
    close(sockfd);
    return 0;
}


customer

// argv[0] - filename
// argv[1] - server_ipaddres
// argv[2] - portnumber


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> // hostent struct
#include <iostream>
#include <arpa/inet.h>

void error(const char *msg) 
{
    perror(msg); 
    exit(1);
}

int main(int argc, char const *argv[])
{
    int sockfd, portnumber, temp;
    struct sockaddr_in server_addr;

    char buffer[255];
    
    if (argc < 2)
    {
        fprintf(stderr, "usage %s hostname port\n", argv[0]);
        exit(1);
    }    

    portnumber = atoi(argv[1]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        error("Error opening socket");
    }

    portnumber = atoi(argv[1]); 
    server_addr.sin_family = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
    server_addr.sin_port = htons(portnumber);

    if (connect(sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0)
    {
        error("Connection failed");
    }
    
    while (1)
    {
        memset(buffer, 0, 255);
        std::cin.getline(buffer, 255);

        temp = write(sockfd, buffer, strlen(buffer));
        if (temp < 0)
        {
            error("Error on writting");
        }

        memset(buffer, 0, 255);
        temp = read(sockfd, buffer, 255); 
        if (temp < 0)
        {
            error("Error on reading");
        }
        printf("server : %s\n", buffer);

        if (int i = strncmp("bye", buffer, 3) == 0)
        {
            break;
        }       
        
    }
    close(sockfd);
    return 0;
}



But I didn't use or create sockaddr structures, I didn't use the getaddrinfo() function, and I don't quite understand why. As I understand it: my code is the way it was written before. And the structure and function that I wrote above is an innovation. Or I don't understand something.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anikavoi, 2020-05-07
@anikavoi

Dear,
You have a very twilight understanding of sockets.
1. What does the "Web" from the title of the question have to do with it? These are normal TCP. WebSocket is completely different, take a look at the documentation.
2. "I didn't use or create sockaddr structures" - are my eyes deceiving me?
Without sockaddr, you wouldn't be able to bind the port on the server, and determine the destination address/port on the client, let alone the protocol.
3. "I didn't use the getaddrinfo() function" - cool!!! Wow!! ... and what?

The getaddrinfo(3) function combines the actions of the getipnodebyname(3), getipnodebyaddr(3), getservbyname(3) and getservbyport(3) functions into one interface. The getaddrinfo(3) function creates one or more socket address structures that can later be used in bind(3) or connect(3) function calls to create a client or server socket.

4. "And the structure and function that I wrote on top is an innovation." - WHAT STRUCTURE??? WHAT FUNCTION??? void error(const char *msg) is this??? This is an amazing innovation! the Nobel Peace Prize and the Nobel Prize in Literature to you! They are still being given to people who are far from the world and from literature.
5. "Or I don't understand something." - nothing. You don't understand anything. Finally, read the documentation on socket programming BEFORE you ask a question that is completely unclear what the question is about.
... or better yet, quit programming.

H
HemulGM, 2020-05-07
@HemulGM

You may not explicitly use the sockaddr, getaddrinfo(), or getipnodebyname() structures, or even be unaware of them at all. They are used in any case somewhere deeper inside, because. these are low-level, basic methods for working with sockets and with network protocols in general.
As noted above, what you are using is not web sockets. Web sockets is a higher level protocol than regular sockets.
PS code copied from the net cannot be considered as written by you. ( hoba )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question