Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sample code for networking  (Read 2399 times)

0 Members and 1 Guest are viewing this topic.

user

  • Newbie
  • *
  • Posts: 6
    • View Profile
Sample code for networking
« on: March 22, 2011, 01:19:25 pm »
Hi,
i wrote a code,in which one machine(A) sends "hi" to another machine(B).Now A sends the data successfully.But B does not receive it.I've bound the socket to the port but still,the status shows NotReady(at B).What is to be done?
Also, i need B to send back an ack to A.can u please help me out by showin a code on this.I need it for my project asap.

PS : I've seen the sample codes that are the tutorial,but donno y my code is not working.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Sample code for networking
« Reply #1 on: March 22, 2011, 01:21:57 pm »
Well, since you already saw the official example that works, what do you expect us to show you now? ;)

You should rather show your own code so that we can help you.
Laurent Gomila - SFML developer

user

  • Newbie
  • *
  • Posts: 6
    • View Profile
well,here's the code...PLEASE HELP
« Reply #2 on: March 23, 2011, 03:30:22 pm »
Code: [Select]
#include <windows.h>
#include "stdafx.h"
#include "nettrial2.h"
#include "resource.h"
#include "commdlg.h"

#include "SFML/System.hpp"
#include "SFML/Network/IPAddress.hpp"
#include "SFML/Network/Http.hpp"
#include "SFML/Network/SocketUDP.hpp"
#include "SFML/Network/SocketHelper.hpp"
#include "string.h"
#include "iostream"

LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK AboutProc( HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK IpProc( HWND, UINT, WPARAM, LPARAM );
int CALLBACK sendConnRequest(char*);
int CALLBACK getConnRequest(sf::SocketUDP);

int flag=1;
unsigned short P1sendPort=3000,P1receivePort=3001,P2receivePort=4000,P2semdPort=4001;
sf::IPAddress P2addr;

int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine,int iCmdShow )
{
   WNDCLASS wc;
   MSG msg;
   HWND hWnd;
   HMENU hmenu;
   if( !hPrevInstance )
   { wc.lpszClassName = TEXT("LetterLink");
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
wc.lpszMenuName = (LPCWSTR)IDC_LETTERLINK;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
RegisterClass( &wc ); }
   hWnd = CreateWindow( TEXT("LetterLink"),TEXT("LetterLink"),WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,    0,    0,
   CW_USEDEFAULT,    CW_USEDEFAULT,    NULL,    NULL,    hInstance,   NULL
);
   hmenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDC_LETTERLINK));
ShowWindow( hWnd, iCmdShow );
UpdateWindow(hWnd);
while( GetMessage( &msg, NULL, 0, 0 ) ) {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
}
return 0;
}
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,    LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hDC;
static HINSTANCE hinst;
static HMENU hmenu;
static int cxClient, cyClient,cxChar,cyChar;
TEXTMETRIC tm;
int sel;

switch( msg ) {
case WM_CREATE:
hinst=((LPCREATESTRUCT)lParam)->hInstance;
hmenu=GetMenu(hWnd);
hDC = GetDC (hWnd) ;
     GetTextMetrics (hDC, &tm) ;
     cxChar = tm.tmAveCharWidth ;
     cyChar = tm.tmHeight + tm.tmExternalLeading ;
     ReleaseDC (hWnd, hDC) ;
break;
case WM_SIZE:
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);
    break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_NEW:
sel=MessageBox(hWnd,TEXT("Are you player 1?"),TEXT(""),MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL);
if(sel==IDYES)
{
//set flags
DialogBox(hinst,(LPCWSTR)IDD_IPBOX,hWnd,(DLGPROC)IpProc);
}
else
{
int cn=getData();
if(cn==1)
MessageBox(hWnd,TEXT("Connection Successful!"),TEXT("Connected"),MB_OK);
}
EnableMenuItem(hmenu,ID_NEW,MF_GRAYED);
break;
case ID_ABOUT:
DialogBox(hinst,(LPCWSTR)IDD_ABOUTBOX,hWnd,(DLGPROC)AboutProc);
break;
case ID_EXIT:
PostQuitMessage(0);
break;
}
return 0;
case WM_PAINT:
    hDC = BeginPaint( hWnd, &ps );

EndPaint( hWnd, &ps );
    return 0;
case WM_DESTROY:
    PostQuitMessage( 0 );
    return 0;
default:
     return( DefWindowProc( hWnd, msg, wParam, lParam ));
}
  return 0;
}

BOOL CALLBACK AboutProc( HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
LPWSTR str=TEXT("");
switch (msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
hdc=GetDC(hdlg);
// GetWindowText(GetDlgItem(hdlg,IDx),str,5);
TextOut(hdc,100,100,(LPCWSTR)str,5);
EndDialog(hdlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

BOOL CALLBACK IpProc( HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND one,two,three,four;
int valid=1,len1,len2,len3,len4;
static TCHAR addr[3];
char *address="";

int connected;
switch (msg)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDCONNECT)
{
//not pasting the other non-necessary code...

                              if(valid==1)
{


connected=sendConnRequest("192.168.3.11");
if(connected==1)
MessageBox(hdlg,TEXT("Connection Successful!"),TEXT("Connected"),MB_OK);
else
MessageBox(hdlg,TEXT("Incorrect IP address!"),TEXT("Error"),MB_OK);
}
}
break;
case WM_CLOSE:
EndDialog(hdlg, LOWORD(wParam));
return TRUE;
}
return FALSE;
}



int CALLBACK sendConnRequest(char *addr)
{
    static sf::SocketUDP P1,P2;
int conn=0;
//connect,get back a flag which is an indication of the server sending back data,return the flag
if(!P1.Bind(P1sendPort))
{
return 0;
}
if(!P2.Bind(P2receivePort))
{
return 0;
}
P2.SetBlocking(false);
P2addr=sf::IPAddress(addr);
TCHAR Message[3]=TEXT("hi");
if(!P2addr.IsValid())
return 0;
if(P1.Send((char*)Message,sizeof((char*)Message),P2addr,P2receivePort)==sf::Socket::Done)
conn=getConnRequest(P2);
if(conn==1)
return 1;
else
return 0;
}



int CALLBACK getConnRequest(sf::SocketUDP P2)
{
if(!P2.Bind(P2receivePort))
{
return 0;
}
std::size_t Received;
sf::IPAddress ClientAddress;
unsigned short ClientPort;
TCHAR Message[3]=TEXT("");
TCHAR temp[3]=TEXT("hi");

sf::Socket::Status st=P2.Receive((char*)Message, sizeof(Message), Received, ClientAddress, ClientPort);
if(st==sf::Socket::Done)
{
if(strcmp((char*)Message,(char*)temp)==0)
return 1;
}
return 0;
}


how to actually run the program? I mean, it won't run if use the same code in both machines, because then,my machine will call its own getConnRequest(). I want the other machine's getConnRequest() to be called.And i want the other machine to send back an ack. when it recieves my machine's message.Then i want both the machines to send and receive data.

user

  • Newbie
  • *
  • Posts: 6
    • View Profile
PLEASE REPLY.Its urgent.
« Reply #3 on: March 24, 2011, 10:59:37 am »
PLEASE REPLY.Its urgent.