Socket call doesn't specify where data will be coming from, nor where it will be going to, it just creates the interface!
int sockid = socket(family, type, protocol);
- PF_INET, IPv4 protocols, Internet addresses (typically used)
- PF_UNIX, Local communication, File addresses
- SOCK_STREAM - reliable, 2-way, connection-based service
- SOCK_DGRAM - unreliable, connectionless, messages of maximum length
- IPPROTO_TCP IPPROTO_UDP
- usually set to 0 (i.e., use default protocol)
�status = close(sockid);
-
sockid: the file descriptor (socket being closed)
-
status: 0 if successful, -1 if error
-
closes a connection (for stream socket)
-
frees up the port used by the socket
struct sockaddr{
unsigned short sa_family; // Address family (e.g. AF_INET)
char sa_data[14]; // Family-specific address information
}
struct in_addr{
unsigned long s_addr;
}
struct sockaddr_in{
unsigned short sin_family; // Internet protocol (AF_INET)
unsigned short sin_port; // Address port (16 bits)
struct in_addr sin_addr; // Internet address (32 bits)
char sin_zero[8]; // Not used
}
sockaddr_in can be casted to a sockaddr
int status = bind(sockid, &addrport, size);
addrport: struct sockaddr, the (IP) address and port of the machine for TCP/IP server, internet address is usually set to INADDR_ANY, i.e.,
chooses any incoming interface
int sockid;
struct sockaddr_in addrport;
sockid = socket(PF_INET, SOCK_STREAM, 0);
addrport.sin_family = AF_INET;
addrport.sin_port = htons(5100);
addrport.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockid, (struct sockaddr *) &addrport, sizeof(addrport))!= -1) {
…}
int status = listen(sockid, queueLimit);
- is never used for sending and receiving
- is used by the server only as a way to get new sockets
int status = connect(sockid, &foreignAddr, addrlen);
int s = accept(sockid, &clientAddr, &addrLen);
- filled in upon return
- must be set appropriately before call
- adjusted upon return
- is blocking: waits for connection before returning
- dequeues the next connection on the queue for socket (sockid)
int count = send(sockid, msg, msgLen, flags);
int count = recv(sockid, recvBuf, bufLen, flags);
- returns only after data is sent / received