1 / 14

Comprehensive Guide to Socket Programming: Client-Server Communication and Select Demo

This guide provides detailed insights into socket programming, showcasing examples of client-server communication in C. It covers key functions like socket creation, binding, listening, accepting connections, and handling multiple clients using the select system call. The practical implementation through demos ensures that readers understand the nuances of handling inputs and outputs, making it an essential resource for both beginners and experienced developers. Dive into the tutorial to explore UDP communication and advanced server management techniques.

evita
Télécharger la présentation

Comprehensive Guide to Socket Programming: Client-Server Communication and Select Demo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket programozásPéldák

  2. write read Client / Server Session read write EOF read close close Áttekintés Client Server socket socket bind listen Connection request connect accept

  3. Select demo – Beej’s guide #include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> #define STDIN 0 // file descriptor for standard input int main(void) { structtimevaltv; fd_setreadfds; tv.tv_sec = 2; tv.tv_usec = 500000; FD_ZERO(&readfds); FD_SET(STDIN, &readfds); // don't care about writefds and exceptfds: select(STDIN+1, &readfds, NULL, NULL, &tv); if (FD_ISSET(STDIN, &readfds)) printf("A key was pressed!\n"); else printf("Timed out.\n"); return 0; }

  4. Select demo – Server side … fd_set master; fd_set readfds; int maxfds = sock; FD_ZERO(&master); FD_SET(sock, &master); for(;;) { readfds = master; select( maxfds+1, &readfds, 0, 0, 0 ); for (i=0; i<=maxfds; ++i) { if (FD_ISSET(i, &readfds)) { if (i==sock) { //új kliens akar csatlakozni int conn = accept(i, …); FD_SET(conn, &master); if (conn > maxfds) maxfds = conn; } else { // meglévő klienstől kaptunk üzenetet if ((rbytes=read(i, buf, sizeof(buf)))<=0) { // hiba ha <0 , a kliens bontotta a kapcsolatot ha =0 close(i); FD_CLR(i, &master); // nem figyeljük tovább } else { // kapott üzenet feldolgozása… } } } } }

  5. Select demo – Server side … fd_set master; fd_set readfds; int maxfds = sock; FD_ZERO(&master); FD_SET(sock, &master); for(;;) { readfds = master; select( maxfds+1, &readfds, 0, 0, 0 ); for (i=0; i<=maxfds; ++i) { if (FD_ISSET(i, &readfds)) { if (i==sock) { //új kliens akar csatlakozni int conn = accept(i, …); FD_SET(conn, &master); if (conn > maxfds) maxfds = conn; } else { // meglévő klienstől kaptunk üzenetet if (rbytes=read(i, buf, sizeof(buf))<=0) { // hiba ha <0 , a kliens bontotta a kapcsolatot ha =0 close(i); FD_CLR(i, &master); // nem figyeljük tovább } else { // kapott üzenet feldolgozása… } } } } }

  6. Select demo – Server side … fd_set master; fd_set readfds; int maxfds = sock; FD_ZERO(&master); FD_SET(sock, &master); for(;;) { readfds = master; select( maxfds+1, &readfds, 0, 0, 0 ); for (i=0; i<=maxfds; ++i) { if (FD_ISSET(i, &readfds)) { if (i==sock) { //új kliens akar csatlakozni int conn = accept(i, …); FD_SET(conn, &master); if (conn > maxfds) maxfds = conn; } else { // meglévő klienstől kaptunk üzenetet if (rbytes=read(i, buf, sizeof(buf))<=0) { // hiba ha <0 , a kliens bontotta a kapcsolatot ha =0 close(i); FD_CLR(i, &master); // nem figyeljük tovább } else { // kapott üzenet feldolgozása… } } } } }

  7. Select demo – Server side … fd_setmaster; fd_setreadfds; int maxfds = sock; FD_ZERO(&master); FD_SET(sock, &master); for(;;) { readfds = master; select( maxfds+1, &readfds, 0, 0, 0 ); for (i=0; i<=maxfds; ++i) { if (FD_ISSET(i, &readfds)) { if (i==sock) { //új kliens akar csatlakozni int conn = accept(i, …); FD_SET(conn, &master); if (conn > maxfds) maxfds = conn; }else { // meglévő klienstől kaptunk üzenetet if ((rbytes=read(i, buf, sizeof(buf)))<=0) { // hiba ha <0 , a kliens bontotta a kapcsolatot ha =0 close(i); FD_CLR(i, &master); // nem figyeljük tovább } else { // kapott üzenet feldolgozása… } } } } }

  8. Select demo – Server side … fd_set master; fd_set readfds; int maxfds = sock; FD_ZERO(&master); FD_SET(sock, &master); for(;;) { readfds = master; select( maxfds+1, &readfds, 0, 0, 0 ); for (i=0; i<=maxfds; ++i) { if (FD_ISSET(i, &readfds)) { if (i==sock) { //új kliens akar csatlakozni int conn = accept(i, …); FD_SET(conn, &master); if (conn > maxfds) maxfds = conn; } else { // meglévő klienstől kaptunk üzenetet if ((rbytes=read(i, buf, sizeof(buf)))<=0) { // hiba ha <0 , // a kliens bontotta a kapcsolatot ha =0 close(i); FD_CLR(i, &master); // nem figyeljük tovább } else { // kapott üzenet feldolgozása… } } } } }

  9. Server leállítása void closeServer(int); Int main() { … signal( SIGTERM, closeServer ); … } void closeServer(int sig) { // kilőtték a szervert, //itt tudod elvégezni a leállításhoz szükséges teendőket }

  10. Server - foglalt port sock = socket(…); … int yes=1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); … bind(sock, …);

  11. UDP alapú kommunikáció Forrás:http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzab6%2Frzab6connectionless.htm

  12. UDP datagram socket Üzenet küldés: • intsendto(intsockfd, const void *msg, intlen, unsigned intflags,conststructsockaddr *to, socklen_ttolen); Üzenet fogadása: • intrecvfrom(intsockfd, void *buf, intlen, unsigned int flags, structsockaddr *from, int *fromlen);

  13. Extra feladat többlet pontok szerzéséhez: • Több felhasználós számológép megvalósítása • (ld. feladatok.txt)

More Related