Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| workshop:sockets [2010-09-29 17:47] – angelegt equinox | workshop:sockets [2025-11-09 09:25] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Vortrag/ | + | ====== Vortrag/ |
| * by Christian Pointner | * by Christian Pointner | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Beispielcode ===== | ||
| + | |||
| + | Es folgen ein paar simple Beispielprogramme die die Verwendung von Sockets unter Unix zeigen. Alle Beispiele und ein kleines Makefile können auch direkt per unten stehendem Link heruntergeladen werden: | ||
| + | |||
| + | {{workshop: | ||
| + | |||
| + | Es folgt noch der Code für einen sehr simplen Chat Server anhand dessen die Verwendung des Resolvers (DNS etc.) gezeigt wird. | ||
| + | |||
| + | ==== Beispiel: udpclient ==== | ||
| + | |||
| + | < | ||
| + | /* | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | if(argc <= 2) { | ||
| + | fprintf(stderr," | ||
| + | exit(1); | ||
| + | } | ||
| + | printf(" | ||
| + | |||
| + | int sock = socket(AF_INET, | ||
| + | if(sock < 0) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | | ||
| + | struct sockaddr_in remote_addr; | ||
| + | int alen, len; | ||
| + | alen = sizeof(remote_addr); | ||
| + | remote_addr.sin_family = AF_INET; | ||
| + | remote_addr.sin_port = htons(atoi(argv[2])); | ||
| + | if (inet_aton(argv[1], | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | uint8_t buffer[1500]; | ||
| + | for (;;) { | ||
| + | if((len = read(0, buffer, 1500))==-1) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | if((len = sendto(sock, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | close(sock); | ||
| + | | ||
| + | printf(" | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beispiel: udpserver ==== | ||
| + | |||
| + | < | ||
| + | /* | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | printf(" | ||
| + | |||
| + | int sock = socket(AF_INET, | ||
| + | if(sock < 0) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | | ||
| + | struct sockaddr_in local_addr; | ||
| + | memset((char *) & | ||
| + | local_addr.sin_family = AF_INET; | ||
| + | local_addr.sin_port = htons(1234); | ||
| + | local_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| + | |||
| + | if(bind(sock, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | struct sockaddr_in remote_addr; | ||
| + | int alen, len; | ||
| + | alen = sizeof(remote_addr); | ||
| + | uint8_t buffer[1500]; | ||
| + | for (;;) { | ||
| + | if((len = recvfrom(sock, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | close(sock); | ||
| + | | ||
| + | printf(" | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beispiel: tcpclient ==== | ||
| + | |||
| + | < | ||
| + | /* | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | if(argc <= 2) { | ||
| + | fprintf(stderr," | ||
| + | exit(1); | ||
| + | } | ||
| + | printf(" | ||
| + | |||
| + | int sock = socket(AF_INET, | ||
| + | if(sock < 0) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | | ||
| + | struct sockaddr_in remote_addr; | ||
| + | remote_addr.sin_family = AF_INET; | ||
| + | remote_addr.sin_port = htons(atoi(argv[2])); | ||
| + | if (inet_aton(argv[1], | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | if(connect(sock, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | int len; | ||
| + | uint8_t buffer[1500]; | ||
| + | for (;;) { | ||
| + | if((len = read(0, buffer, 1500))==-1) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | int offset = 0; | ||
| + | int wlen; | ||
| + | for(;;) { | ||
| + | wlen = send(sock, & | ||
| + | if(wlen == -1) { | ||
| + | if(errno != EINTR) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | wlen = 0; | ||
| + | } | ||
| + | |||
| + | offset += wlen; | ||
| + | if(offset+1 >= len) | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | close(sock); | ||
| + | | ||
| + | printf(" | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beispiel: tcpserver ==== | ||
| + | |||
| + | < | ||
| + | /* | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | * | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(int argc, char** argv) | ||
| + | { | ||
| + | printf(" | ||
| + | |||
| + | int server = socket(AF_INET, | ||
| + | if(server < 0) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | | ||
| + | struct sockaddr_in local_addr; | ||
| + | memset((char *) & | ||
| + | local_addr.sin_family = AF_INET; | ||
| + | local_addr.sin_port = htons(1234); | ||
| + | local_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
| + | |||
| + | if(bind(server, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | if(listen(server, | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | fd_set readfds, tmpfds; | ||
| + | FD_ZERO(& | ||
| + | FD_SET(server, | ||
| + | int max_fd = server; | ||
| + | uint8_t buffer[1500]; | ||
| + | struct sockaddr_in remote_addr; | ||
| + | memset (& | ||
| + | int len, alen=sizeof(remote_addr); | ||
| + | for (;;) { | ||
| + | memcpy(& | ||
| + | int num = select(max_fd+1, | ||
| + | if(num == -1 && errno != EINTR) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | if(num == -1 || !num) | ||
| + | continue; | ||
| + | |||
| + | if(FD_ISSET(server, | ||
| + | int new_client = accept(server, | ||
| + | if(new_client==-1) { | ||
| + | perror(" | ||
| + | exit(-1); | ||
| + | } | ||
| + | printf(" | ||
| + | FD_SET(new_client, | ||
| + | max_fd = (max_fd < new_client) ? new_client : max_fd; | ||
| + | FD_CLR(server, | ||
| + | } | ||
| + | |||
| + | int fd; | ||
| + | for(fd=0; fd< | ||
| + | if(FD_ISSET(fd, | ||
| + | len = recv(fd, buffer, 1500, 0); | ||
| + | if(len <= 0) { | ||
| + | if(len < 0) perror(" | ||
| + | else fprintf(stderr, | ||
| + | | ||
| + | printf(" | ||
| + | FD_CLR(fd, & | ||
| + | close(fd); | ||
| + | } | ||
| + | else | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | close(server); | ||
| + | | ||
| + | printf(" | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| ---- | ---- | ||
| {{tag> | {{tag> | ||