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 » Community » Newbie corner » Trouble with Socket
Trouble with Socket [message #36457] Mon, 28 May 2012 22:15 Go to next message
jonarmani is currently offline  jonarmani
Messages: 6
Registered: May 2012
Location: Texas
Promising Member
I just started throwing some programs together with U++ (just downloaded the latest stable release today), and am running into the following compiler errors from TheIDE:

C:\MyApps\TestServer\main.cpp:13:2: error: 'Socket' does not name a type
C:\MyApps\TestServer\main.cpp:14:13: error: ISO C++ forbids initialization of member 'port' [-fpermissive]
C:\MyApps\TestServer\main.cpp:14:13: error: making 'port' static [-fpermissive]
C:\MyApps\TestServer\main.cpp:14:13: error: ISO C++ forbids in-class initialization of non-const static member 'port'
C:\MyApps\TestServer\main.cpp:16:2: error: expected unqualified-id before 'if'
C:\MyApps\TestServer\main.cpp:20:2: error: expected unqualified-id before 'if'
C:\MyApps\TestServer\main.cpp:72:1: error: expected '}' at end of input
C:\MyApps\TestServer\main.cpp:72:1: error: expected unqualified-id at end of input
TestServer: 1 file(s) built in (0:01.32), 1326 msecs / file, duration = 1326 msecs, parallelization 0%

There were errors. (0:01.38)



This is even after just a copy/paste of the server code from the Connection-Oriented Socket Tutorial.

Here is my code. Should I put the Socket code somewhere else beside the Top Window? Am I missing a #include?

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

#define IMAGECLASS TestImg
#define IMAGEFILE <Test/images.iml>
#include <Draw/iml.h>

struct TheWindow : TopWindow
{
	MenuBar menu;
	Socket accept_socket, data_socket;
	int port = 2000;
	// Listen for connections using _accept_socket;
	if( !ServerSocket(accept_socket, port) ){
    	throw Exc("Couldn't bind socket on the local port.");
	}
// You can do this in a loop to accept many connections:
	if( accept_socket.IsOpen() ){
    	dword ip_addr;
    	// Hand off successful connection to _data_socket
    	if( !accept_socket.IsError() && accept_socket.Accept(data_socket, &ip_addr) )
    	{
        	Cout() << "Connection from " << FormatIP(m_ipaddr) << "\n";
        	// Read from the socket until it is closed, has an error, or you see an end-of-file marker
        	// (EOF optional and application-specific)
        	while(data_socket.IsOpen() && !data_socket.IsEof() && !data_socket.IsError())
        	{
            	Cout() << data_socket.Read();
        	}
    	}
    	Cout() << "\n";
	}
	
	void Exit()
	{
		if(PromptOKCancel("You really wish to exit?")) Break();
	}
	
	void SubMenu(Bar& bar)
	{
		bar.Add("Exit", TestImg::exit, THISBACK(Exit));
	}
	
	void MainMenu(Bar& bar)
	{
		bar.Add("Menu", THISBACK(SubMenu));
	}
	
	virtual void Paint(Draw& w)
	{
		w.DrawRect(GetSize(), Black);
		w.DrawText(20,20,"Server is running", Arial(30), Green);
	}
	
	typedef TheWindow CLASSNAME;
	
	TheWindow()
	{
		Title("Test Server");
		AddFrame(menu);
		menu.Set(THISBACK(MainMenu));
	}
};

GUI_APP_MAIN
{
	TheWindow app;
	app.SetRect(0,0,400,100);
	app.Run();
}


Re: Trouble with Socket [message #36458 is a reply to message #36457] Tue, 29 May 2012 08:17 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
jonarmani wrote on Mon, 28 May 2012 16:15

Am I missing a #include?



Indeed. You need to add Web package and #include "Web/Web.h".

Alternatively (and better), socket encapsulation was recently refactored into new TcpSocket class, which is part of Core (so you have it alread included).

See http://www.ultimatepp.org/src$Core$TcpSocket$en-us.html

Mirek
Re: Trouble with Socket [message #36486 is a reply to message #36458] Fri, 01 June 2012 04:47 Go to previous messageGo to next message
jonarmani is currently offline  jonarmani
Messages: 6
Registered: May 2012
Location: Texas
Promising Member
Thanks for your response. I'm now trying to figure out how to use the TcpSocket class correctly. Looks like it's what I need, I just need to be a better programmer. Smile
Re: Trouble with Socket [message #36509 is a reply to message #36458] Sun, 03 June 2012 19:04 Go to previous messageGo to next message
jonarmani is currently offline  jonarmani
Messages: 6
Registered: May 2012
Location: Texas
Promising Member
Ahhh! After messing with this for so long, I figured out I needed to download the latest nightly release for Socket or TcpSocket stuff to work! I was using version 4193 previously, which I believe was the latest "stable" release.

Is it standard practice for everyone to be using the latest nightly release? The one I grabbed was version 5021.
Re: Trouble with Socket [message #36511 is a reply to message #36509] Sun, 03 June 2012 20:19 Go to previous messageGo to next message
BioBytes is currently offline  BioBytes
Messages: 307
Registered: October 2008
Location: France
Senior Member
Hi Jonarmani,

A lot of improvments are generally implemented in the nightly builds. My way is to rename the installation directory of my current stable U++ version and install the lastest build (ex. I rename Upp directory Upp-5010 for example and install the latest version ex. U++5021 in Upp).

So I can try the new build with minimal risk and can return easily to the previous stable version by renaming back the Upp-XXXX to Upp.

Regards

Biobytes
Re: Trouble with Socket [message #36584 is a reply to message #36511] Mon, 11 June 2012 16:49 Go to previous messageGo to next message
jonarmani is currently offline  jonarmani
Messages: 6
Registered: May 2012
Location: Texas
Promising Member
Ok, I really like that suggestion. Thank you. Smile
Re: Trouble with Socket [message #37144 is a reply to message #36457] Wed, 29 August 2012 11:34 Go to previous message
jash08 is currently offline  jash08
Messages: 1
Registered: August 2012
Junior Member

Quote:

Sets timeout for all operations. Zero means that all operations return immediatelly (in that case it is usually a good idea to perform some sort of external blocking on socket or socket group using e.g. SocketWaitEvent). Null means operations are blocking (but they still can invoke WhenProgress periodically if defined). Other values specify a number of milliseconds. Note: It is possible to adjust timeout before any single TcpSocket operation. Returns *this.



Thanks for the information. i really appreciate it a lot.

click here

[Updated on: Wed, 29 August 2012 11:35]

Report message to a moderator

Previous Topic: running program at different speeds
Next Topic: VectorMap:how to replace a Value
Goto Forum:
  


Current Time: Thu Apr 25 22:17:16 CEST 2024

Total time taken to generate the page: 0.06433 seconds