winsoc2.h lib

Pages: 12
hi,
i have develeop the game on c++ with allegro now i want to make multi player means connect 2 pc through internet by ip address ...
how to make this please help me ....
Hi Karan,

If I understood your situation properly, I can strongly advice learning Windows Socket Programming. I have included some links which may help you with this:
_______________________________________________
Server (Transmission Control Protocol based) : http://www.youtube.com/watch?v=jBAAbp6cNpg

Client (Transmission Control Protocol based):
http://www.youtube.com/watch?v=IY5IyX_KFJk

_______________________________________________

Of course as you are developing Network\ Multiplayer capabilities I think User Datagram Protocol type server & client will be useful:
http://www.youtube.com/watch?v=IqzqiI0kMk4


Hope You find this useful.

Kind Regards,
SpaceWorm

sir i will try for more i will again contact you..
thnks
Last edited on
Karan,

Thanks, did you find it useful?

Feel free to contact me.

Kind Regards,
SpaceWorm
hi SpaceWorm,
me watched that video on link you provide but not fulfilling my problem please give me another complete solution in source code something else....
i have not much time to do this i have to submit my project by thursday 2:00 p.m...and project covering my 50% course marks...
so please do my help to done this as soon as possible i be great thankful to you ...
regards
karan
@ SpaceWorm: void main()? Really? If you're using namespaces then you're writing in C++, you should follow the standard. Everything else was great.

Part 1: This tutorial was a beautiful start, I thought using comments and text would get annoying but you type well enough that it works. I especially liked the use of Telnet to bring it all together, it shows the watcher that they are making a functional application. I'm a little surprised to see that you didn't cover "send()" or "recv()" right here.

Part 2: The Red on Black needs to die. It's not unwatchable but it is distracting. When using the "send()" function you should take advantage of the strings library which you've already included. Things like the ".length()" member function allow the user to enter whatever message they want and they don't have to adjust the size of the buffer by hand. The "infinite for loop" looks a little amateurish, and you'll notice it didn't work. I would suggest do{ /*code code code*/ }while(message != "Quit");.

Server-Client applications are a great place to see the advantages of multi-threading. If you think it's too much for most people to take in at once then I can see that as a valid argument. But if you're looking to redo these or try your hand at a part 3 and\or 4 then that would be useful add-on.
Last edited on
Those videos are a complete waste of time. No one will ever learn anything useful from them.

You have to understand the concepts behind the sockets library to some extent before you can use it.

This is a good place to start.
http://www.beej.us/guide/bgnet/output/html/multipage/index.html
@kbw
thanks for help ,but sir i have not much tie read this now i have submit my project by 2 pm please give me only complete solution so i can apply and submit my project....??
If you think that someone giving you an entire solution that you can readily pass off as you're own work then I seriously believe you've missed the point of your education.
@kbw: Not exactly constructive criticism there is it? Video is a terrible medium for teaching anything but given that it was a 20 min shotgun lesson I think he did pretty good. Saying you need to understand sockets before you use them is like saying you need to understand iostream before writing Hello World. I learned what I know by working backward from functional code, and Im just getting around to authentication now.

EDIT: karan124: I could help you out. It would only cost you 5 Bitcoins.
Last edited on
What do you mean "not constructive"? I posted a useful alternative that most folk find helpful.
@ihutch ,
me don't want that ,you understand my point what i want to say ...
I just don't think his attempt is a total loss. I think with a few pointers here and there would make this one of the few passable video tutorials on YouTube. I'm also kind of hijacking the thread since the OP has no intention of trying to learn anything. That is a good link you posted but it's a POSIX tutorial. You don't need Cygwin to write networking code in Windows.
hello,
please help me to done my project ...i read all links but still confused to done my project please send me code of multiplayer game in c++ please so get through that one and do my project....
If you are looking for ready solution then this is bad place for that.
means here all are failures.......!!!!!!!
i try my best to do this but can't getting result....therefore i want best solution...
otherwise i developed game so can do any thing else also but this going out of mind....
so help me....really want this not only for project but i want to learn this really
Hi,

Do not ever ask for code , because you will not learn anything. If you wish to get a code so much at least show the code you did till now.

I can do it for you but you need to give me $4.00 or even $2.00.

