|
|
Home » U++ Library support » U++ MT-multithreading and servers » Non-Blocking socket example
Non-Blocking socket example [message #4381] |
Thu, 03 August 2006 19:23  |
benedetta
Messages: 1 Registered: August 2006
|
Junior Member |
|
|
Hi to all!
I'm a new user of "ultimate++" and i need to creare a simple non blocking application!
Can some1 attach an exsample?
Thx to all!
|
|
|
|
Re: Socket non block [message #9119 is a reply to message #9116] |
Wed, 18 April 2007 10:40   |
fallingdutch
Messages: 258 Registered: July 2006
|
Experienced Member |
|
|
You can use UPP-Sockets nonblocking
you need the Package "Web" and "Web/SSL" if you need SSL support
Then these two functions create sockets for you.
bool ServerSocket(Socket& socket, int port, bool nodelay = true, int listen_count = 5, bool is_blocking = true);
bool ClientSocket(Socket& socket, const char *host, int port, bool nodelay = true, dword *my_addr = NULL, int timeout = DEFAULT_CONNECT_TIMEOUT, bool is_blocking = true);
Set "is_blocking" to false.
Socket::Wait is a call to select
Bas
|
|
|
|
|
|
|
|
|
Re: Socket example? [message #11005 is a reply to message #10984] |
Mon, 13 August 2007 21:00   |
lectus
Messages: 329 Registered: September 2006 Location: Brazil
|
Senior Member |
|
|
I still don't understand how to use the socket classes.
(Sorry, I'm newbie at C++. Used C before. Now moving to C++ and Ultimate++ framework).
Could you give me an example of ServerSocket and ClientSocket?
Thanks
I'm building a client/server application. U++ is perfect for the GUI, but I'd like to use the Sockets classes, so I don't need to use the lower level Winsock directly.
U++ GUI + Sockets classes + the simple compression library zlib, all these already in U++, will be enough for my application.
Thank you again.
[Updated on: Mon, 13 August 2007 21:01] Report message to a moderator
|
|
|
|
|
Re: Socket example? [message #11012 is a reply to message #11010] |
Tue, 14 August 2007 04:06   |
lectus
Messages: 329 Registered: September 2006 Location: Brazil
|
Senior Member |
|
|
Well, I've checked the http examples. But I really need pure sockets, that I can implement my own protocol.
I really code use winsock.h, but the class from U++ would be more suitable once I learn how to use it. So, I search for examples.
I'll give more details:
The program will be a program to share files bewtween PCs over the internet using TCP/IP.
It will be a very simple P2P, able to comunicate between 2 machines, both sending and receiving files from each other.
Both connect via IP and Port using TCP sockets.
I'll use an array control to show the data of the files, like name, size, progress of download/upload, etc.
I'll have also a bunch of buttons and check boxes.
Pretty simple GUI. But array control from U++ will make it a powerful and easy to use application.
Thanks
|
|
|
Re: Socket example? [message #11024 is a reply to message #11012] |
Wed, 15 August 2007 04:51   |
 |
fudadmin
Messages: 1321 Registered: November 2005 Location: Kaunas, Lithuania
|
Ultimate Contributor Administrator |
|
|
Could this help as a starting point for your server part:
#include <CtrlLib/CtrlLib.h>
#include <Web/Web.h>
using namespace Upp;
#define LAYOUTFILE <P2PSocket/P2PSocket.lay>
#include <CtrlCore/lay.h>
class SocketApp : public WithMainLay<TopWindow> {
Socket sock;
public:
void SSwitch();
typedef SocketApp CLASSNAME;
SocketApp();
};
void SocketApp::SSwitch()
{
if (ssw==0) {
sock.Close();
PromptOK("4662 socket server CLOSED. You can check it with nmap command");
}
else if (ssw==1) {
ServerSocket(sock, 4662, true, 5, false);
PromptOK("4662 socket server OPENED. You can check it with nmap command");
}
else
PromptOK("Warning: check your switch inits!");
}
SocketApp::SocketApp()
{
CtrlLayout(*this, "P2PSocket Server");
ssw.SetLabel(0, "STOP 4662");
ssw.SetLabel(1, "START 4662");
ssw=0;
ssw.WhenAction = THISBACK(SSwitch);
Sizeable().Zoomable();
}
GUI_APP_MAIN
{
SocketApp().Run();
}
P2PSocket.lay:
LAYOUT(MainLay, 400, 200)
ITEM(Switch, ssw, HSizePosZ(144, 172).VSizePosZ(68, 96))
END_LAYOUT
|
|
|
|
Re: Socket example? [message #11032 is a reply to message #10984] |
Wed, 15 August 2007 20:33   |
lectus
Messages: 329 Registered: September 2006 Location: Brazil
|
Senior Member |
|
|
fudadmin: Thanks! That was a good starting point.
I'm connecting to 4662 port using telnet to check the connection.
I try sock.Write("Welcome!\r\n") and String data sock.Read(Null, 250), for writing and reading strings, but it seems to not be working.
What's right way to read and write lines from/to the client?
ReadRaw() looks OK for files. What about strings with \n at the end?
Thanks!
I tried this:
void test::BtngetPush()
{
sock.Write("Welcome!\r\n");
String data = sock.ReadUntil('\n', Null, 1000000);
editdata <<= data;
}
While btnget is a button, that when clicked should executed that code. editdata is EditString control. But that is not working. =\
[Updated on: Wed, 15 August 2007 20:36] Report message to a moderator
|
|
|
|
|
Re: Socket example? [message #11091 is a reply to message #11063] |
Sun, 19 August 2007 14:23   |
 |
fudadmin
Messages: 1321 Registered: November 2005 Location: Kaunas, Lithuania
|
Ultimate Contributor Administrator |
|
|
luzr wrote on Fri, 17 August 2007 18:21 |
lectus wrote on Wed, 15 August 2007 14:33 |
What's right way to read and write lines from/to the client?
ReadRaw() looks OK for files. What about strings with \n at the end?
|
Well, "\n" / "\r\n" stupidity....
U++ way is to not to try some sort of automatic "text" translations (like those special binary/text modes in C fopen etc..) in streams.
In most cases, we prefer to manually supply '\r' on output and ignore it on input (only watch for '\n').
Mirek
|
Mirek, could you put that into FAQ's "about endlines" (or similar), please?
This really looks like the third STUPIPHIC way, then.
STrange Unexpected soPHisticated specifIC 
lectus, sorry for trying to confuse you about those endlines.
|
|
|
|
Goto Forum:
Current Time: Sun Apr 27 18:24:20 CEST 2025
Total time taken to generate the page: 0.00664 seconds
|
|
|