Look up Type Casting in c good luck.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
void FreeSocketInformation(DWORD Index)
{
LPSOCKET_INFORMATION SI = SocketList[Index];
DWORD i;
SI->loggedin=false;
if(SI->username!=NULL){
printf("%s:%d has Disconnected!\n",SI->username,Index);
}
closesocket(SI->Socket);
printf("Closing socket\n");
GlobalFree(SI);
// Remove from the socket array
for (i = Index; i < TotalSockets; i++)
{
SocketList[i] = SocketList[i + 1];
}
TotalSockets--;
}
typedef struct _MYSOCKET_INFORMATION {
CHAR Buffer[BUFFERSIZE];
char* username;
bool loggedin;
char* xaxis,* yaxis,* world;
WSABUF DataBuf;
SOCKET Socket;
DWORD SendBytes;
DWORD RecvBytes;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
LPSOCKET_INFORMATION SocketList[FD_SETSIZE];
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <string.h>
#include <time.h>
#include <fstream>
#include <sstream>
#include <mysql/mysql.h>
#include <exception>
#include <stdio.h>
using namespace std;
#include "resources.h"
#define PORT 1030
#define BUFFERSIZE 8192
typedef struct _MYSOCKET_INFORMATION {
CHAR Buffer[BUFFERSIZE];
char* username;
bool loggedin;
char* xaxis,* yaxis,* world;
WSABUF DataBuf;
SOCKET Socket;
DWORD SendBytes;
DWORD RecvBytes;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;
BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Index);
DWORD TotalSockets = 0;
LPSOCKET_INFORMATION SocketList[FD_SETSIZE];
string datanum[13][2]={{"1002","Login Checking"},
{"1003","New User"}, //New User//
{"1001","Chat"}, //Chat//
{"1004","Online Check"}, //Online Check//
{"1005","Userlsit Update"}, //Userlist Update//
{"1006","Wisper"}, //Wisper//-Under Construction
{"1007","Movement"}, //Movement//
{"1008","Update"}, //Update//
{"1009","Name Check"}, //Name Check//
{"1011","World Change"}, //World Change//
{"1013","Server Broadcast"}, //Server Broadcast//
{"1015","Server MOTD"},//Server MOTD//
{"1016","NPC Drop"}}; //NPC Set//
int main()
{
SOCKET ListenSocket;
SOCKET AcceptSocket;
SOCKADDR_IN InternetAddr;
WSADATA wsaData;
FD_SET Writer;
FD_SET Reader;
ULONG NonBlock;
DWORD Flags;
int Ret;
if ((Ret = WSAStartup(MAKEWORD(2,0),&wsaData)) != 0)
{
//startup failed
}
// Create a socket for the winsock tutorial.
if ((ListenSocket = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0,
WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
{
printf("Winsock tutorial error: WSASocket() failed %d\n", WSAGetLastError());
return 0;
}
InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);
if (bind(ListenSocket, (SOCKADDR *) &InternetAddr, sizeof(InternetAddr))== SOCKET_ERROR)
{
printf("Winsock tutorial error: Binding failed %d\n", WSAGetLastError());
return 0;
}
if (listen(ListenSocket, 5))
{
printf("Winsock tutorial error: listen failed %d\n", WSAGetLastError());
return 0;
}
// Change the socket mode on the listening socket from blocking to non-block
NonBlock = 1;
if (ioctlsocket(ListenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("ioctlsocket() failed \n");
return 0;
}
while(TRUE)
{
// Initialize the Read and Write socket set.
FD_ZERO(&Reader);
FD_ZERO(&Writer);
// Check for connection attempts.
FD_SET(ListenSocket, &Reader);
// Set Read and Write notification for each socket based on the
// current state the buffer.
int Total;
for (int i = 0; i < TotalSockets; i++)
if (SocketList[i]->RecvBytes > SocketList[i]->SendBytes)
FD_SET(SocketList[i]->Socket, &Writer);
else
FD_SET(SocketList[i]->Socket, &Reader);
if (( Total = select(0, &Reader, &Writer, NULL, NULL)) == SOCKET_ERROR)
{
printf("Winsock tutorial error: select function returned with error %d\n", WSAGetLastError());
return 0;
}
// Check for arriving connections on the listening socket.
if (FD_ISSET(ListenSocket, &Reader))
{
Total--;
if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET)
{
// Set the accepted socket to non-blocking mode so the server will
// not get caught in a blocked condition on WSASends
NonBlock = 1;
if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
printf("Winsock tutorial error: ioctlsocket() failed with error %d\n", WSAGetLastError());
return 0;
}
if (CreateSocketInformation(AcceptSocket) == FALSE)
return 0;
}
else
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("accept() failed with error %d\n", WSAGetLastError());
return 0;
}
}
}
// Check each socket for Read and Write notification for Total number of sockets
for (int i = 0; Total > 0 && i < TotalSockets; i++)
{
LPSOCKET_INFORMATION SocketInfo = SocketList[i];
// If the Reader is marked for this socket then this means data
// is available to be read on the socket.
if (FD_ISSET(SocketInfo->Socket, &Reader))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = BUFFERSIZE;
Flags = 0;
DWORD RecvBytes;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("Winsock tutorial: Receive failed with error\n");
FreeSocketInformation(i);
}
continue;
}
else
{
SocketInfo->RecvBytes = RecvBytes;
//printf("%s\n",SocketInfo->DataBuf.buf);
string data =(string)SocketInfo->DataBuf.buf;
string check = data.substr(0, 4);
if(datanum[0][0].compare(check)==0 && SocketInfo->loggedin!=true){
int loc = data.find( "@", 0 );
char user[MAX_PATH];
char pass[MAX_PATH];
string src=data.substr(4,loc-4);
int ploc=data.find("@",loc-4);
string psrc=data.substr(5+src.size(),data.size()-src.size()-6);
sprintf(user,"%s",src.c_str());
sprintf(pass,"%s",psrc.c_str());
SocketInfo->username = user;
bool res=mysql_check(user,pass,SocketInfo->Socket);
if(res==true){
SocketInfo->loggedin=true;
}
}
for (int b = 0; b < TotalSockets; b++){
LPSOCKET_INFORMATION SocketInf = SocketList[b];
if(SocketInfo->Socket!=SocketInf->Socket && SocketInfo->loggedin==true && SocketInf->loggedin!=false){
send(SocketInf->Socket,SocketInfo->DataBuf.buf,SocketInfo->RecvBytes,0);
printf("Recieved Data From %s\n",SocketInfo->username);
}
}
// If zero bytes are received, this indicates connection is closed.
if (RecvBytes == 0)
{
FreeSocketInformation(i);
continue;
}
}
}
// If the Writer is marked on this socket then this means the internal
// data buffers are available for more data.
if (FD_ISSET(SocketInfo->Socket, &Writer))
{
Total--;
SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes;
SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes;
printf("%s\n",SocketInfo->username);
DWORD SendBytes;
if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("Send failed with error\n");
FreeSocketInformation(i);
}
continue;
}
else
{
SocketInfo->SendBytes += SendBytes;
if (SocketInfo->SendBytes == SocketInfo->RecvBytes)
{
SocketInfo->SendBytes = 0;
SocketInfo->RecvBytes = 0;
}
}
}
}
}
}
BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;
printf("Accepted socket\n");
if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("Winsock tutorial error: GlobalAlloc() failed\n");
return FALSE;
}
// Prepare SocketInfo structure for use.
SI->Socket = s;
SI->SendBytes = 0;
SI->RecvBytes = 0;
SI->loggedin = false;
SocketList[TotalSockets] = SI;
TotalSockets++;
return(TRUE);
}
void FreeSocketInformation(DWORD Index)
{
LPSOCKET_INFORMATION SI = SocketList[Index];
DWORD i;
SI->loggedin=false;
if(SI->username!=NULL){
printf("%s:%d has Disconnected!\n",SI->username,Index);
}
closesocket(SI->Socket);
printf("Closing socket\n");
GlobalFree(SI);
// Remove from the socket array
for (i = Index; i < TotalSockets; i++)
{
SocketList[i] = SocketList[i + 1];
}
TotalSockets--;
}
Page created in 0.175 seconds with 16 queries.