Discussion:
where is a error in this codes.
(too old to reply)
li
2010-08-20 05:41:48 UTC
Permalink
Raw Message
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?
Pavel A.
2010-08-21 22:53:38 UTC
Permalink
Raw Message
"li" <***@gmail.com> wrote in message news:***@god...
Do you understand my meaning?
It looks like you're trying to get a Linux question answered in a Windows
newsgroup?

--pa
li
2010-08-22 06:40:46 UTC
Permalink
Raw Message
在 2010-08-22日的 01:53 +0300,Pavel A.写道:
Post by li
Do you understand my meaning?
It looks like you're trying to get a Linux question answered in a Windows
newsgroup?
--pa
^_^haha, The question was a Platform-independent. Do you know address
for Linux newsgroup?

Loading...