Original Post
[size="5"]Old post ---- scroll down to my last post #13
Hello everyone
I'm a network newbie and not long ago i started learning Winsock.
anyways I have a structer that contain player X and Y position and and this structure is being send from the server to the client 6300 times every second. I tried to send it 60 times every second but there was a 1 sec delay from when I pressed Up key to move in the server side till the client respond and moved up.
I had to send the packet 6300 time so there would be no delay. I was just wounding isn't that a bit high. I read that sending packet 200times every second was enough. if that is true then why do I need to send my packet 6300 times ever second for it to not have any delay ?
Server Code:-
Main Server Code:-
/----------------------------------------------
Client Code:-
Main Client Code:-
Hello everyone
I'm a network newbie and not long ago i started learning Winsock.
anyways I have a structer that contain player X and Y position and and this structure is being send from the server to the client 6300 times every second. I tried to send it 60 times every second but there was a 1 sec delay from when I pressed Up key to move in the server side till the client respond and moved up.
I had to send the packet 6300 time so there would be no delay. I was just wounding isn't that a bit high. I read that sending packet 200times every second was enough. if that is true then why do I need to send my packet 6300 times ever second for it to not have any delay ?
Server Code:-
#include "Networking.h"
#include "Player.h"
struct PlayerMovment {
int PlayerXPos;
int PlayerYPos;
};
PlayerMovment PlayerPos;
Player P;
Networking::Networking()
{
SendSocket = INVALID_SOCKET;
Port = 27015;
P.SetPosition();
}
Networking::~Networking()
{
}
void Networking::NetworkPlayerPosition(sf::RenderWindow &Window)
{
P.Movment(Window);
PlayerPos.PlayerXPos = P.GetPlayerX();
PlayerPos.PlayerYPos = P.GetPlayerY();
}
void Networking::Init()
{
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
wprintf(L"WSAStartup failed with error: %d\n", iResult);
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SendSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
}
}
void Networking::SendData()
{
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//wprintf(L"Sending data...");
iResult == sendto(SendSocket, (char*)&PlayerPos, sizeof(PlayerPos), 0, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
if (iResult == SOCKET_ERROR) {
wprintf(L"sendto failed with error: %d\n", WSAGetLastError());
closesocket(SendSocket);
WSACleanup();
}
}
void Networking::CloseSocket()
{
wprintf(L"Finished sending. Closing socket.\n");
iResult = closesocket(SendSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"closesocket failed with error: %d\n", WSAGetLastError());
WSACleanup();
}
}
void Networking::CleanUp()
{
wprintf(L"Exiting.\n");
WSACleanup();
}
Main Server Code:-
#include "Networking.h"
#include "Player.h"
int main()
{
Networking N;
Player P;
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Network Test with SFML");
N.Init();
P.LoadFile();
P.SetPosition();
sf::Clock Clock;
while (Window.IsOpened())
{
sf::Event Event;
Window.SetFramerateLimit(60);
float ElapsedTime = Window.GetFrameTime();
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
}
N.NetworkPlayerPosition(Window);
int Timer = Clock.GetElapsedTime();
for(int i = 0; i<100; i++)
{
N.SendData(); // sending data 100times * 63FPS = 6300 times every second
}
P.Movment(Window);
Window.Clear();
P.Draw(Window);
Window.Display();
}
N.CloseSocket();
N.CleanUp();
return 0;
}
/----------------------------------------------
Client Code:-
#include "Networking.h"
struct PlayerMovment {
int PlayerXPos;
int PlayerYPos;
};
PlayerMovment PlayerPos;
Networking::Networking()
{
iResult = 0;
Port = 27015;
SenderAddrSize = sizeof (SenderAddr);
}
Networking::~Networking()
{
}
void Networking::Init()
{
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error %d\n", iResult);
}
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (RecvSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error %d\n", WSAGetLastError());
}
}
void Networking::Bind()
{
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
if (iResult != 0) {
wprintf(L"bind failed with error %d\n", WSAGetLastError());
}
}
void Networking::RcevData()
{
//wprintf(L"Receiving datagrams...\n");
iResult = recvfrom(RecvSocket, (char*)&PlayerPos, sizeof(PlayerPos), 0, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
if (iResult == SOCKET_ERROR) {
wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
}
}
void Networking::CloseSocket()
{
wprintf(L"Finished receiving. Closing socket.\n");
iResult = closesocket(RecvSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
}
}
void Networking::CleanUp()
{
wprintf(L"Exiting.\n");
WSACleanup();
}
int Networking::GetPlayerX()
{
return PlayerPos.PlayerXPos;
}
int Networking::GetPlayerY()
{
return PlayerPos.PlayerYPos;
}
Main Client Code:-
#include "Networking.h"
#include "Player.h"
int main()
{
Networking N;
Player P;
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Network Test with SFML");
N.Init();
N.Bind();
P.LoadFile();
while (Window.IsOpened())
{
sf::Event Event;
Window.SetFramerateLimit(60);
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
}
for(int i = 0; i<100; i++)
{
N.RcevData(); // receving data 100times * 63FPS = 6300 times every second
}
P.SetPosition(N.GetPlayerX(), N.GetPlayerY());
Window.Clear();
P.Draw(Window);
Window.Display();
}
N.CloseSocket();
N.CleanUp();
return 0;
}