I have been working on some socket related programming for a windows platform and I have found that my programs only work when I am connected to a router. I originally installed windows on a laptop (SP) so I could test a pair of UDP programs (attached) but I did not want to install security programs on the machine so I decided not to EVER connect it to the internet. However, I found that unless I connected the machine to at least a router, the programs would not work. I thought they would communicate over localhhost no matter what, but that does not appear to be true and I don't understand why.
When connected to the other machine, this is what the ipconfig and routing table looks like:
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Autoconfiguration IP Address. . . : 169.254.212.222
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
=========================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...00 11 43 cc aa 7f ...... Broadcom 570x Gigabit Integrated Controller - Packet Scheduler Miniport
==========================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
169.254.0.0 255.255.0.0 169.254.212.222 169.254.212.222 20
169.254.212.222 255.255.255.255 127.0.0.1 127.0.0.1 20
169.254.255.255 255.255.255.255 169.254.212.222 169.254.212.222 20
224.0.0.0 240.0.0.0 169.254.212.222 169.254.212.222 20
255.255.255.255 255.255.255.255 169.254.212.222 169.254.212.222 1
=======================================
Persistent Routes:
None
When I unplug the connection, this is the results:
Ethernet adapter Local Area Connection:
Media State . . . . . . . . . . . : Media disconnected
================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...00 11 43 cc aa 7f ...... Broadcom 570x Gigabit Integrated Controller - Packet Scheduler Miniport
================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
255.255.255.255 255.255.255.255 255.255.255.255 2 1
================================================
Persistent Routes:
None
Here are the short test programs I wrote:
//------------------------------------------
// client.c
//------------------------------------------
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 1000
#define JMAX 24
#define IMAX 10
int main(int argc, char *argv[])
{
WSADATA wsadata;
int sd, rc;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
char teststring[MAX_MSG];
unsigned char i,j,k;
for(i=0;i<IMAX;i++)
{
for(j=0;j<JMAX;j++)
{
teststring[((25*i)+j)] = j + 65 ;
}
}
teststring[IMAX*JMAX]=0x00;
WSAStartup(MAKEWORD(2,2),&wsadata);
if(argc<2)
{
argv[1]="localhost";
}
printf("argv[1] = %s\n",argv[1]);
h = gethostbyname(argv[1]);
if(h==NULL)
{
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == sd)
{
perror("Could not create socket");
exit(1);
}
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0)
{
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
printf("%s\n",teststring);
for(i=0;i<255;i++)
{
rc = sendto(sd, teststring, 950, 0, (struct sockaddr *) &remoteServAddr, sizeof remoteServAddr);
if(rc<0)
{
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);
exit(1);
}
}
closesocket(sd);
WSACleanup();
return 1;
}
//-------------------------------------------
// server.c
//-------------------------------------------
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 1000
#define RECORDSIZE 240
struct timeval tim;
double tStart, tStop, tDelta, tTotal, tThis, tPrev;
unsigned int i;
int main(int argc, char *argv[])
{
WSADATA wsadata;
int sd, rc, n, nBytes;
int cliLen;
struct sockaddr_in cliAddr, servAddr;
char msg[MAX_MSG];
LARGE_INTEGER lPreTime, lPostTime, lFrequency;
float lPassTick;
float lPassTime;
LARGE_INTEGER lStartTime, lStopTime;
WSAStartup(MAKEWORD(2,2), &wsadata);
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == sd)
{
perror("Could not create socket");
exit(1);
}
servAddr.sin_family = AF_INET; //bind local server port
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(LOCAL_SERVER_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0)
{
printf("%s: cannot bind port number %d \n", argv[0], LOCAL_SERVER_PORT);
exit(1);
}
i=0;
printf("Listening on Port %u for UDP packets\n", LOCAL_SERVER_PORT);
while(i<254)
{
memset(msg,0x0,MAX_MSG);
cliLen = sizeof(cliAddr);
n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
if(n<0)
{
printf("%s: cannot receive data \n",argv[0]);
continue;
}
i++;
if(i==2)
{
QueryPerformanceCounter(&lPreTime);
}
if(i==201)
{
QueryPerformanceCounter(&lPostTime);
}
}
if(i=201)
{
QueryPerformanceFrequency(&lFrequency);
lPassTick = (float)(lPostTime.QuadPart - lPreTime.QuadPart);
lPassTime = (float)lPassTick/(float)lFrequency.QuadPart;
nBytes = RECORDSIZE*200;
printf("tTotal = %0.6lf ntotal = %d Data Rate = %0.2lf MB/s\n",lPassTime,nBytes,(float)nBytes/(lPassTime*1000000));
//return 0;
}
closesocket(sd);
QueryPerformanceCounter(&lStartTime);
//sleep(10000);
QueryPerformanceCounter(&lStopTime);
lPassTick = (float)(lStopTime.QuadPart - lStartTime.QuadPart);
lPassTime = (float)lPassTick/(float)lFrequency.QuadPart;
printf("ten sec = %0.6lf sec\n",lPassTime);
WSACleanup();
}
When connected to the other machine, this is what the ipconfig and routing table looks like:
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Autoconfiguration IP Address. . . : 169.254.212.222
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
=========================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...00 11 43 cc aa 7f ...... Broadcom 570x Gigabit Integrated Controller - Packet Scheduler Miniport
==========================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
169.254.0.0 255.255.0.0 169.254.212.222 169.254.212.222 20
169.254.212.222 255.255.255.255 127.0.0.1 127.0.0.1 20
169.254.255.255 255.255.255.255 169.254.212.222 169.254.212.222 20
224.0.0.0 240.0.0.0 169.254.212.222 169.254.212.222 20
255.255.255.255 255.255.255.255 169.254.212.222 169.254.212.222 1
=======================================
Persistent Routes:
None
When I unplug the connection, this is the results:
Ethernet adapter Local Area Connection:
Media State . . . . . . . . . . . : Media disconnected
================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...00 11 43 cc aa 7f ...... Broadcom 570x Gigabit Integrated Controller - Packet Scheduler Miniport
================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
255.255.255.255 255.255.255.255 255.255.255.255 2 1
================================================
Persistent Routes:
None
Here are the short test programs I wrote:
//------------------------------------------
// client.c
//------------------------------------------
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 1000
#define JMAX 24
#define IMAX 10
int main(int argc, char *argv[])
{
WSADATA wsadata;
int sd, rc;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
char teststring[MAX_MSG];
unsigned char i,j,k;
for(i=0;i<IMAX;i++)
{
for(j=0;j<JMAX;j++)
{
teststring[((25*i)+j)] = j + 65 ;
}
}
teststring[IMAX*JMAX]=0x00;
WSAStartup(MAKEWORD(2,2),&wsadata);
if(argc<2)
{
argv[1]="localhost";
}
printf("argv[1] = %s\n",argv[1]);
h = gethostbyname(argv[1]);
if(h==NULL)
{
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name, inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == sd)
{
perror("Could not create socket");
exit(1);
}
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0)
{
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
printf("%s\n",teststring);
for(i=0;i<255;i++)
{
rc = sendto(sd, teststring, 950, 0, (struct sockaddr *) &remoteServAddr, sizeof remoteServAddr);
if(rc<0)
{
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);
exit(1);
}
}
closesocket(sd);
WSACleanup();
return 1;
}
//-------------------------------------------
// server.c
//-------------------------------------------
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#define LOCAL_SERVER_PORT 1500
#define MAX_MSG 1000
#define RECORDSIZE 240
struct timeval tim;
double tStart, tStop, tDelta, tTotal, tThis, tPrev;
unsigned int i;
int main(int argc, char *argv[])
{
WSADATA wsadata;
int sd, rc, n, nBytes;
int cliLen;
struct sockaddr_in cliAddr, servAddr;
char msg[MAX_MSG];
LARGE_INTEGER lPreTime, lPostTime, lFrequency;
float lPassTick;
float lPassTime;
LARGE_INTEGER lStartTime, lStopTime;
WSAStartup(MAKEWORD(2,2), &wsadata);
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == sd)
{
perror("Could not create socket");
exit(1);
}
servAddr.sin_family = AF_INET; //bind local server port
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(LOCAL_SERVER_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0)
{
printf("%s: cannot bind port number %d \n", argv[0], LOCAL_SERVER_PORT);
exit(1);
}
i=0;
printf("Listening on Port %u for UDP packets\n", LOCAL_SERVER_PORT);
while(i<254)
{
memset(msg,0x0,MAX_MSG);
cliLen = sizeof(cliAddr);
n = recvfrom(sd, msg, MAX_MSG, 0, (struct sockaddr *) &cliAddr, &cliLen);
if(n<0)
{
printf("%s: cannot receive data \n",argv[0]);
continue;
}
i++;
if(i==2)
{
QueryPerformanceCounter(&lPreTime);
}
if(i==201)
{
QueryPerformanceCounter(&lPostTime);
}
}
if(i=201)
{
QueryPerformanceFrequency(&lFrequency);
lPassTick = (float)(lPostTime.QuadPart - lPreTime.QuadPart);
lPassTime = (float)lPassTick/(float)lFrequency.QuadPart;
nBytes = RECORDSIZE*200;
printf("tTotal = %0.6lf ntotal = %d Data Rate = %0.2lf MB/s\n",lPassTime,nBytes,(float)nBytes/(lPassTime*1000000));
//return 0;
}
closesocket(sd);
QueryPerformanceCounter(&lStartTime);
//sleep(10000);
QueryPerformanceCounter(&lStopTime);
lPassTick = (float)(lStopTime.QuadPart - lStartTime.QuadPart);
lPassTime = (float)lPassTick/(float)lFrequency.QuadPart;
printf("ten sec = %0.6lf sec\n",lPassTime);
WSACleanup();
}