Thanks
i am pasting my code server and client i had then give me solution

server code:


#include "chat_server.h"


CChatServer CServerObj;


UINT ServerRecThread(LPVOID pParam)
{
SOCKET sRecSocket = (SOCKET)pParam;
while(1)
{
if(CServerObj.RecClient(sRecSocket))
break;
}
return 0;
}



UINT ServerListenThread(LPVOID pParam)
{

while(1)
CServerObj.StartListenClient();
return 0;
}



CChatServer::CChatServer()
{

m_bIsConnected = false;

WSADATA wsaData;

sockaddr_in local;

int wsaret=WSAStartup(0x101,&wsaData);

if(wsaret!=0)
{
return;
}

local.sin_family=AF_INET;
local.sin_addr.s_addr=INADDR_ANY;
local.sin_port=htons((u_short)8084);

m_SListenClient=socket(AF_INET,SOCK_STREAM,0);


if(m_SListenClient==INVALID_SOCKET)
{
return;
}


if(bind(m_SListenClient,(sockaddr*)&local,sizeof(local))!=0)
{
return;
}


if(listen(m_SListenClient,10)!=0)
{
return;
}

m_bIsConnected = true;
return;
}

CChatServer::~CChatServer()
{
closesocket(m_SListenClient);

WSACleanup();
}

void CChatServer::StartListenClient()
{

sockaddr_in from;
int fromlen=sizeof(from);

m_SClient=accept(m_SListenClient,
(struct sockaddr*)&from,&fromlen);

if(m_SClient != INVALID_SOCKET)
m_vClientList.push_back(m_SClient);

AfxBeginThread(ServerRecThread,(void *)m_SClient);

}



int CChatServer::SendMessagePort(string sMessage)
{
int iStat = 0;
list<SOCKET>::iterator itl;

if(m_vClientList.size() == 0)
return 0;

for(itl = m_vClientList.begin();itl != m_vClientList.end();itl++)
{
iStat = send(*itl,sMessage.c_str(),sMessage.size()+1,0);
if(iStat == -1)
m_vClientList.remove(*itl);
}

if(iStat == -1)
return 1;

return 0;

}

int CChatServer::RecClient(SOCKET sRecSocket)
{
char temp[4096];
int iStat;


iStat = recv(sRecSocket,temp,4096,0);
if(iStat == -1)
{
m_vClientList.remove(sRecSocket);
return 1;
}
else
{
cout <<":"<<temp<<"\n";
SendMessagePort(temp);
return 0;
}
return 0;

}




int main(int argc, char* argv[])
{
int nRetCode = 0;
char buf[4096];


cout << "Press ONLY ENTER to quit.\n";
cout << "=================================================\n";

if(!CServerObj.IsConnected())
{
cout<<"\nFailed to initialise server socket";
cout<<"\nThis is boby signing off : Bye";
getch();
return 1;
}
AfxBeginThread(ServerListenThread,0);


while(gets(buf))
{
if(strlen(buf) == 0)
break;
if(CServerObj.SendMessagePort(buf))
{
cout<<"Problem in connecting to server.\n";
break;
}
}

cout<<"This is user signing off.";
getch();

return nRetCode;
}

header files of server:




#include <Afxwin.h>
#include <stdio.h>
#include <winsock2.h>
#include <conio.h>
#include<list>
#include <iostream>

using namespace std;

class CChatServer
{
public:
CChatServer();
~CChatServer();
bool IsConnected(){return m_bIsConnected;} // returns connection status
void StartListenClient(); // Listen to client
int SendMessagePort(string sMessage); // Send message to sll clients.
int RecClient(SOCKET sRecSocket); // receive message for a particulat socket
private:
bool m_bIsConnected; // true - connected false - not connected
int m_iServerPort;
list<SOCKET> m_vClientList; // All socket connected to client
SOCKET m_SClient;
SOCKET m_SListenClient; // socket listening for client calls
};





client code:



#include "chat_client.h"


CIPMessage MyMessObj;


CIPMessage::CIPMessage()
{
m_bIsConnected = false;
}

