5.1 Netzwerk-Programmierung in C/C++

Ein Simpler TCP-Server:

#include "cliserv.h"


int main()
{
    struct sockaddr_in serv, cli;
    char   request[REQUEST], reply[REPLY];
    int    listenfd, sockfd, n, clilen;

    if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
        err_sys("socket error");

    memset($serv, sizeof(serv), 0);
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    serv.sin_port = htons(TCP_SERV_PORT);

    if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
        err_sys("bind error");
    if (listen(listenfd, SOMAXCONN) < 0)
        err_sys("listen error");

    for (;;) {
        clilen = sizeof(cli);
        if ((sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
            err_sys("accept error");
        if ((n = read_stream(sockfd, request, REQUEST)) < 0)
            err_sys("read error");

        // n Bytes in request[] verarbeiten, reply[] erzeugen

        if (write(sockfd, reply, REPLY) != REPLY)
            err_sys("write error");

        close(sockfd);
    }
}
import java.net.*;
import java.io.*;

public static void main(String[] args)
{
    ServerSocket serv;
    Socket       cli;
    PrintStream  out;
    InputStream  in;


    try {
        serv = new ServerSocket(33333);
    } catch(IOException e) { ... }







    while(true) {
        try {
            cli = serv.accept();
        } catch(IOException e) { ... }
        try {
            out = cli.getOutputStream();
            in = cli.getInputStream();
            String request = in.readln();

            // reply erzeugen...

            out.println(reply);
            cli.close();
        } catch (IOException e) { ... }
    }
    try {
        serv.close();
    } catch (IOException e) { ... }
}

Ein simpler TCP-Client:

#include "cliserv.h"


int main(int argc, char *argv[])
{
    struct sockaddr_in serv;
    char   request[REQUEST], reply[REPLY];
    int    sockfd, n;

    // Prüfen der Parameter...

    memset(&serv, sizeof(serv), 0);
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr(argv[1]);
    serv.sin_port = htons(TCP_SERV_PORT);
    if (connect(sockfd, (SA) &serv, sizeof(serv)) < 0
        err_sys("connect error");

    // request[] initialisieren...

    if (write(sockfd, request, REQUEST) != REQUEST)
        err_sys("write error");
    if (shutdown(sockfd, 1) < 0)
        err_sys("shutdown error");
    if ((n = read_stream(sockfd, reply, REPLY)) < 0)
        err_sys("read error");

    // n Bytes von reply[] verarbeiten...

    exit(0);
}
import java.net.*;
import java.io.*;

public static void main(String[] args)
{
    Socket       clnt;
    PrintStream  out;
    InputStream  in;



    try {
        serv = new Socket("localhost", 33333);
    } catch(IOException e) { ... }







    try {
        out = serv.getOutputStream();
        in = serv.getInputStream();
        out.print("Hallo Server!");
        String reply = in.readln();
        serv.close();
    } catch (IOException e) { ... }
}


Zurück zur Übersicht; Beispiele: Class Hierarchy Index