Jump to content

Socket programing


photo

Recommended Posts

Posted

Is there any Socket programming in Unigine api that for TCP or UDP?

If not, can I use a normal Winsock2 header?

Posted

thank you .

When using sockets, do I have to set the sending size and receiving size first? Or can I omit it?

 

socket->send(SEND_SIZE);

socket->recv(RECV_SIZE);

Posted

simplest example

 

// client socket 
Unigine::SocketPtr send_socket_ptr = Socket::create(Socket::SOCKET_DGRAM);                                                              
if (host_adress.size() && send_socket_ptr->open(host_adress, send_port) == 0)                                        
{                                                                                                                    
	Log::error("can't create client socket\n");                                                 
	return;                                                                                                          
}  

// listen socket
Unigine::SocketPtr recv_socket_ptr = Socket::create(Socket::SOCKET_DGRAM);                                                              
if (recv_socket_ptr->open(recv_port) == 0 || recv_socket_ptr->bind() == 0 || recv_socket_ptr->nonblock() == 0)       
{                                                                                                                    
	Log::error("can't create listen socket\n");                                              
	return;                                                                                                          
}    

///...............

// send
if (send_socket_ptr.get() && send_socket_ptr->isOpened())
{
unsigned char *packet_data = ....
int dest_size = ...
send_socket_ptr->write(packet_data, dest_size);                 
}

// read 
if (recv_socket_ptr.get() && recv_socket_ptr->isOpened() && recv_socket_ptr->isReadyToRead(1000))
{
unsigned char *packet_data = new unsigned char[MAX_PACKET_SIZE * 2];
int src_size = (int)recv_socket_ptr->read(packet_data, packet_size);
//...
}
                                                                                                                


you can also see the source code of the old СigiСlient plugin.
the way to work with our sockets has not changed.

  • Like 1
Posted

Is it the same for sending or receiving data to other programs as normal way in the example and for communicating data through the Blob class?

Blob is just usefull buffer?

Posted

yes. The Blob it`s just comfortable wrap over the unsigned char *

  • Like 1
×
×
  • Create New...