void CIPMessage::Init(string sIpAddress, int iPort)
{

m_sServerIPAddress = sIpAddress;
m_iServerPort = iPort;
struct hostent *hp;
unsigned int addr;
struct sockaddr_in server;


WSADATA wsaData;

int wsaret=WSAStartup(0x101,&wsaData);


if(wsaret!=0)
{
return;
}

conn=socket(AF_INET,SOCK_STREAM,0);
if(conn==INVALID_SOCKET)
return;

addr=inet_addr(m_sServerIPAddress.c_str());
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

if(hp==NULL)
{
closesocket(conn);
return;
}

server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);
server.sin_family=AF_INET;
server.sin_port=htons(m_iServerPort);
if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
{
closesocket(conn);
return;
}
m_bIsConnected = true;
return;
}

CIPMessage::~CIPMessage()
{
if(m_bIsConnected)
closesocket(conn);
}

int CIPMessage::SendMessagePort(string sMessage)
{
int iStat = 0;

iStat = send(conn,sMessage.c_str(),sMessage.size()+1,0);
if(iStat == -1)
return 1;

return 0;

}

int CIPMessage::RecMessagePort()
{

char acRetData[4096];
int iStat = 0;

iStat = recv(conn,acRetData,4096,0);
if(iStat == -1)
return 1;
cout<<"-->"<<acRetData<<"\n";

return 0;

}



UINT MessageRecThread(LPVOID pParam)
{
while(1)
{
if(MyMessObj.RecMessagePort())
break;
}
return 0;
}



int main(int argc, char* argv[])
{
char buf[4096];
cout<<"\nPress ONLY ENTER to quit\n\n";


FILE *fp = fopen("server.ini","r");
if(fp == NULL)
{
cout<<"\nUnable to open server.ini. Please specify server IPsddress in server.ini";
return 1; // main failed
}
string sServerAddress;
while((fgets(buf,4096,fp)) != NULL)
{
if(buf[0] == '#')
continue;
sServerAddress = buf;

}
fclose(fp);

if(sServerAddress.size() == 0)
{
cout<<"\nUnable to find server IPaddress in server.ini";
cout<<"\nPlease set server IPaddress";
cout<<"\nThis is user Signing off.";
getch();
return 0;
}

MyMessObj.Init(sServerAddress.c_str(),8084);
if(!MyMessObj.IsConnected())
{
cout<<"\nUnable to connect to the IPaddress specified in server.ini";
cout<<"\nPlease check server IPaddress";
cout<<"\nThis is user Signing off. BYE:";
getch();
return 0;
}

AfxBeginThread(MessageRecThread,0);
while(gets(buf))
{
if(strlen(buf) == 0)
break;
if(MyMessObj.SendMessagePort(buf))
{
cout<<"Problem in connecting to server. \n";
break;
}
}

cout<<"\nThis is user Signing off. BYE:";
getch();
return 0;
}

client header :


#include <Afxwin.h>
#include <stdio.h>
#include <winsock2.h>
#include <conio.h>

#include <iostream>

using namespace std;


class CIPMessage
{
public:
CIPMessage();
~CIPMessage();
void Init(string sIpAddress, int iPort);
int SendMessagePort(string sMessage);
int RecMessagePort();
bool IsConnected(){return m_bIsConnected;}
private:
bool m_bIsConnected; // true - connected false - not connected
string m_sServerIPAddress;
int m_iServerPort;
SOCKET conn; // socket connected to server
};

now tell me what to do....
It would be helpful if you used the code formatting tag to format your code and preserve indentation. That would make it a lot easier to read.

The server's client handler doesn't do much:
1
2
3
4
5
6
7
8
9
10
UINT ServerRecThread(LPVOID pParam)
{
    SOCKET sRecSocket = (SOCKET)pParam;
    while(1)
    {
        if(CServerObj.RecClient(sRecSocket))
            break;
    }
    return 0;
}
What it should do depends on how you want you chat thing to work. Do peers chat to each other one on one, or you they chat in a forum hosted on the server?

You seem to pretty much have all the comms code and thread stuff (perhaps too many). But it's not connected up properly.

If you could describe in detail how you expect it work (in words), I might have a better idea of what you're trying to do and help you connect the parts together.

You have to remember that the client and server work together. They have to agree on when data is to be send and received and what is communicated. You don't seem to cover that area very well.
Last edited on
Pages: 12