จาก Wikipedia http://en.wikipedia.org/wiki/Berkeley_sockets#bind.28.29
เชื่อมต่อ ():
การเรียกระบบ connect () เชื่อมต่อซ็อกเก็ตซึ่งระบุโดยตัวอธิบายไฟล์กับโฮสต์ระยะไกลที่ระบุโดยแอดเดรสของโฮสต์นั้นในรายการอาร์กิวเมนต์
ซ็อกเก็ตบางประเภทไม่มีการเชื่อมต่อซ็อกเก็ตโปรโตคอลดาตาแกรมที่ผู้ใช้ส่วนใหญ่มักใช้ สำหรับซ็อกเก็ตเหล่านี้การเชื่อมต่อจะมีความหมายพิเศษ: เป้าหมายเริ่มต้นสำหรับการส่งและรับข้อมูลจะถูกตั้งค่าเป็นที่อยู่ที่กำหนดทำให้สามารถใช้ฟังก์ชันต่างๆเช่น send () และ recv () บนซ็อกเก็ตที่ไม่มีการเชื่อมต่อ
connect () ส่งคืนจำนวนเต็มแทนรหัสข้อผิดพลาด: 0 หมายถึงความสำเร็จในขณะที่ -1 แสดงถึงข้อผิดพลาด
ผูก():
bind () กำหนดซ็อกเก็ตให้กับแอดเดรส เมื่อสร้างซ็อกเก็ตโดยใช้ซ็อกเก็ต () จะได้รับเฉพาะตระกูลโปรโตคอล แต่ไม่ได้กำหนดแอดเดรส การเชื่อมโยงกับแอดเดรสนี้ต้องดำเนินการด้วยการเรียกระบบ bind () ก่อนที่ซ็อกเก็ตจะสามารถยอมรับการเชื่อมต่อกับโฮสต์อื่น ผูก () รับสามอาร์กิวเมนต์:
sockfd ตัวอธิบายที่แสดงถึงซ็อกเก็ตเพื่อทำการผูก my_addr ตัวชี้ไปยังโครงสร้าง sockaddr ที่แสดงที่อยู่ที่จะผูก addrlen คือฟิลด์ socklen_t ที่ระบุขนาดของโครงสร้าง sockaddr Bind () ส่งคืน 0 เมื่อสำเร็จและ -1 หากเกิดข้อผิดพลาด
ตัวอย่าง: 1. ) การใช้ 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. ) ตัวอย่างการผูก:
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);
    }
ฉันหวังว่าจะชี้แจงความแตกต่าง
โปรดทราบว่าประเภทซ็อกเก็ตที่คุณประกาศจะขึ้นอยู่กับสิ่งที่คุณต้องการซึ่งเป็นสิ่งสำคัญอย่างยิ่ง