I added a printf to see if I was getting from the socket. It just came across with a bunch of @@@@@ which leds me to believe that i'm not reading from the socket right.
I'm using visual c++. This is the code thats giving me the trouble.
Code:
void check_autoroller(void)
{
/* this autoroller is for DM only! */
int stat;
char from[500];
int str, in, wis, dex, con;
int AUTOROLLING = TRUE;
printf("Max str?\n\r");
scanf("%d",&str);
printf("Max int?\n\r");
scanf("%d",&in);
printf("Max wis?\n\r");
scanf("%d",&wis);
printf("Max dex?\n\r");
scanf("%d",&dex);
printf("Max con?\n\r");
scanf("%d",&con);
read_from_socket(from, 1038);
printf("%s",from);
while (AUTOROLLING != FALSE)
{
if (from[0] != 'S')
return;
// str
stat = (from[10]-48) * 10 + from[11]-48;
if (stat < str){
write_to_socket("no\n");
return;
}
// int
stat = (from[27]-48) * 10 + from[28]-48;
if (stat < in){
write_to_socket("no\n");
return;
}
// wis
stat = (from[38]-48) * 10 + from[39]-48;
if (stat < wis){
write_to_socket("no\n");
return;
}
// dex
stat = (from[52]-48) * 10 + from[53]-48;
if (stat < dex){
write_to_socket("no\n");
return;
}
// con
stat = (from[69]-48) * 10 + from[70]-48;
if (stat < con){
write_to_socket("no\n");
return;
}
write_to_socket("yes\n");
AUTOROLLING = FALSE;
return;
}
}
int main(void)
{
char host[50];
int port;
printf("Enter the host to connect to.\n\r");
gets(host);
port = 2222;
init_winsock();
connect_to_host(host,port);
do_specs();
send_spec();
printf("specs sent. Initiating autorolling.\n\r");
check_autoroller();
close_connection();
shutdown_winsock();
return 0;
}
this is the socket functions.
Code:
void close_connection(void)
{
closesocket(sock);
}
int init_winsock(void)
{
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
wVersionRequested = MAKEWORD(1,1);
WSACleanup();
WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
return false;
}
return true;
}
int connect_to_host(char *hostname, int port)
{
LPHOSTENT lpHostEntry;
SOCKADDR_IN saServer;
int arg;
unsigned long arg2;
// Perform DNS lookup
lpHostEntry = gethostbyname(hostname);
if (lpHostEntry == NULL)
{
return DNS_FAILURE;
}
// Create the socket
sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
// set the recv buf to 4k
arg = 4096;
setsockopt(sock,SOL_SOCKET,SO_RCVBUF,(char *) &arg,sizeof(arg));
// set the send buf to 1k
arg = 1024;
setsockopt(sock, SOL_SOCKET,SO_SNDBUF, (char *) &arg, sizeof(arg));
if (sock == INVALID_SOCKET)
{
return SOCKET_FAILURE;
}
// Connect to host
saServer.sin_family = PF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(port);
if ( connect(sock,
(LPSOCKADDR)&saServer,
sizeof(struct sockaddr)) == SOCKET_ERROR)
{
closesocket(sock);
return CONNECT_FAILURE;
}
// Turn off blocking mode
ioctlsocket(sock,FIONBIO,&arg2);
return true;
}
int read_from_socket(char * buf, int bufsize)
{
int cnt;
// unsigned long arg;
// if (ioctlsocket(sock, FIONREAD, &arg) == SOCKET_ERROR)
// return -1;
// if (arg <= 0) return false;
cnt = recv(sock,buf, bufsize,0);
if (cnt == SOCKET_ERROR || cnt == 0)
{
int error;
error = WSAGetLastError();
// Might should check for other error codes too, but these seem most likely
// to indicate lost connection
if (error == WSAENETRESET ||
error == WSAECONNABORTED ||
error == WSAECONNRESET ||
error == WSAEINVAL ||
cnt == 0)
return -1;
return false;
}
buf[cnt] = '\0';
return true;
}
void write_to_socket(char * buf)
{
send(sock, buf, strlen(buf),0);
return;
}
void shutdown_winsock(void)
{
// cleanup winsock
closesocket(sock);
WSACleanup();
return;
}