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