| Network · LAN, WAN, Firewalls Alle Fragen rund um das große, kleine Internet finden hier eine Antwort. LANs, WANs, Router, Switches, Bridges, Verkabelung... |
Diskussion: C Programm(Socket): TCP zu UDP im Forum Network · LAN, WAN, Firewalls, in der Kategorie Web, Network & Multimedia Palace; Anzeige hallo, ich hab ein "kleines" problem, ich habe ein Programm welches ich nun von tcp auf udp umprogrammieren muss, ...
![]() |
| | #1 (permalink) |
| Anzeige hallo, ich hab ein "kleines" problem, ich habe ein Programm welches ich nun von tcp auf udp umprogrammieren muss, das Programm verwendet C Sockets, und ich kenn mich nicht aus. Kann mir bitte wer helfen, anbei der Code, danke schön, mfg sodawasser #include <stdio.h> #include <errno.h> #include <string.h> /* Windows-System */ #ifdef _WIN32 #include <winsock.h> /* Unix-System */ #else #include <sys/socket.h> // listen, socket, bind, getsockname, shutdown #include <sys/types.h> // socket, bind #include <netinet/in.h> // htonl, htons, ntohl, ntohs #include <fcntl.h> // fcntl //#include <arpa/inet.h> //#include <netdb.h> //#include <unistd.h> #endif void set_nonblocking (int sockfd) { int prev_mode (fcntl (sockfd, F_GETFL, 0)); if (prev_mode != -1) { fcntl (sockfd, F_SETFL, prev_mode | O_NONBLOCK); } } int main (int argc, char *argv[], char* envv[]) { #ifdef _WIN32 WSADATA wsaData; if (WSAStartup (MAKEWORD (1, 1), &wsaData) != 0) { fprintf (stderr, "WSAStartup (): Kann Winsock nicht initialisieren.\n"); exit (EXIT_FAILURE); } #endif // Socket holen int sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror ("socket ()"); } // Port anbinden sockaddr_in my_addr; my_addr.sin_family = AF_INET; my_addr.sin_port = htons(5000); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sockfd, (sockaddr *)&my_addr, sizeof(sockaddr)) == -1) { perror ("bind ()"); } // Port kontrollieren socklen_t len; getsockname(sockfd, (sockaddr *) &my_addr, &len); printf ("Port: %d\n", ntohs(my_addr.sin_port)); // auf eine Verbindung warten if (listen (sockfd, 5) == -1) { perror ("listen ()"); } // warten mit select set_nonblocking (sockfd); fd_set readfs, writefs, exceptfs; struct timeval tv; int maxfd (sockfd + 1); FD_ZERO (&readfs); FD_ZERO (&writefs); FD_ZERO (&exceptfs); FD_SET (sockfd, &readfs); tv.tv_sec = 10; tv.tv_usec = 0; int sel (select (maxfd, &readfs, &writefs, &exceptfs, &tv)); if (sel > 0) { // Verbindung annehmen socklen_t sin_size = sizeof (sockaddr_in); sockaddr_in remote_host; int sockfd2 = accept (sockfd, (sockaddr *) &remote_host, &sin_size); if (sockfd2 == -1) { perror ("accept ()"); } // Daten senden char content[] = "<html>Inhalt</html>"; int len (strlen (content)); int sent (send (sockfd2, content, len, 0)); printf ("Sent: %d bytes\n", sent); int down = shutdown (sockfd2, SHUT_RDWR); if (down == -1) { perror ("shutdown (sockfd2)"); } } int down = shutdown (sockfd, SHUT_RDWR); if (down == -1) { perror ("shutdown (sockfd)"); } #ifdef _WIN32 WSACleanup (); #endif } | |
| | |
| | #2 (permalink) |
| Registriert seit: 30.05.05 ![]() Likes: 0 | solln wir des jetz für dich umprogrammieren oder was? hast du konkrete fragen? kannst dir mal die seite hier angucken www.c-worker.ch |
| | |
![]() |
| - Anzeige - | |
| |
| Themen-Optionen | |
| Ansicht | |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| XML Socket | 10110010 | (Web-) Design und webbasierte Sprachen | 9 | 05.09.06 00:14 |
| Socket [asm] | CPU8080 | Code Kitchen | 5 | 20.08.06 00:31 |
| Socket | metinoenal | Code Kitchen | 2 | 29.05.03 13:39 |
| socket error | reactor | Die Problemzone | 4 | 23.07.02 17:20 |
| Socket Error | hackys | Network · LAN, WAN, Firewalls | 3 | 08.11.01 19:37 |