aus Wikipedia http://en.wikipedia.org/wiki/Berkeley_sockets#bind.28.29
connect():
Die connect() Systemaufruf verbindet eine Buchse, durch seinen Dateideskriptor identifiziert, auf eine Remote-Host, durch die für Host-Adresse in der angegebenen Argumentliste.
Bestimmte Arten von Sockets sind verbindungslos, am häufigsten Benutzer Datagramm-Protokoll-Sockets. Für diese Sockets hat connect eine besondere Bedeutung: Das Standardziel für das Senden und Empfangen von Daten wird auf die angegebene Adresse gesetzt, so dass Funktionen wie send() und recv() auf verbindungslosen Sockets verwendet werden können.
connect() gibt eine Ganzzahl zurück, die den Fehlercode darstellt: 0 steht für Erfolg, während -1 für einen Fehler steht.
bind():
bind() weist eine Buchse auf eine Adresse. Wenn ein Socket mit socket() erstellt wird, erhält er nur eine Protokollfamilie, aber keine Adresse. Diese Zuordnung zu einer Adresse muss mit dem Systemaufruf bind() erfolgen, bevor der Socket Verbindungen zu anderen Hosts akzeptieren kann. bind() benötigt drei Argumente:
sockfd, ein Deskriptor, der den Socket zum Ausführen der Bindung darstellt. my_addr, ein Zeiger auf eine Sockaddr-Struktur, die die zu bindende Adresse darstellt. addrlen, ein socklen_t-Feld, das die Größe der Sockaddr-Struktur angibt. Bind() gibt 0 bei Erfolg und -1 zurück, wenn ein Fehler auftritt.
Beispiele: 1.) Mit Connect
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set the IP address to desired host to connect to */
serverAddr.sin_addr.s_addr = inet_addr("192.168.1.17");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
recv(clientSocket, buffer, 1024, 0);
/*---- Print the received message ----*/
printf("Data received: %s",buffer);
return 0;
}
2.) Bind Beispiel:
int main()
{
struct sockaddr_in source, destination = {}; //two sockets declared as previously
int sock = 0;
int datalen = 0;
int pkt = 0;
uint8_t *send_buffer, *recv_buffer;
struct sockaddr_storage fromAddr; // same as the previous entity struct sockaddr_storage serverStorage;
unsigned int addrlen; //in the previous example socklen_t addr_size;
struct timeval tv;
tv.tv_sec = 3; /* 3 Seconds Time-out */
tv.tv_usec = 0;
/* creating the socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
printf("Failed to create socket\n");
/*set the socket options*/
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
/*Inititalize source to zero*/
memset(&source, 0, sizeof(source)); //source is an instance of sockaddr_in. Initialization to zero
/*Inititalize destinaton to zero*/
memset(&destination, 0, sizeof(destination));
/*---- Configure settings of the source address struct, WHERE THE PACKET IS COMING FROM ----*/
/* Address family = Internet */
source.sin_family = AF_INET;
/* Set IP address to localhost */
source.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY = 0.0.0.0
/* Set port number, using htons function to use proper byte order */
source.sin_port = htons(7005);
/* Set all bits of the padding field to 0 */
memset(source.sin_zero, '\0', sizeof source.sin_zero); //optional
/*bind socket to the source WHERE THE PACKET IS COMING FROM*/
if (bind(sock, (struct sockaddr *) &source, sizeof(source)) < 0)
printf("Failed to bind socket");
/* setting the destination, i.e our OWN IP ADDRESS AND PORT */
destination.sin_family = AF_INET;
destination.sin_addr.s_addr = inet_addr("127.0.0.1");
destination.sin_port = htons(7005);
//Creating a Buffer;
send_buffer=(uint8_t *) malloc(350);
recv_buffer=(uint8_t *) malloc(250);
addrlen=sizeof(fromAddr);
memset((void *) recv_buffer, 0, 250);
memset((void *) send_buffer, 0, 350);
sendto(sock, send_buffer, 20, 0,(struct sockaddr *) &destination, sizeof(destination));
pkt=recvfrom(sock, recv_buffer, 98,0,(struct sockaddr *)&destination, &addrlen);
if(pkt > 0)
printf("%u bytes received\n", pkt);
}
Ich hoffe, dass die Differenz
EDIT stellt klar: Bitte beachten Sie, dass die Buchse Typ, den Sie angeben, hängt davon ab, was Sie benötigen, das ist extrem wichtig
In einem Satz: bind ist an lokale Adresse, Verbindung ist an Remote-Adresse. – SHR