Hackerboard Wiki HaboBlog
Hackerboard bei Facebook Hackerboard bei Google+ Hackerboard bei Twitter

[HaBo]

 
Network · LAN, WAN, Firewalls Alle Fragen rund um das große, kleine Internet finden hier eine Antwort. LANs, WANs, Router, Switches, Bridges, Verkabelung...

C Programm(Socket): TCP zu UDP

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, ...

Antwort
Alt 28.02.06, 08:24   #1 (permalink)
 
Registriert seit: 30.09.04
Sodawasser Leistung: Facit NTK
Sodawasser eine Nachricht über ICQ schicken
Likes: 0
Standard C Programm(Socket): TCP zu UDP

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

}

Sodawasser ist offline   Mit Zitat antworten
Alt 28.02.06, 09:15   #2 (permalink)
 
Registriert seit: 30.05.05
v01d Leistung: Facit NTK
Likes: 0
Standard

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
v01d ist offline   Mit Zitat antworten
   
HaBOT
 
- Anzeige -

Werbung ist gerade online    
Alt 28.02.06, 12:12   #3 (permalink)
Gulliver
Guest
 
Likes:
Standard

halb so wild!

http://www.zotteljedi.de/doc/index.xhtml

mfg
  Mit Zitat antworten
Antwort
   
- Anzeige -

Werbung ist gerade online    

[HaBo] » Web, Network & Multimedia Palace » Network · LAN, WAN, Firewalls » C Programm(Socket): TCP zu UDP
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus


Ä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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61