Problem with using socket from SDL_Net SDLNet_TCP_Accept

here’s the code :

int main(int argc, char **argv)

{

nt len = 0;

char text[20];

char msg[1024];

int mousexbefore = 0;

int mouseybefore = 0;

bool needtojoin = 0;

char needtojoinip[50];

Uint16 port;

IPaddress hostip, serverip, *somecomputerip;

TCPsocket listeningsocket, joiningsocket, somecomputer;

bool close = 0;

printf(“joining. plz wait\n”);

needtojoin = 1;

printf(“write the IP Address you want to join to :\n”);

scanf("%s", needtojoinip);

printf(“write the port you want to use : \n”);

scanf("%d", &port);

internetinit();

setupsocket(&hostip, NULL, port);

listeningsocket = openTCP(&hostip);

char mychar = 3;

msg[0] = ‘0’;

msg[1] = 0;

setupsocket(&serverip, needtojoinip, port);

joiningsocket = openTCP(&serverip);

senddataTCP(joiningsocket, msg);

somecomputer = 0;

while (!somecomputer)

somecomputer = SDLNet_TCP_Accept(listeningsocket);

while(SDLNet_TCP_Recv(somecomputer,&mychar,1)==1 && mychar!=0)

{

if (mychar == ‘0’)

printf(“recieved message\n”);

}

mychar = 3;

cleanstring(msg);

msg[0] = ‘0’;

msg[1] = 0;

senddataTCP(somecomputer, msg);

somecomputer = 0;

while (!somecomputer)

somecomputer = SDLNet_TCP_Accept(listeningsocket);

while(SDLNet_TCP_Recv(somecomputer,&mychar,1)==1 && mychar!=0)

{

if (mychar == ‘0’)

printf(“recieved second message\n”);

}

scanf("%s", &text);

SDLNet_Quit();

SDL_Quit();

exit(0);

}

void internetinit(void)

{

if(SDLNet_Init()==-1)

{

printf(“SDLNet_Init: %s\n”,SDLNet_GetError());

}

}

void setupsocket(IPaddress *address, char *ipinstring, Uint16 port)

{

if(SDLNet_ResolveHost(address,ipinstring,port)==-1)

{

printf(“SDLNet_ResolveHost: %s\n”,SDLNet_GetError());

}

}

TCPsocket openTCP(IPaddress *ip)

{

TCPsocket socket;

socket = SDLNet_TCP_Open(ip);

if(!socket)

{

printf(“SDLNet_TCP_Open: %s\n”,SDLNet_GetError());

}

return socket;

}

void senddataTCP(TCPsocket socket, char *msg)

{

int len,result;

len=strlen(msg)+1; // add one for the terminating NULL

result=SDLNet_TCP_Send(socket,msg,len);

if(result<len)

{

printf(“SDLNet_TCP_Send: %s\n”, SDLNet_GetError());

// It may be good to disconnect sock because it is likely invalid now.

}

}

anyways, what the program was meant to do is to communicate with itself. at start , port is random number and the IP address is localhost. so the computer talks to itself, at the start it sends a message which is “0” , and then the program tries to recieve a message which was sent - and it indeed recieves, but when i get the socket of the sender (which is the localhost) and i try to send it a message - the message never arrives, so “second message recieved” text is never being shown and the program is in an infinite loop. please tell me what is the problem because im stuck for a week already…____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

anyways, what the program was meant to do is to communicate with itself.
at start , port is random number and the IP address is localhost. so the
computer talks to itself, at the start it sends a message which is “0” ,
and then the program tries to recieve a message which was sent - and it
indeed recieves, but when i get the socket of the sender (which is the
localhost) and i try to send it a message - the message never arrives,
so “second message recieved” text is never being shown and the program
is in an infinite loop. please tell me what is the problem because im
stuck for a week already…

Don’t call SDLNet_TCP_Accept() a second time…you are sending on the
first socket still, but the reading portion of your code throws it away
(without closing it) and waits for a second connection, instead of
reading from the first again.

There are other bugs in the program, but if you get rid of the second
Accept and keep using the original somecomputer value, it gets to the
scanf("%s") call.

–ryan.

Don’t call SDLNet_TCP_Accept() a second time…you are sending on the
first socket still, but the reading portion of your code throws it away
(without closing it) and waits for a second connection, instead of
reading from the first again.

i don’t really undestand what did you say. my main problem with understanding you is this sentence :
“you are sending on the first socket still” - what do you mean by first?

There are other bugs in the program, but if you get rid of the second
Accept and keep using the original somecomputer value, it gets to the
scanf("%s") call.

–ryan.


No need to miss a message. Get email on-the-go
with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/mail

----- Original Message -----
From: icculus@icculus.org (icculus)
To: A list for developers using the SDL library. (includes SDL-announce)
Sent: Thursday, March 1, 2007 10:27:15 PM
Subject: Re: [SDL] problem with using socket from SDL_Net SDLNet_TCP_Accept