li
2010-08-20 05:41:48 UTC
On linux This is a example source code from "TCP/IP Sockets in C"
website :http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/code/code/TCPServerUtility.c
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "Practical.h"
static const int MAXPENDING = 5; // Maximum outstanding connection requests
int SetupTCPServerSocket(const char *service) {
// Construct the server address structure
struct addrinfo addrCriteria; // Criteria for address match
memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
addrCriteria.ai_family = AF_UNSPEC; // Any address family
addrCriteria.ai_flags = AI_PASSIVE; // Accept on any address/port
addrCriteria.ai_socktype = SOCK_STREAM; // Only stream sockets
addrCriteria.ai_protocol = IPPROTO_TCP; // Only TCP protocol
struct addrinfo *servAddr; // List of server addresses
int rtnVal = getaddrinfo(NULL, service, &addrCriteria, &servAddr);
if (rtnVal != 0)
DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));
int servSock = -1;
for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
// Create a TCP socket
servSock = socket(servAddr->ai_family, servAddr->ai_socktype,servAddr->ai_protocol); //error??? the servAddr is always first node for everyloops.
//I think here use "addr" replacement of "servAddr"
//modify: servSock = socket(addr->ai_family, addr->ai_sockyped, addr->ai_protocol) is right ?
if (servSock < 0)
continue; // Socket creation failed; try next address
// Bind to the local address and set socket to list
if ((bind(servSock, servAddr->ai_addr, servAddr->ai_addrlen) == 0) &&
(listen(servSock, MAXPENDING) == 0)) //modify: bind(servSock, addr->ai_addr, servAddr->ai_addrlen
{
// Print local address of socket
struct sockaddr_storage localAddr;
socklen_t addrSize = sizeof(localAddr);
if (getsockname(servSock, (struct sockaddr *) &localAddr, &addrSize) < 0)
DieWithSystemMessage("getsockname() failed");
fputs("Binding to ", stdout);
PrintSocketAddress((struct sockaddr *) &localAddr, stdout);
fputc('\n', stdout);
break; // Bind and list successful
}
close(servSock); // Close and try again
servSock = -1;
}
// Free address list allocated by getaddrinfo()
freeaddrinfo(servAddr);
return servSock;
}
________________________________________________________________________
The for() loop Effect is find a success addrinfo structur form list.
The "addr" not useed.
Do you understand my meaning?
website :http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/code/code/TCPServerUtility.c
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "Practical.h"
static const int MAXPENDING = 5; // Maximum outstanding connection requests
int SetupTCPServerSocket(const char *service) {
// Construct the server address structure
struct addrinfo addrCriteria; // Criteria for address match
memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
addrCriteria.ai_family = AF_UNSPEC; // Any address family
addrCriteria.ai_flags = AI_PASSIVE; // Accept on any address/port
addrCriteria.ai_socktype = SOCK_STREAM; // Only stream sockets
addrCriteria.ai_protocol = IPPROTO_TCP; // Only TCP protocol
struct addrinfo *servAddr; // List of server addresses
int rtnVal = getaddrinfo(NULL, service, &addrCriteria, &servAddr);
if (rtnVal != 0)
DieWithUserMessage("getaddrinfo() failed", gai_strerror(rtnVal));
int servSock = -1;
for (struct addrinfo *addr = servAddr; addr != NULL; addr = addr->ai_next) {
// Create a TCP socket
servSock = socket(servAddr->ai_family, servAddr->ai_socktype,servAddr->ai_protocol); //error??? the servAddr is always first node for everyloops.
//I think here use "addr" replacement of "servAddr"
//modify: servSock = socket(addr->ai_family, addr->ai_sockyped, addr->ai_protocol) is right ?
if (servSock < 0)
continue; // Socket creation failed; try next address
// Bind to the local address and set socket to list
if ((bind(servSock, servAddr->ai_addr, servAddr->ai_addrlen) == 0) &&
(listen(servSock, MAXPENDING) == 0)) //modify: bind(servSock, addr->ai_addr, servAddr->ai_addrlen
{
// Print local address of socket
struct sockaddr_storage localAddr;
socklen_t addrSize = sizeof(localAddr);
if (getsockname(servSock, (struct sockaddr *) &localAddr, &addrSize) < 0)
DieWithSystemMessage("getsockname() failed");
fputs("Binding to ", stdout);
PrintSocketAddress((struct sockaddr *) &localAddr, stdout);
fputc('\n', stdout);
break; // Bind and list successful
}
close(servSock); // Close and try again
servSock = -1;
}
// Free address list allocated by getaddrinfo()
freeaddrinfo(servAddr);
return servSock;
}
________________________________________________________________________
The for() loop Effect is find a success addrinfo structur form list.
The "addr" not useed.
Do you understand my meaning?