Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

outil de programmation socket sur ubuntu

sock = socket(AF_INET, SOCK_STREAM, 0);
Comment

outil de programmation socket sur ubuntu

struct sockaddr_in
{
    short      sin_family;
    unsigned short   sin_port;
    struct   in_addr   sin_addr;
    char   sin_zero[8];
};
Comment

outil de programmation socket sur ubuntu

#if defined (WIN32)
    #include <winsock2.h>
    typedef int socklen_t;
#elif defined (linux)
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #define INVALID_SOCKET -1
    #define SOCKET_ERROR -1
    #define closesocket(s) close(s)
    typedef int SOCKET;
    typedef struct sockaddr_in SOCKADDR_IN;
    typedef struct sockaddr SOCKADDR;
#endif
 
#include <stdio.h>
#include <stdlib.h>
#define PORT 23
 
 
 
int main(void)
{
    #if defined (WIN32)
        WSADATA WSAData;
        int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
    #else
        int erreur = 0;
    #endif
    
    /* Socket et contexte d'adressage du serveur */
    SOCKADDR_IN sin;
    SOCKET sock;
    socklen_t recsize = sizeof(sin);
    
    /* Socket et contexte d'adressage du client */
    SOCKADDR_IN csin;
    SOCKET csock;
    socklen_t crecsize = sizeof(csin);
    
    int sock_err;
    
    
    if(!erreur)
    {
        /* Création d'une socket */
        sock = socket(AF_INET, SOCK_STREAM, 0);
        
        /* Si la socket est valide */
        if(sock != INVALID_SOCKET)
        {
            printf("La socket %d est maintenant ouverte en mode TCP/IP
", sock);
            
            /* Configuration */
            sin.sin_addr.s_addr = htonl(INADDR_ANY);  /* Adresse IP automatique */
            sin.sin_family = AF_INET;                 /* Protocole familial (IP) */
            sin.sin_port = htons(PORT);               /* Listage du port */
            sock_err = bind(sock, (SOCKADDR*)&sin, recsize);
            
            /* Si la socket fonctionne */
            if(sock_err != SOCKET_ERROR)
            {
                /* Démarrage du listage (mode server) */
                sock_err = listen(sock, 5);
                printf("Listage du port %d...
", PORT);
                
                /* Si la socket fonctionne */
                if(sock_err != SOCKET_ERROR)
                {
                    /* Attente pendant laquelle le client se connecte */
                    printf("Patientez pendant que le client se connecte sur le port %d...
", PORT);
                    csock = accept(sock, (SOCKADDR*)&csin, &crecsize);
                    printf("Un client se connecte avec la socket %d de %s:%d
", csock, inet_ntoa(csin.sin_addr), htons(csin.sin_port));
                }
                else
                    perror("listen");
            }
            else
                perror("bind");
            
            /* Fermeture de la socket client et de la socket serveur */
            printf("Fermeture de la socket client
");
            closesocket(csock);
            printf("Fermeture de la socket serveur
");
            closesocket(sock);
            printf("Fermeture du serveur terminée
");
        }
        else
            perror("socket");
        
        #if defined (WIN32)
            WSACleanup();
        #endif
    }
    
    return EXIT_SUCCESS;
}
Comment

outil de programmation socket sur ubuntu

#if defined (WIN32)
    #include <winsock2.h>
    typedef int socklen_t;
#elif defined (linux)
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #define INVALID_SOCKET -1
    #define SOCKET_ERROR -1
    #define closesocket(s) close(s)
    typedef int SOCKET;
    typedef struct sockaddr_in SOCKADDR_IN;
    typedef struct sockaddr SOCKADDR;
#endif
 
#include <stdio.h>
#include <stdlib.h>
#define PORT 23
 
 
 
int main(void)
{
    #if defined (WIN32)
        WSADATA WSAData;
        int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
    #else
        int erreur = 0;
    #endif
 
    SOCKET sock;
    SOCKADDR_IN sin;
 
    if(!erreur)
    {
        /* Création de la socket */
        sock = socket(AF_INET, SOCK_STREAM, 0);
 
        /* Configuration de la connexion */
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");
        sin.sin_family = AF_INET;
        sin.sin_port = htons(PORT);
 
        /* Si le client arrive à se connecter */
        if(connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
            printf("Connexion à %s sur le port %d
", inet_ntoa(sin.sin_addr), htons(sin.sin_port));
        else
            printf("Impossible de se connecter
");
 
        /* On ferme la socket précédemment ouverte */
        closesocket(sock);
 
        #if defined (WIN32)
            WSACleanup();
        #endif
    }
 
    return EXIT_SUCCESS;
}
Comment

PREVIOUS NEXT
Code Example
Shell :: Add Repote into local existing Repostry 
Shell :: Errors: grub-efi-amd64-signed & shim-signed 
Shell :: E: No se pudo abrir el fichero de bloqueo «/var/cache/apt/archives/lock» 
Shell :: wsl2 icmp_seq=165 Destination Host Unreachable after installing docker 
Shell :: install graylog for pfsense on centos 8 
Shell :: install using kartik extention 
Shell :: quit while loop bash 
Shell :: install any desc ubunto 
Shell :: ubuntu dual monitor mouse flicker 
Shell :: ignoring time stamp from the future mac 
Shell :: gnomestar 
Shell :: change hostname rhel 6 
Shell :: boundEastLongitude: -74.18249700000001 
Shell :: redirect folder to 403 
Shell :: gulp-tar 
Shell :: git undo only a chunk 
Shell :: set music cover image linux 
Shell :: zsh leading zeros 
Shell :: how to find the version of apache server in pentest 
Shell :: filetype exfat not configured in kernel 
Shell :: running InfluxDB in a container over a network 
Shell :: mirror image via command line 
Shell :: bash set+x hide 
Shell :: check powershell profile create if not exists 
Shell :: how to generate keys for mtls 
Shell :: C:Ruby30-x64in uby.exe: Is a directory -- . (LoadError) 
Shell :: chmod rwx for all users 
Shell :: --force-confold 
Shell :: ubuntu kubernetes monitoring tools free 
Shell :: automatically install all dependences of a package in R 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =