Da Wikipedia http://en.wikipedia.org/wiki/Berkeley_sockets#bind.28.29
connect():
Il connect() chiamata di sistema collega una presa, identificato dal suo descrittore di file, ad un host remoto specificato da indirizzo dell'host nel lista di argomenti.
Alcuni tipi di socket sono socket di protocollo datagramma utente senza connessione. Per questi socket, connect assume un significato speciale: il target predefinito per l'invio e la ricezione dei dati viene impostato sull'indirizzo dato, consentendo l'uso di funzioni come send() e recv() su socket senza connessione.
connect() restituisce un numero intero che rappresenta il codice di errore: 0 rappresenta il successo, mentre -1 rappresenta un errore.
bind():
bind() assegna un socket a un indirizzo. Quando un socket viene creato utilizzando socket(), viene fornita solo una famiglia di protocolli, ma non viene assegnato un indirizzo. Questa associazione con un indirizzo deve essere eseguita con la chiamata di sistema bind() prima che il socket possa accettare connessioni ad altri host. bind() accetta tre argomenti:
sockfd, un descrittore che rappresenta il socket per eseguire il bind. my_addr, un puntatore a una struttura sockaddr che rappresenta l'indirizzo a cui collegarsi. addrlen, un campo socklen_t che specifica la dimensione della struttura sockaddr. Bind() restituisce 0 in caso di successo e -1 se si verifica un errore.
Esempi: 1. ) Usando Collegare
#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 Esempio:
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);
}
Spero che chiarisce la differenza
EDIT: Si prega di notare che la presa tipo che dichiari dipenderà da ciò che richiedi, questo è estremamente importante
In una frase: bind è all'indirizzo locale, la connessione è all'indirizzo remoto. – SHR