Babulesnik
Member
|
Problem with a network
I want to transfer the data to myself, but the program hangs up after button click "KB_DOWN"
SockAddr my_adres,serv_adres;
Socket my_socket;
my_socket.createUdp();
my_adres.port(1234);
my_adres.ip("127.0.0.1");
serv_adres.port(1234);
serv_adres.ip("127.0.0.1");
if (Kb.bp(KB_ENTER))
{
...
my_socket.send(my_adres,text_enter,SIZE(text_enter));
}
if (Kb.bp(KB_DOWN))
{
Str rcv_text;
Int r_in=my_socket.receive(my_adres,&rcv_text,SIZE(rcv_text));
if( r_in>0) rezultat=rcv_text;
}
|
|
02-06-2011 02:12 PM |
|
PRG3D
Member
|
RE: Problem with a network
In tutorial (Socket.cpp) is:
Code:
// create this instance socket (internet connection)
{
// try to initialize socket on one of the ports starting from BASE_PORT
// some ports will not be accessible because other program instances may have already reserved them
FREP(50)
{
sock.createUdp();
if(sock.bind(SockAddr().setServer(BASE_PORT+i)))break; // 50 attempts, stops on first usable port
}
sock.block(false); // set non-blocking mode, this will disable waiting when receiving data through socket
}
// make contact with other sockets created by other program instances
{
SockAddr addr; // socket address
addr.ip("127.0.0.1"); // set address ip to 'localhost', which is your computer
FREP(50) // now check 50 ports where possibly other instances of the tutorial have created a socket
{
addr.port(BASE_PORT+i); // set port of the address
if(addr.port()!=sock.port()) // if it's on a different port than our socket (which means that it's not us)
sock.send(addr,NULL,0); // send empty data just to contact the other socket to let it know that we exist
}
}
(This post was last modified: 02-06-2011 04:23 PM by PRG3D.)
|
|
02-06-2011 04:23 PM |
|
Babulesnik
Member
|
RE: Problem with a network
(02-06-2011 04:23 PM)PRG3D Wrote: In tutorial (Socket.cpp) is:
Code:
// create this instance socket (internet connection)
{
// try to initialize socket on one of the ports starting from BASE_PORT
// some ports will not be accessible because other program instances may have already reserved them
FREP(50)
{
sock.createUdp();
if(sock.bind(SockAddr().setServer(BASE_PORT+i)))break; // 50 attempts, stops on first usable port
}
sock.block(false); // set non-blocking mode, this will disable waiting when receiving data through socket
}
// make contact with other sockets created by other program instances
{
SockAddr addr; // socket address
addr.ip("127.0.0.1"); // set address ip to 'localhost', which is your computer
FREP(50) // now check 50 ports where possibly other instances of the tutorial have created a socket
{
addr.port(BASE_PORT+i); // set port of the address
if(addr.port()!=sock.port()) // if it's on a different port than our socket (which means that it's not us)
sock.send(addr,NULL,0); // send empty data just to contact the other socket to let it know that we exist
}
}
I did this code on an example socket.cpp. I need to learn why my code does not work
|
|
02-06-2011 04:49 PM |
|
Driklyn
Member
|
RE: Problem with a network
You shouldn't have that KB_DOWN if statement. Look in the Update function of Socket.cpp for how you should receive data.
|
|
02-06-2011 07:31 PM |
|
Babulesnik
Member
|
RE: Problem with a network
(02-06-2011 07:31 PM)Driklyn Wrote: You shouldn't have that KB_DOWN if statement. Look in the Update function of Socket.cpp for how you should receive data.
I do as in an example-same result.
for(SockAddr addr;
{
Str rcv_text;
Int r_in=my_socket.receive(my_adres,&rcv_text,SIZE(rcv_text));
if( r_in<0)break;
// lobbi_chat.getText("chat_list").set(chat_text.insert(0,rcv_text+"\n"));
rezultat=rcv_text;
}
|
|
02-06-2011 08:00 PM |
|
Driklyn
Member
|
RE: Problem with a network
Try editing Socket.cpp, replacing a few lines at a time with your code, testing often to see when it stops working. This should tell you where your problem(s) are.
|
|
02-06-2011 08:39 PM |
|