Problem with using socket from SDL_Net SDLNet_TCP_Accept - sorry for the previous message

sorry for the previous message, i was reading what you said again and again and i think you meant that i need to close the socket instead of giving it a zero value.

with this changed code the problem remains :

int main(int argc, char **argv)
{
int len = 0;
char text[20];
char msg[1024];
int mousexbefore = 0;
int mouseybefore = 0;
bool needtojoin = 0;
char needtojoinip[50];
Uint16 port;
pressed = 0;
mousex = 0;
mousey = 0;
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);
while (!(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);
SDLNet_TCP_Close(somecomputer);
while (!(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.
}
}
void translateip(IPaddress ipnativeform , char *ipstring)
{
Uint32 ipaddr;
ipaddr=SDL_SwapBE32(ipnativeform.host);
char one[6];
char two[6];
char three[6];
char four[6];
sprintf(ipstring, “%d.%d.%d.%d”, ipaddr>>24, (ipaddr>>16)&0xff, (ipaddr>>8)&0xff,ipaddr&0xff);
}____________________________________________________________________________________
Now that’s room service! Choose from over 150,000 hotels
in 45,000 destinations on Yahoo! Travel to find your fit.
http://farechase.yahoo.com/promo-generic-14795097

sorry for the previous message, i was reading what you said again and
again and i think you meant that i need to close the socket instead of
giving it a zero value.

No, you need to keep using the same socket. Don’t reset the variable,
don’t close it. All data sent between the two things talking need to go
over that socket. You only close it when both ends are done talking.

So the sequence is something like:

  • Server creates a socket for listening.
  • Client creates a socket for connecting (usually this is a different
    machine, you’re doing it all in one program though, but the principle is
    the same).
  • Client connects to server with socket it created.
  • Server accepts connection (this creates a separate “socket” from
    the listening socket it originally created. this is so it can accept
    multiple unique connections from different machines).
  • Client sends data, using socket it created.
  • Server receives data on that socket it created by accepting the
    connection. It can also use that same socket to send data to the client.
  • Repeat sending and receiving of data on both sides of the
    connection as long as you like.
  • When done, both ends close their own sockets, disconnecting.

Generally you don’t make a new connection for each message you want to send.

Google around for “Socket programming” … SDL_net is more or less just
a wrapper over “BSD sockets” so all the same principles and most of the
tutorials apply.

–ryan.

No, you need to keep using the same socket. Don’t reset the variable,
don’t close it. All data sent between the two things talking need to go
over that socket. You only close it when both ends are done talking.

looks like even when i don’t close the socket before trying to accept the second time, the problem still remains. your solution to the problem didn’t even made sense either, because , at the start of the program ‘somecomputer’ was closed. and in the first accept it got opened, then , if i close it before the first accept to the second accept why should it matter? if it got opened in the first time, it can be opened in the second time. i know you have showed me its a waste of CPU to close the socket. but i really need a way to fix this problem and to get to the last scanf


Bored stiff? Loosen up…
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front

----- Original Message -----

matter? if it got opened in the first time, it can be opened in the
second time.

You didn’t open it a second time in the source code you posted.

–ryan.

You didn’t open it a second time in the source code you posted.

i don’t really understand, i think i did open it both in the first time and in the second time.
is that what this line meant to do, isn’t it?
while (!(somecomputer = SDLNet_TCP_Accept(listeningsocket)));
the program waits for a message from the listening socket, (i do wonder sometimes why i don’t get spammed in this ‘while’ with other networking services that send data into my computer) and then it opens a socket with communication to the one who sent the message , and that ‘socket’ is : somecomputer. well, its like that for both messages - the first and second. and after the function returns a value other than 0 the program proceeds____________________________________________________________________________________
Don’t pick lemons.
See all the new 2007 cars at Yahoo! Autos.

bebe unknown wrote:

You didn’t open it a second time in the source code you posted.

i don’t really understand, i think i did open it both in the first
time and in the second time. is that what this line meant to do,
isn’t it? while (!(somecomputer = SDLNet_TCP_Accept
(listeningsocket))); the program waits for a message from the
listening socket

no, it waits for a remote socket to “connect”. it does not read any
data from the remote side. from all your posts, it seems you miss the
very basics of network programming. at least that’s the impression i
got, sorry if i’m wrong. so i suggest you first try to get your hands
on a book, or at least some online tutorials. you should read the
man pages for open (), close(), socket (), connect(), listen(), accept
(), read() and write().

btw. your posted source was completly messed up (no indention) so i
didn’t even bother reading it. posting with a real name wouldn’t hurt
either.

best regards …
clemens

i don’t really understand, i think i did open it both in the first time
and in the second time.
is that what this line meant to do, isn’t it?
while (!(somecomputer = SDLNet_TCP_Accept(listeningsocket)));
the program waits for a message from the listening socket, (i do wonder
sometimes why i don’t get spammed in this ‘while’ with other networking
services that send data into my computer) and then it opens a socket
with communication to the one who sent the message , and that 'socket’
is : somecomputer. well, its like that for both messages - the first and
second.

I think you have a fundamental misunderstanding about how sockets work,
and I don’t feel that I’m explaining it very well, so I’m going to point
you to some tutorials that probably explain it better.

http://gpwiki.org/index.php/SDL:Tutorial:Using_SDL_net

This explains network sockets in much more detail, but it’s not quite
SDL_net … the same principles apply, though.

http://beej.us/guide/bgnet/

–ryan.