| die letzten Änderungen * Seitenstruktur * Stichwortsuche :


logo_puschin.jpg

Startseite

Knowledge base



Kontakt

Impressum

Knowledge base - C

Knowledge base

7 Benutzer online Druckversion




zurück



C
Einen lokalen Port öffnen (SOCKET-Programmierung)

socket.c:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#define PORT 0 /* random port number */
#define MAX_QUEUE 1
#define BUF_SIZE 1024

main()
{
    int sock_1,sock_2; /* file descriptors for sockets */
    int rec_value, length;
    char buf[BUF_SIZE];
    struct sockaddr_in server;

    /* create stream socket in internet domain*/
    sock_1 = socket(AF_INET,SOCK_STREAM,0);

    if (sock_1 < 0)
    {
        perror("open stream socket");
     exit(1);
    }

    /* build address in internet domain */
    server.sin_family = AF_INET;

    /* everyone is allowed to connet to server */
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = PORT;

    /* bind socket */
    if(bind(sock_1,&server,sizeof(struct sockaddr_in)))
    {
        perror("bind socket to server_addr");
     exit(1);
    }

    listen(sock_1,MAX_QUEUE);

    /* start accepting connection */
    sock_2 = accept(sock_1,0,0);

    if(sock_2 < 0)
    {
     perror("accept");
        exit(1);
    }
    /* read from sock_2 */
    while (rec_value=read(sock_2,buf,BUF_SIZE))
    {
     if(rec_value<0)
     {
         perror("reading stream message");
         exit(1);
     }
     else
         write(1,buf,rec_value);
    }
    printf("Ending connection.\n");
    close(sock_1);
    close(sock_2);
}

# make socket
# ./socket


zurück



Knowledge base wurde zuletzt bearbeitet am 12.07.13 durch Frank

www.puschin.de
login

<body bgcolor='#FFFFFF' link='#000000' vlink='#000000' alink='#000000' text='#000000'><font face='Verdana' size='2'><strong>www.puschin.de</strong><br>Die Webseite http://www.puschin.de bietet eine interessante Webseite zu vielen Themen aus dem Bereich Linux und Windows. Man findet hier Tipps und Tricks zu cms php apache postfix openxchange tomcat windows linux firewall <br><br><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=191'>Startseite</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=63'>Knowledge base</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=57'>Kontakt</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=9'>Impressum</a></body>