Archived

This topic is now archived and is closed to further replies.

Stating up

This topic is 5503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello ppl.... I''m a complete nweby of networking and I''m trying to undertand how to do multiplayer....
#include <windows.h>
#include  

void abc(char *p)
{
      FILE *fp=fopen("z.txt","a+");
	fprintf(fp,"%s\n",p);
	fclose(fp);
}

WNDCLASS a; HWND b; MSG c; char aa[200]; SOCKET s; struct hostent h;
WSADATA ws; DWORD e; int ii,dw; char bb[100]; struct sockaddr_in Sa;

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);

int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
	a.lpszClassName="a1";
	a.hInstance=i;
	a.lpfnWndProc=zzz;
	a.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	RegisterClass(&a);
	b=CreateWindow("a1","time client",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
	ShowWindow(b,3);
	while ( GetMessage(&c,0,0,0) )
		DispatchMessage(&c);
	return 1;
}

long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
if ( x == WM_LBUTTONDOWN)
{	
	e=WSAStartup(0x0101,&ws);
	sprintf(aa,"e = %ld",e);
	abc(aa);
	s = socket(PF_INET,SOCK_DGRAM,0);
	sprintf(aa,"s = %ld",s);
	abc(aa);
	Sa.sin_family=AF_INET;
	Sa.sin_addr.s_addr = inet_addr("127.0.0.1");
	Sa.sin_port=htons(13);
	strcpy (bb,"hello how are you");
	e=sendto(s,bb,100,0,(struct sockaddr *)&Sa,sizeof(Sa));
	sprintf(aa,"SendTo %ld",e);
	int dw = sizeof(Sa);
	recvfrom(s,bb,100,0,(sockaddr *)&Sa,&dw);
	MessageBox(0,bb,"data from server",0);
	MessageBox(0,"end","end",0);
}

if ( x == WM_DESTROY)
	PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
} 
Ok, this is the code that I get from a tut (http://www.vijaymukhi.com/vmis/wsockexp.htm) and I''m reading it. It craches at line: recvfrom(s,bb,100,0,(sockaddr *)&Sa,&dw); and I don''t jnow why..... Anyway, I even don''t know what a hell this code should do!!!!! Filami Techno Grooves

Share this post


Link to post
Share on other sites
The code is trying to send a message to itself (127.0.0.1) and then receive it. The problem is, the socket needs to be bound to a specific port before receiving anything. To fix this, insert this anywhere in your code before the recvfrom():

SOCKADDR_IN addrBind;
addrBind.sin_family = AF_INET;
addrBind.sin_port = htons(13);
addrBind.sin_addr = INADDR_ANY;
bind(s, (sockaddr *) &addrBind, sizeof(addrBind));

That will bind the socket to the port 13 which means that it''ll receive messages sent to this port.
For more info on winsock, check out my tutorial at http://frenchwhales_site.tripod.com

Joey

Share this post


Link to post
Share on other sites
addrBind.sin_addr = INADDR_ANY;

error C2679: binary ''='' : no operator defined which takes a right-hand operand of type ''unsigned long'' (or there is no acceptable conversion)



Techno Grooves

Share this post


Link to post
Share on other sites
I suggest you to use SDL and SDL_net, they are very easy, portable, have some source code, etc.
www.libsdl.org

[edited by - Raduprv on November 18, 2002 6:54:32 PM]

Share this post


Link to post
Share on other sites
quote:
I suggest you to use SDL and SDL_net, they are very easy, portable, have some source code, etc.


REALY!!!!!!!! I'M USING SDL TO PROGRAM!!!!!


frenchwhale: still craches!!! :?

Filami



[edited by - filami on November 19, 2002 12:01:55 PM]

Share this post


Link to post
Share on other sites