Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Mon Dec 11, 2017 5:50 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: A question of port
PostPosted: Wed Oct 04, 2017 6:45 pm 
Offline

Joined: Wed Nov 25, 2015 4:56 pm
Posts: 24
Chapter 12 “If more than one application running on a computer uses the same port, a conflict will result “

Can you just expand on this, does this mean that it will not work at all?

Is there a way to check and maybe use a different port?


Cheers :)


Top
 Profile  
Reply with quote  
 Post subject: Re: A question of port
PostPosted: Fri Oct 06, 2017 12:33 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 543
https://www.howtogeek.com/howto/28609/how-can-i-tell-what-is-listening-on-a-tcpip-port-in-windows/

Your game code should allow the user to specify the port to use.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
 Post subject: Re: A question of port
PostPosted: Mon Oct 16, 2017 7:49 pm 
Offline

Joined: Wed Nov 25, 2015 4:56 pm
Posts: 24
OK I’ve managed to answer my question on checking for a used port in my program. Code is as follows

Code:
int                                         iCheck,
                                            iX;
 
iX =                                        0;
 
//Loop/If-----------------------------------*-------------|                           |
do
{
   APP_SERVER_PORT +=            iX;

   iCheck =
    cServer->CreateServer(                 APP_SERVER_PORT,
                                            APP_PROTOCOL);
 
    iX +=                                    1;
 
} while (iCheck != NET_OK);



Now all I need to know is sending custom structures. Am I correct in thinking that when using the ReadData function, the Size variable needs to be set to the expected size and the Data And SenderIP are blank pointers that receive the data and sender IP?

Is it possible to create a server and client on different ports within the same app and send data to each other or do they have to be in separate apps. It’s just I’m trying to test sending a custom structure and it is easier to debug In one app. My game initialisation has the following code.

Code:
//------------------------------------------*-----------------------------------------|
int               
UDP =                              0,
TCP =                              1,

APP_SERVER_PORT =                     5123,
APP_CLIENT_PORT =                     4072
APP_PROTOCOL =                        TCP;

//------------------------------------------*-----------------------------------------|
const char               
SERVER_IP_ADDRESS[] =                  "192.168.1.189";

//------------------------------------------*-----------------------------------------|
cNetwork                           *cServer,
                                 *cClient;

cServer = new                         cNetwork;
cClient = new                        cNetwork;

//------------------------------------------*-----------------------------------------|

int                                         iCheck,
                                            iX;
 
iX =                                        0;
 
//Loop/If-----------------------------------*-------------|                           |
do
{
        APP_SERVER_PORT +=            iX;

   iCheck =
    cServer->CreateServer(                 APP_SERVER_PORT,
                                            APP_PROTOCOL);
 
    iX +=                                    1;
 
} while (iCheck != NET_OK);
 
iX =                                        0;

//Loop/If-----------------------------------*-------------|                           |
do
{
        APP_CLIENT_PORT +=        iX;

      iCheck =
    cClient->CreateClient(                  (char*)SERVER_IP_ADDRESS,
                                            APP_CLIENT_PORT,
                                            APP_PROTOCOL);
 
      iX +=                                 1;
 
} while (iCheck != NET_OK);
 
//------------------------------------------*-----------------------------------------|
char                                        *iSenderIP,
                                            *iFileData;

int                                         iSize;
 
sAuth                                       *iAuth;

//------------------------------------------*-----------------------------------------|
iAuth->Email =                                 "john.smith@hotmail.co.uk";
iAuth->LicenceKey =                          "1546.3795.6248.6428";
iAuth->Password =                             "Password001";
iAuth->UpdateDate.D =                        12;
iAuth->UpdateDate.M =                        10;
iAuth->UpdateDate.Y =                        2017;
iAuth->Username =                            "John Smith";
 
iSize =                                     sizeof(sAuth);

//------------------------------------------*-----------------------------------------|
iCheck =
cClient->SendData(                         (char*)iAuth,
                                            iSize,
                                            (char*)SERVER_IP_ADDRESS,
                                            APP_SERVER_PORT);

//Loop/If-----------------------------------*-------------|                           |
if (iCheck != NET_OK)
{
    std::string                              iError;
    
    iError = cClient->GetError(             iCheck);   
}


My game loop has the following.


Code:
//------------------------------------------*-----------------------------------------|
char                                        *iSenderIP,
                                            *iFileData;

int                                         
iSize =                              sizeof(sAuth);
 
sAuth                                       *iAuth;

//------------------------------------------*-----------------------------------------|
iCheck =
cServer->ReadData(                         iFileData,
                                            iSize,
                                            (char*)iSenderIP);

//Loop/If-----------------------------------*-------------|                           |
if (iCheck != NET_OK)
{
    std::string
   iError = cServer->GetError(             iCheck);
}

//Loop/If-----------------------------------*-------------|                           |
else
{
   iAuth =                                  (sAuth*)iFileData;
}



sAuth is my custom structure. The server never receives any data. iError always states no network errors.

What is it that I’m doing wrong?


Many thanks :)


Top
 Profile  
Reply with quote  
 Post subject: Re: A question of port
PostPosted: Tue Oct 17, 2017 2:26 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 543
KDK1976 wrote:
Is it possible to create a server and client on different ports within the same app and send data to each other or do they have to be in separate apps. It’s just I’m trying to test sending a custom structure and it is easier to debug In one app.

Hmmm? I think it would be possible but I would need to write some test code to be sure. Each client/server connection would require a unique socket that specifies the port to use. Unfortunately I don't have any free time to experiment at the moment.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group