Sign in to follow this  

new to networking and need al little help

This topic is 4104 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

i have a sever exe and en client exe. the client sends a message and the server will give a cout if the message is received. just cant get it working. the client code
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA w;    // used to store information about WinSock version
int error = WSAStartup (0x0202, &w);   // Fill in w
if (error)
{ // there was an error
	printf("oioio");
}
if (w.wVersion != 0x0202)
{ // wrong WinSock version!
  WSACleanup (); // unload ws2_32.dll
  }

	SOCKET s;			// Socket handle
	s = socket (AF_INET,SOCK_STREAM,0);
	sockaddr_in target; 

	target.sin_family = AF_INET; // address family Internet 
	target.sin_port = htons (5555); // set server’s port number 
	target.sin_addr.s_addr = inet_addr ("127.0.0.1"); // set server’s IP 
bool connection=false;

while(connection!=true){

	if (connect(s,(LPSOCKADDR)&target,sizeof(target))==SOCKET_ERROR){
	printf("error bij het connecten");
	WSACleanup();
		
		return 0;
		}else{
	printf ("connection gemaakt\n");
	connection=true;
	}
		
}//eind van while
printf("begin met verzenden\n");
char buffer[10];  // buffer that is 11 characters big
char piet='a';
int jan=2;
sprintf(buffer,"%c%d",TEST_A,0);

send (s, buffer, 2, 0);
if(buffer[0]==TEST_A){
	std::cout << "test a is wel degelijk verzonden";
}
printf("informatie is verzonden\n");

	return 0;
}


and here the server code
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA w;    // used to store information about WinSock version
int error = WSAStartup (0x0202, &w);   // Fill in w
if (error)
{ // there was an error
	printf("oioio");
}
if (w.wVersion != 0x0202)
{ // wrong WinSock version!
  WSACleanup (); // unload ws2_32.dll
  }//testen of alles werkt



bool connected=false;
int addr_size = sizeof (sockaddr);
sockaddr_in dezeS;// de struckt met alle socked data die nodig is(server)
sockaddr client1;// de struckt met alle socked data die nodig is(client)
SOCKET gast;// de client socket

SOCKET s = socket (AF_INET, SOCK_STREAM, 0); //maak de socket aan
	//hieronder alle info die de socket nodig heeft in een struckt plaatsen
	dezeS.sin_family = AF_INET;    
	dezeS.sin_port = htons (5555); // poort nummer (htons is een functie die het in het goede formaat maakt)
	dezeS.sin_addr.s_addr = htonl (INADDR_ANY);
	if (bind(s,(LPSOCKADDR)&dezeS,sizeof(dezeS))==SOCKET_ERROR)// geeft de informatie hierboven geplaats aan het nieuwe socket
	{
		printf("FOUT!!");
		WSACleanup ();    }
	if (listen(s,1)==SOCKET_ERROR) //start met accepteren van connetion+error tjek
	{
		printf( "Error:  Unable to listen!\n");
		WSACleanup ();
		}
	SDL_Delay(3000);
printf("wacht op verbinding\n");





while(connected==false){

	gast = accept(s,&client1,&addr_size);
						if (gast==INVALID_SOCKET)//werderom error tjeking
		{
			printf( "Error:  Unable to accept connection!\n");
			WSACleanup ();
			
		}else {

  
				SDL_Delay(1000);
			printf("wacht op verbinding\n");
			SDL_Delay(3000);
			printf("wacht op verbinding\n");
			connected=true;
			
						}				
}

char buffer[80]; 
recv (gast,buffer,2,0);

if((buffer[0]==TEST_A)){

	std::cout << "HOERAAAA HET WERKT";
}else {

	std::cout << "het werkt nog steeds niet post je code op gamedev";
}
		



		




return 0;
}

Share this post


Link to post
Share on other sites
TEST_A looks like a candidate, you sure thats the same in both client and server? I can't really see anything else that would stop your connection "working."

Start by adding some error checking to send and recv, make sure they are sending/receiving the right sizes, and not returning -1 on error, or ZERO (disconnected).
Instead of testing the first byte in the received packet on the server, just output the buffer (as hex perhaps), see what it gives.

And just for your knowledge, char buffer[10] is 10 characters big. I'm guessing you picked that up from VB? There's no need on 10 or 80 character buffers if all you're sending is 2 bytes.



Share this post


Link to post
Share on other sites

This topic is 4104 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this