Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
benedetta is currently offline  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 #9116 is a reply to message #4381] Wed, 18 April 2007 09:33 Go to previous messageGo to next message
lundman is currently offline  lundman
Messages: 175
Registered: March 2006
Location: Tokyo
Experienced Member

Heh only just saw this, but well, probably too late, but we used my own "lion" library with U++ to do everything nonblocking, and platform independent.

But that was a little bit cheating, in that we setup a U++ timer to call the lion code periodically.

http://www.lundman.net/wiki/index.php/LiON

Re: Socket non block [message #9119 is a reply to message #9116] Wed, 18 April 2007 10:40 Go to previous messageGo to next message
fallingdutch is currently offline  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
Socket example? [message #10984 is a reply to message #4381] Thu, 09 August 2007 20:59 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
Hello!
I want to develop a client/server socket application.
Ultimate++ GUI is very suitable for the task, especially the array control.
I need to use sockets that do not block the GUI.
I know there is Web/Socket.h, can anyone give me one simple client and server source code example? I can't seem to find it nowhere in documentation.

Thanks!
Re: Socket example? [message #10986 is a reply to message #10984] Fri, 10 August 2007 04:55 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
http://msdn2.microsoft.com/en-us/library/aa371713.aspx
win32 & COM->networking->windows socket 2 or something
Re: Socket example? [message #10987 is a reply to message #10984] Fri, 10 August 2007 05:57 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
I think you misunderstood me.
I really want source examples for using the U++ Web/Socket.h classes.
icon11.gif  Re: Socket example? [message #10988 is a reply to message #10984] Fri, 10 August 2007 08:16 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
O yeah. I did not notice it. I suppose it is mainly for http or ftp, since it's WEB and for streams only.

uppsrc's web/tserv is a U++ project, you might wanna take a look @ it. i did not look carefully. and there's a plugin/ftp

i seem to redoing this kind of thing Sad but i use UDP, too, in my project

[Updated on: Fri, 10 August 2007 09:05]

Report message to a moderator

Re: Socket example? [message #10996 is a reply to message #10984] Mon, 13 August 2007 16:58 Go to previous messageGo to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
Anyone?
Re: Socket example? [message #10999 is a reply to message #10996] Mon, 13 August 2007 18:34 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Would about this thread:
http://www.ultimatepp.org/forum/index.php?t=msg&goto=438 1&&srch=block+socket#msg_4381
?
Re: Socket example? [message #11005 is a reply to message #10984] Mon, 13 August 2007 21:00 Go to previous messageGo to next message
lectus is currently offline  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 #11007 is a reply to message #11005] Mon, 13 August 2007 22:52 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
It's difficult to guess what you need and what you know. Please ask "smaller" questions and give some more background.
Have you studied U++ examples httpcli etc.?
Or, I'd strongly suggest start programming and post your packages here (you can check forlano first posts here who came from C background...). Also add comments with pseudo-code.
Re: Socket example? [message #11010 is a reply to message #11005] Mon, 13 August 2007 23:32 Go to previous messageGo to next message
mezise is currently offline  mezise
Messages: 54
Registered: April 2006
Member
lectus wrote on Mon, 13 August 2007 21:00


Could you give me an example of ServerSocket and ClientSocket?

Searching through upp sources for ServerSocket and ClientSocket you may find e.g.:
- \uppsrc\Web\httpsrv.cpp(251)
- \uppsrc\Web\httpcli.cpp(120)
- \uppsrc\Web\smtp.cpp(176)

In examples there are cmdsrv and imgsrv packages using HttpServer class and there is httpcli package using HttpClient.

Maybe that will help.
Michal
Re: Socket example? [message #11012 is a reply to message #11010] Tue, 14 August 2007 04:06 Go to previous messageGo to next message
lectus is currently offline  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 Go to previous messageGo to next message
fudadmin is currently offline  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 #11026 is a reply to message #11024] Wed, 15 August 2007 08:29 Go to previous messageGo to next message
fallingdutch is currently offline  fallingdutch
Messages: 258
Registered: July 2006
Experienced Member
have a look at the downloads of my hp or at this post and check the XML-RPC source, it is out of date, a new one will come soon, but i guess it shows how to use sockets of Upp

Bas
Re: Socket example? [message #11032 is a reply to message #10984] Wed, 15 August 2007 20:33 Go to previous messageGo to next message
lectus is currently offline  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 #11037 is a reply to message #11032] Thu, 16 August 2007 08:12 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
Well, first of all, U++ is cross-platform, so don't use M$ stupiphic Smile things "\r\n" (look in forums for info on this).
Second, Uvs2 package has some useful things like file parts transfer, compression etc.
Third, sorry, I'm off till Sunday (probably).
Re: Socket example? [message #11063 is a reply to message #11032] Fri, 17 August 2007 19:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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
Re: Socket example? [message #11091 is a reply to message #11063] Sun, 19 August 2007 14:23 Go to previous messageGo to next message
fudadmin is currently offline  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 Smile

lectus, sorry for trying to confuse you about those endlines.
Re: Socket example? [message #11097 is a reply to message #10984] Sun, 19 August 2007 17:23 Go to previous messageGo to previous message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
Hello!
I know about \r\n vs \n, but I'm building mainly for Windows.
Also, I really asked about how to read/write line-buffered data from/to sockets, instead of byte-buffered raw data?

Thanks
Previous Topic: Again ReadMemoryBarrier() etc
Next Topic: Threading example for U++
Goto Forum:
  


Current Time: Fri Mar 29 00:24:29 CET 2024

Total time taken to generate the page: 0.01132 seconds