RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

3d Party Utility(Sockets)

ABTOP

Sorceror
Ok here is 2 threads
answserver_socket - hold cliend data after accept()
client_socket - hold data to RunUO server
Code:
int WINAPI ThreadProcOne(LPVOID lpParameter)
{
     char msg[1024];

     while(true)
     {
          memset(&msg, 0, sizeof(msg));
          recv(answserver_socket, msg, sizeof(msg), 0);
          cout<<"Client: "<<msg<<endl;
          Sleep(250);
          send(client_socket,msg, sizeof(msg), 0);
     }

     return 0;
}

int WINAPI ThreadProcTwo(LPVOID lpParameter)
{
     char msg[1024];

     while(true)
     {
          memset(&msg, 0, sizeof(msg));
         [COLOR="Red"] recv(client_socket, msg, sizeof(msg), 0);[/COLOR]
          cout<<"Server: "<<msg<<endl;
          Sleep(250);
          send(answserver_socket, msg, sizeof(msg), 0);
     }

     return 0;
}
doesn't work. Error in red line.
 

ABTOP

Sorceror
recv failed.
Software caused connection abort.
An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error.
 

Smjert

Sorceror
Well, i forgot that 2 threads can have problems with shared resources (like sockets).
So what you have to do first is to put recv send that uses the same socket in the same thread, then use wsaselect() to select the correct thing to do (so if there's something to recv it will receive, but it doesn't block the execution if there's nothing, otherwise if there's something to send it will send).
 

ABTOP

Sorceror
Ok so return code to one thread? And use "select" because "wasselect" i cant find on msdn.
Ohh by the way maybe i should set non blocking sockets?
 

Smjert

Sorceror
global thing:
Code:
CRITICAL_SECTION _critSectionserver;
CRITICAL_SECTION _critSectionclient;

main part:

Code:
InitializeCriticalSection (& _critSectionserver);
InitializeCriticalSection (& _critSectionclient);

Code:
int WINAPI ThreadProcOne(LPVOID lpParameter)
{
     char msg[1024];
     WSANETWORKEVENTS NetworkEvents;
     WSAEVENTS Events;
     WSAEventSelect(answerer_socket, Events, FD_READ);
     while(true)
     {
         if (WSAWaitForMultipleEvents(1, &Events, FALSE,WSA_INFINITE,     FALSE) == WSA_WAIT_FAILED)
                   return 0;

          WSAEnumNetworkEvents(answserver_socket,&Events, &NetworkEvents);

         if(NetworkEvents & FD_READ)
        {
          memset(&msg, 0, sizeof(msg));
          EnterCriticalSection(& _critSectionserver);
          int bytesread = recv(answserver_socket, msg, sizeof(msg), 0);
          LeaveCriticalSection(& _critSectionserver);
          cout<<"Client: "<<msg<<endl;
          Sleep(250);
          EnterCriticalSection(& _critSectionclient);
          send(client_socket,msg, bytesread, 0);
          LeaveCriticalSection(& _critSectionclient);
        }
     }

     return 0;
}

int WINAPI ThreadProcTwo(LPVOID lpParameter)
{
     char msg[1024];
     WSANETWORKEVENTS NetworkEvents;
     WSAEVENTS Events;
     WSAEventSelect(client_socket, Events, FD_READ);

     while(true)
     {
         if (WSAWaitForMultipleEvents(1, &Events, FALSE,WSA_INFINITE,     FALSE) == WSA_WAIT_FAILED)
                   return 0;

          WSAEnumNetworkEvents(client_socket,&Events, &NetworkEvents);

         if(NetworkEvents & FD_READ)
        {
          memset(&msg, 0, sizeof(msg));
          EnterCriticalSection(& _critSectionclient);
          int bytesread = recv(client_socket, msg, sizeof(msg), 0);
          LeaveCriticalSection(& _critSectionclient);
          cout<<"Server: "<<msg<<endl;
          Sleep(250);
          EnterCriticalSection(& _critSectionserver);
          send(answserver_socket, msg, bytesread, 0);
          LeaveCriticalSection(& _critSectionserver);
       }
     }

     return 0;
}

Ok well it should be something like that.

Edit: since i'm too lazy to rewrite, the code in ThreadProcOne should be in main() function, not a thread since this way you'll have 3 threads (the main plus the new two spawned).
 

ABTOP

Sorceror
Ok thanks. I put code in one Thread in global "while" and work fine.
So i have next structure
while(1)
{
if( messsage from client )
send to server
if( message from server )
send to client
}

I use select() for check is there any message.

I doesn't work earlier because i recv packet as soon as client connected. And buffer contain only 4 bytes.
If wait a bit buffer will get other packet(Login attemp) 62 bytes.
:)
 
Top