Socket Programmierung
-
Hi !
Ich habe folgendes Programm geschrieben, das als Server laufen soll
Auschnitt:#include <sys/types.h> //bind #include <sys/socket.h> //bind #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #define PORT 9999 int main( int argc , char** argv ) { const int y = 1; int socket_fd; if( socket_fd = socket( AF_INET , SOCK_STREAM , 0 ) > 0 ) printf("Serversocket erfolgreich angelegt.\n"); setsockopt( socket_fd , SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int) ); struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); //socket binden if( bind( socket_fd , (struct sockaddr*) &address , sizeof( address ) ) == 0 ) printf("Socket erfolgreich gebunden.\n"); else { printf("Port %i nicht freigegeben.\nProgramm beendet.\n", PORT ); exit(0); } //usw
Leider springt mein Programm immer in den else Block des letzten ifs. Wieso kann er den Port 9999 nicht benutzen ? Andere Port funktionieren auch nicht.
-
hi,
if( socket_fd = socket( AF_INET , SOCK_STREAM , 0 ) > 0 )
der code sollte so aussehn
if( (socket_fd = socket( AF_INET , SOCK_STREAM , 0 )) > 0 )
ansonsten bekommst dein socket_fd den wert 1 statt den return von socket()
mfg blan
-
blan: mach doch nicht immer alles so kompliziert
#include <sys/types.h> //bind #include <sys/socket.h> //bind #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #define PORT 9999 int main( int argc , char** argv ) { const int y = 1; int socket_fd; if( (socket_fd = socket( AF_INET , SOCK_STREAM , 0 )) > 0 ){ printf("Serversocket erfolgreich angelegt.\n"); setsockopt( socket_fd , SOL_SOCKET, SO_REUSEADDR, &y, sizeof(int) ); struct sockaddr_in address; address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); //socket binden if( bind( socket_fd , (struct sockaddr*) &address , sizeof( address ) ) == 0 ) printf("Socket erfolgreich gebunden.\n"); else { printf("Port %i nicht freigegeben.\nProgramm beendet.\n", PORT ); exit(0); } //usw
-
psychoO schrieb:
blan: mach doch nicht immer alles so kompliziert
blan hat ein mustergültiges Beispiel dafür abgeliefert, wie man Fragen sinnvoll beantwortet, Dein Post war hingegen vermutlich gut gemeint, aber nicht sehr hilfreich.
-
Danke für die Antwort, jetzt funktionierts. Allerdings wundert es mich ein bißchen, daß
if( socket_fd = socket( AF_INET , SOCK_STREAM , 0 ) > 0 )
nicht linksassozitiv ist ? Hat ">" höhere Prio als "="?
-
ändy schrieb:
Danke für die Antwort, jetzt funktionierts. Allerdings wundert es mich ein bißchen, daß
if( socket_fd = socket( AF_INET , SOCK_STREAM , 0 ) > 0 )
nicht linksassozitiv ist ? Hat ">" höhere Prio als "="?
Natürlich. Normalerweise will man doch erst das Endergebnis in einer Variable speichern und nicht erst den Ausdruck einklammern, damit man das Ergebnis speichern kann. Sonst würde folgendes ja nicht gehen:
int a,b; int greater; [...] greater = a > b;
Darum hat "=" eine der niedrigsten Prioritäten.
Prioritätenliste: http://info.baeumle.com/ansic/c04.html#1-9