|
#sckt usage examples Server sidefirst of all we need to include sckt header file #include <sckt.hpp> After that we can start using the library //In case of errors the sckt::Exc exception is thrown
//So, enclose the network code into try/catch.
try{
//Initialize the library by creating a singletone object.
//It will deinitialize the library when the object
//will be destroyed due to going out of scope.
sckt::Library socketsLib;
//create TCP server socket for listening
sckt::TCPServerSocket listenSock;
//open listening socket and start listening
listenSock.Open(80);//start listening on 80th port
//Accept some connection
sckt::TCPSocket sock;
while(!sock.IsValid()){
sock = listenSock.Accept();
}
//if we get here then new connection is accepted.
//send some data
sckt::byte data[] = {'0', '1', '2', '4'};
sock.Send(data, sizeof(data));
}catch(sckt::Exc &e){
std::cout << "Network error: "<< e.What() <<std::endl;
}Client sidetry{
sckt::Library socketsLib;
//create IP address for connecting
sckt::IPAddress ip("127.0.0.1", 80);//we will connect to localhost 80th port
sckt::TCPSocket sock;
//connect to remote listening socket
sock.Open(ip);
//receive some data (note that it is a blocking call)
sckt::byte buf[1024];
sock.Recv(buf, sizeof(buf));
}catch(sckt::Exc &e){
std::cout << "Network error: "<< e.What() <<std::endl;
}Socket Setsnote that it is also possible to use socket sets (sckt::SocketSet) to check one or more sockets for activity. Using the socket sets is analogous to using the select() function from BSD sockets API. To use socket set one needs to create the sckt::SocketSet object first: //create a socket set for 10 sockets at maximum
sckt::SocketSet sockSet(10); After creating the object one needs to add some sockets to the socket set: sckt::TCPSocket s1, s2, s3;
sockSet.AddSocket(&s1);
sockSet.AddSocket(&s2);
sockSet.AddSocket(&s3); Now we can check the sockets from socket set for activity. To do this we use sckt::SocketSet::CheckSockets() function, which takes a timeout as argument which specifies for how long to wait if there are no sockets with activity. It will return true if there are some sockets with activity or false if there is no any activity on sockets from socket set. //sckt::SocketSet::CheckSockets() will set a ready flag for all sockets with activity
bool res = sockSet.CheckSockets(1000);//1 second timeout
if(res){
//There are some active sockets.
//This means that there are data available for reading from socket
//or remote socket has disconnected.
//... handle socket activity
}After sckt::SocketSet::CheckSockets() returned true we can check every socket for activity with sckt::Socket::IsReady() function: if(s1.IsReady()){
//try to read some data from socket
sckt::byte buf[1024];
//after calling Recv() the ready flag will be cleared
sckt::uint len = sock.Recv(buf, sizeof(buf));
if(len == 0){
//0 received length means remote socket disconnection
//Handle disconnection
//...
}
}
if(s2.IsReady()){
//...
}
//etc.Error HandlingNote that in case of errors a sckt::Exc exception is thrown. Some of the examples above do not catch any exceptions just for code simplicity. In real life it is a good practice to catch exceptions ;-). UDP Socket server side#include "sckt.hpp"
try {
sckt::Library socketsLib;
sckt::UDPSocket rcvSock;
sckt::IPAddress ip("127.0.0.1", 5060);
rcvSock.Open(5060);
while(1){
sckt::byte buf[1024];
rcvSock.Recv(buf, sizeof(buf), ip);
}
}catch(sckt::Exc &e){
std::cout << "Network error: "<< e.What() <<std::endl;
}UDP Sending side#include "sckt.hpp"
try {
sckt::Library socketsLib;
sckt::UDPSocket sendSock;
sckt::IPAddress ip("127.0.0.1", 5060);
sendSock.Open();
sckt::byte data[] = {'0', '1', '2', '4'};
sendSock.Send(data, sizeof(data), ip);
}catch(sckt::Exc &e){
std::cout << "Network error: "<< e.What() <<std::endl;
}
|
UDP Socket server side (The same comments as in TCP Socket) #include "sckt.hpp"
try {
} catch(sckt::Exc &e) { }UDP Sending side #include "sckt.hpp"
try {
} catch(sckt::Exc &e) { } }Sorry for Google formatting. Mayby it will be better to send this examps to Ivan by mail to post it here.
you can use WiKi? markup to format your comments (see the help bar to the right from "Enter a comment" field)
I reformatted your example:
UDP Socket server side
#include "sckt.hpp" try { sckt::Library socketsLib; sckt::UDPSocket rcvSock; sckt::IPAddress ip("127.0.0.1", 5060); rcvSock.Open(5060); while (1) { sckt::byte buf1024?; rcvSock.Recv(buf, sizeof(buf), ip); } } catch(sckt::Exc &e) { std::cout << "Network error: "<< e.What() <<std::endl; }I also added your example to the page.
thx a lot ))
Is it possible to get the IPAddress from a TCPServer Accept call. Now you only get the socket object, but how do you get the ip address (and the port) of the TCPSocket client?
vdvleon,
good point! It is not possible now. I will add this in next sckt release. I filed a bug for this: http://code.google.com/p/sckt/issues/detail?id=19
Thank you for your finding!
No point! ;)
I like your work, it works great and is easily extenable (the classess). I am making some additions right know such as Send Recv with std::string and a threaded client handler (i use the QThread class).
Thanks for doing this!
I don't think we need to add threading support; that seems a bit outside what this library is trying to accomplish. Although it would be cool if someone else made a project using this library that included threading ;)
Also do you think its possible to have something like the SocketSet? only with a growable amount of sockets?
Thanks again ~ Ryan
Hi Ryan,
check out my another library which includes threading: http://code.google.com/p/ting/
Actually it also includes sockets now (the latest version in SVN), and I plan to discontinue sckt library support because of that.
Also, I am preparing a library for TCP client-server, where server can manage multiple TCP connections in different threads. In C++. Contact me if you are interested in it, I will gather the things up and upload the code, I already created a page for it but did not upload the code yet: http://code.google.com/p/cliser/
Thanks, Ivan