Unicode Help

This is a discussion on Unicode Help within the C++ Programming forums, part of the General Programming Boards category; Hi, im writing a packet analzyer and well I got some progress. Then I realized I was not getting anywere ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Unicode Help

    Hi, im writing a packet analzyer and well I got some progress. Then I realized I was not getting anywere with it.


    It listens to the server and gets a packet. Ill put a //***HERE*** in the code when it STOPS working.


    Code:
    
    // Standard Includes
    #include <iostream>
    #include <winsock.h>
    #include <stdio.h>
    #include "packets.h"
    using namespace std;
    //
    // ----> Main Program Function (REQUIRED)
    //
    
    void PacketDisplay(char *buf,int iBytesSent);
    
    int main()
    {
    	SOCKET		skSocket;
    	sockaddr_in saServerAddress;
    	int			iPort = 2593,iStatus;
    	WSADATA		wsaData;
    	WORD		wVersionRequested;
    	LPHOSTENT	lpHost;
    	char		szHost[128];
    	char		szSendBuffer[256];
        char szRecvBuffer[32768];
    	int			iBytesSent;
    	int			iBytesReceived;
    	//Ricks ........
    	int i;
    	stLogin LoginPacket = { 0 };
    	stHuepacket HuePacket = { 0 };
    	stServerSelect ServerSelect = {0};
    	stGameLogin GameLogin = {0};
    	stServerKey ServerKey = {0};
    	stGeneric GenericPacket = {0};
    	stCharacterSelect CharacterSelect = {0};
    	stTalkRequest TalkRequest = {0};
    	stMoveRequest MoveRequest = {0};
    	stMoveRejected MoveRejected = {0};
    	stRedirect Redirect = {0};
    	MoveRequest.cmd = 2;
    	MoveRequest.direction = 2;
    	MoveRequest.sequence = 125;
        
    	int iBytesRecv = 0;
      
        TalkRequest.cmd.cmd = 0xAD;
    	//00 26 00 00 26 00 03
    	TalkRequest.exact[1] = 0x00;
    	TalkRequest.exact[2] = 0x26;
    	TalkRequest.exact[3] = 0x00;
    	TalkRequest.exact[4] = 0x00;
    	TalkRequest.exact[5] = 026;
    	TalkRequest.exact[6] = 0x00;
    	TalkRequest.exact[7] = 0x03;
    	//00 48 00
        TalkRequest.Unkown1[1] = 0x00;
    	TalkRequest.Unkown1[2] = 0x48;
    	TalkRequest.Unkown1[3] = 0x00;
    	/*TalkRequest.size = 0x0026;
    	TalkRequest.type = 0x00;
    	TalkRequest.color = 0x0026;
    	TalkRequest.font = 0x0003;*/
    	char Languages[4] = "ENU";
    	char msgs[30] = "HELLO WORLD";
    
    	strcpy(TalkRequest.msg,msgs);
    	strcpy(TalkRequest.Language,Languages);
    
    	GameLogin.cmd.cmd = 0x91;
    
    	ServerSelect.One = 160;
    
    	HuePacket.One = 192;
    	HuePacket.Two = 168;
    	HuePacket.Thee = 1;
    	HuePacket.Four = 103;
    
    	char OverflowMainLogin[62];
    	char OverflowHue[4];
    	char OverflowServerSelect[3];
    	char OverflowLogin[35];
    	char OverflowCharSelect[73];
    	char OverflowTalkRequest[42];
    	char OverflowMove[7];
    
    	char RecvBuffer[40];
        char DataPacketiP[4];
    	char Uname[30] = "Name";
    	char Pword[30] = "Password";
    	char CharName[30] = "Rhy";
    	sprintf(szHost,"192.168.1.103");
    
    	CharacterSelect.cmd = 93;
    	
    
    	LoginPacket.i = 128;
    	LoginPacket.Unkown1 = 93;
    
    	strcpy(LoginPacket.Username,Uname);
    	strcpy(LoginPacket.Password,Pword);
    
    	strcpy(CharacterSelect.CharName,CharName);
    
    	strcpy(GameLogin.Username,Uname);
    	strcpy(GameLogin.Password,Pword);
        //Ricks ........ end
    
    	// Init the host value, change this IP to whatever valid IP you wish
    	
        
    	// Tell WinSock we want version 2
    	wVersionRequested = MAKEWORD( 2, 0 );
    
    	// Initialize the socket handle
    	skSocket = INVALID_SOCKET;
    
    	// Startup WinSock
    	iStatus = WSAStartup( wVersionRequested, &wsaData );
    
    	// Create the socket
    	skSocket = socket( AF_INET, SOCK_STREAM, 0 );
    	
    	// Check if there was an error
    	if( skSocket == INVALID_SOCKET ) {
    		cout << "**ERROR** Could Not Create Socket" << endl;
    		// Clean up WinSock
    		WSACleanup();
    		exit(1);
    	}
    
    	cout << "<-- SOCKET CREATED -->" << endl;
    
    	// Initialize the server address data structure
    	memset(&saServerAddress,0,sizeof(sockaddr_in));
    	// Set this by default
    	saServerAddress.sin_family = AF_INET;
    	// Load the IP Address
    	saServerAddress.sin_addr.s_addr = inet_addr(szHost);
    
    	// If the host specified is not an IP Address we must look up the value
    	if( saServerAddress.sin_addr.s_addr == INADDR_NONE )
    	{
    		cout << "<-- LOOKING UP HOST IP -->" << endl;
    		// Get the host name
    		lpHost = gethostbyname(szHost);
    		// Check if we got something back
    		if (lpHost != NULL) {
    			// Load the server address with the host information
    			saServerAddress.sin_addr.s_addr = ((LPIN_ADDR)lpHost->h_addr)->s_addr;
    		}
    		else {
    			cout << "**ERROR** Could Not Locate Host" << endl;
    			// Clean up WinSock
    			WSACleanup();
    			exit(1);
    		}
    	}
    
    	// Set the Server Port
    	saServerAddress.sin_port = htons(iPort);
    	// Attempt to connect to the server
    	iStatus = connect(skSocket, (struct sockaddr*)&saServerAddress,sizeof(sockaddr));
    	
    	// Check if there was an error
    	if( iStatus == SOCKET_ERROR ) {
    		cout << "**ERROR** Could Not Connect To Server" << endl;
    		// Clean up WinSock
    		WSACleanup();
    		exit(1);
    	}
    
    	cout << "<-- CONNECTED TO SERVER -->" << endl;
        
    	// Load the data to send
    	// Send the HTTP Request
    	    memcpy(OverflowMainLogin,&LoginPacket,sizeof(stLogin));
    		memcpy(OverflowHue,&HuePacket,sizeof(stHuepacket));
    		//memcpy(OverflowLogin,&GameLogin,sizeof(stGameLogin));
    		memcpy(OverflowCharSelect,&CharacterSelect,sizeof(stCharacterSelect));
    		memcpy(OverflowServerSelect,&ServerSelect,sizeof(stServerSelect));
    		memcpy(OverflowTalkRequest,&TalkRequest,sizeof(TalkRequest));
    
    
    	iBytesSent = send(skSocket,OverflowHue,sizeof(OverflowHue),0);
    
    	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowHue,	iBytesSent);
    
    	iBytesSent = send(skSocket,OverflowMainLogin,sizeof(OverflowMainLogin),0);
    
    	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowMainLogin,	iBytesSent);
    	// Wait for incoming data
    
    	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);
    
    	cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;
         memcpy(&GenericPacket,szRecvBuffer,sizeof(stGeneric));
    	// Output the data received
         PacketDisplay(szRecvBuffer,iBytesReceived);
    
    	// Close the socket
     
    	iBytesSent = send(skSocket,OverflowServerSelect,sizeof(stServerSelect),0);
    	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowServerSelect,	iBytesSent);
       
    	system("pause");
    
    	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);
        cout << "<-- RECEIVED REDIRECT " << iBytesReceived << " BYTES -->" << endl;
    	memcpy(&Redirect,szRecvBuffer,sizeof(stRedirect));
    	PacketDisplay(szRecvBuffer,iBytesReceived);
        
    	GameLogin.key[0] = Redirect.key[0];
    	GameLogin.key[1] = Redirect.key[1];
    	GameLogin.key[2] = Redirect.key[2];
    	GameLogin.key[3] = Redirect.key[3];
         
    	memcpy(OverflowLogin,&GameLogin,sizeof(stGameLogin));
    	iBytesSent = send(skSocket,OverflowLogin,sizeof(stGameLogin),0);
    	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowLogin,	iBytesSent);
       system("pause");
    	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);
        cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;
         
    	system("pause");
    
    	iBytesSent = send(skSocket,OverflowCharSelect,sizeof(stCharacterSelect),0);
    	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowCharSelect,	iBytesSent);
    
        //****HERE**** packets no longer make sence.
        int Wait = 0;
    	while( Wait != 1){
               	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);
    			 cout << "BYTES " << iBytesSent << endl;
    			 memcpy(&GenericPacket,szRecvBuffer,sizeof(stGeneric));
    			 cout << " Packet Id: " << hex<<unsigned short(szRecvBuffer[0]) << endl;
    			PacketDisplay(szRecvBuffer,iBytesReceived);
    
    			if(int(unsigned char(szRecvBuffer[0])) == 0xBF)
    			{
    				PacketDisplay(szRecvBuffer,iBytesReceived);
    				Wait = 1;
    			}
    	}
    
    
        system("pause");
    	closesocket(skSocket);
    	cout << "< CLOSED SOCKET > " << endl;
    
    	// Clean up WinSock
    	WSACleanup();
    	cout << "< CLEANED UP WSA > " << endl;
       
    	return EXIT_SUCCESS;
    	
    }
    
    
    
    
    void PacketDisplay(char *buf,int iBytesSent)
    {
    	int i;
       
    	for(i=1;i<iBytesSent;i++)
    	{
    		if(buf[i] == 0)
    		{
    	cout << "00" << " ";
    		}
    		else
    		{
    
    	cout << hex<<int(unsigned char(buf[i])) << " ";
    		}
    	}
    	cout << endl;
       /*cout<< "In Chars" << endl;
        cout << " Packet Id: " << unsigned char(buf[0]) << endl;
    	for(i=0;i<iBytesSent;i++)
    	{
    		if(buf[i] == 0)
    		{
    	cout << "00" << " ";
    		}
    		else
    		{
    
    	cout << unsigned char(buf[i]) << " ";
    		}
    	}
    	cout << endl;*/
    
    }
    
    
    
    /*iBytesReceived = 0;
    	while(iBytesReceived == 0){
    	iBytesReceived = recv(skSocket,szRecvBuffer,32768,0);
        cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;
    	cout << "Packet Id: " << int(szRecvBuffer[0]) << endl;
    	for(i=1;i<iBytesReceived;i++)
    	{
    	cout << szRecvBuffer[i] << " ";
    	}
    	cout << endl;
    	}
    	cout  << "< Finish Loop >" << endl;*/ 
    /*memcpy(OverflowMove,&MoveRequest,sizeof(stMoveRequest));
    	iBytesSent = send(skSocket,OverflowMove,sizeof(stMoveRequest),0);
    	cout << "<-- SENT MOVE " << iBytesSent << " BYTES -->" << endl;
    
    	system("pause");
    	iBytesSent = send(skSocket,OverflowTalkRequest,sizeof(stTalkRequest),0);
    	cout << "<-- SENT TALK " << iBytesSent << " BYTES -->" << endl;
    	PacketDisplay(OverflowTalkRequest,iBytesSent);*/

    and the packet.h
    Code:
    class header
    {
    public:
    	unsigned char cmd;
    };
    class stGeneric
    {
    public:
    header cmd;
    unsigned char msg[100];
    };
    
    class stRedirect
    {
    public:
    header cmd;
    unsigned char IP[4];
    unsigned char port[2];
    unsigned char key[4];
    };
    class stMoveRejected
    {
    public:
    header cmd;
    char sequence;
    short int xLoc[2];
    short int yLoc[2];
    char direction;
    char zLoc;
    };
    struct stLogin
    {
     unsigned char i;
     char Username[30];
     char Password[30];
     char Unkown1;
    };
    struct stCharacterSelect
    {
    	unsigned char cmd;
    	char CharName[30];
    	char Unkown[42];
    };
    struct stHuepacket
    {
    unsigned char One;
     unsigned char Two;
     unsigned char Thee;
     unsigned char Four;
    };
    struct stServerSelect
    {
     unsigned char One;
     char Two;
     char Thee;
    };
    struct stGameLogin
    {
     header cmd;
     unsigned char key[4];
     char Username[30];
     char Password[30];
    };
    struct stServerKey
    {
    unsigned char cmd;
    char ip[4];
    char port[2];
    char keyb1;
    char keyb2;
    char keyb3;
    char keyb4;
    };
    struct stTalkRequest
    {
    header cmd;
    //short int size;
    //unsigned char type;
    //short int color;
    //short int font;
    unsigned char exact[7];
    char Language[4];
    unsigned char Unkown1[3];
    char msg[30];
    };
    struct stMoveRequest
    {
    unsigned char cmd;
    unsigned char direction;
    unsigned char sequence;
    char key[4];
    };
    struct stRecvBuffer
    {
    char szRecvBuffer[32768];
    };


    Now the reason I know the packet is weird is this.

    Here is an example of a WORKING packet logger

    Code:
    15:03:20.7968: Server -> Client 0xAE (Length: 72)
            0  1  2  3  4  5  6  7   8  9  A  B  C  D  E  F
           -- -- -- -- -- -- -- --  -- -- -- -- -- -- -- --
    0000   AE 00 48 00 00 09 2F 01  90 00 00 26 00 03 45 4E   ..H.../....&..EN
    0010   55 00 52 68 79 6D 65 73  00 00 00 00 00 00 00 00   U.Rhymes........
    0020   00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00   ................
    0030   00 48 00 65 00 6C 00 6C  00 6F 00 20 00 57 00 6F   .H.e.l.l.o. .W.o
    0040   00 72 00 6C 00 64 00 00                             .r.l.d..
    Here is an output of what I think is the same packet

    Code:
    BYTES 49
     Packet Id: 78 //Just the first item in th character array and a2 is second
    a2 1c 18 9f 3f ae 6 b8 68 8f f7 22 b aa 3 3b 7 cb b0 00 00 00 00 00 00 87 3c 8d
    43 51 e0 b4 b9 1e 8 63 51 c8 1a

    So the problem is my logger. It logs into the server, I can see on the server hes logged in. Im just not sure why my packets get so fubar when it makes it in game.

    Thankyou

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Is it my imagination, or is your main() like 5000 lines long?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    its just learning proccess. I could split it all up but I would loose focus

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Yeah, but when you've finished, you'll be able to focus on specific bits with much more clarity.
    That in itself usually helps you to figure out the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  2. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM
  3. Should I go to unicode?
    By nickname_changed in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2003, 11:37 AM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21