Yep, cool. However, the server side wont bind to it. Keep getting error: Unalble to bind to port??
Here is the code. I am real new to sockets (so i have probably left out a few bits here)....but this compiles and runs. Except for the error on bind. If i use any other number for the ip it runs...
Code:
#include <winsock.h>
#include <stdio.h>
#include <conio.h>
#define SERVERPORT 4545
#define BACKLOG 20
struct sockaddr_in sAddress;
int clientHandles[BACKLOG];
struct sockaddr_in cAddress[BACKLOG];
int connectionCount=0;
struct hostent *he;
int socketHandle;
WSADATA wsaData; // use to inititalize winsock
int main(void){
if(!WSAStartup(MAKEWORD(1,1), &wsaData)==0)
return (0);
he = gethostbyname("lucifer");
if(!(socketHandle= socket(AF_INET, SOCK_STREAM, 0))){
printf("\nERR: Unable to create socket...");
while(!kbhit());
return(0);
}
sAddress.sin_family=AF_INET;
sAddress.sin_port = htons(SERVERPORT);
sAddress.sin_addr.S_un.S_addr= inet_addr ("127.0.0.1");//16177343;
memset(&(sAddress.sin_zero), '\0', 8);
// THIS IS WHERE IT STOPS WITH IP 127.0.0.1 ??
if(!bind(socketHandle, (struct sockaddr *)&sAddress, sizeof(struct sockaddr))){
int errorCode=WSAGetLastError();
printf("\nERR %i: Unable to bind to address...", errorCode);
while(!kbhit());
return (0);
}
// set the backlog for how many clients the server accepts
if(!listen(socketHandle, BACKLOG)){
printf("\nERR: Unable to listen to socket...");
while(!kbhit());
return (0);
}
char hostname[50];
gethostname(hostname, sizeof(hostname));
printf("Server Information:\n Family: %i --> Port: %i --> Address: %s ServerName: %s\n\n Waiting for connections...\n\n", sAddress.sin_family,
sAddress.sin_port,
inet_ntoa(sAddress.sin_addr),
hostname);
while(1){
int sin_size = sizeof(struct sockaddr_in);
if((clientHandles[connectionCount] =
accept(socketHandle, (struct sockaddr *)&cAddress[connectionCount], &sin_size))!=-1){
printf("\n\nAccepted Connection: \n\n ClintId: %i\n", clientHandles[connectionCount]);
send(clientHandles[0], "Welcome to RIO Server!", sizeof("Welcome to RIO Server"), 0);
connectionCount++;
}
if(kbhit())
break;
}
closesocket (socketHandle);
WSACleanup();
return(0);
}