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 » Cant connect but dont get an error
Cant connect but dont get an error [message #39500] Sat, 23 March 2013 16:03 Go to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Hello,
sorry for my bad english, i'm from germany.

I want to check whether a port is open or not.

Here is my code:

main.cpp
#include "PortScan.h"
using namespace Upp;
PortScan::PortScan() {
	CtrlLayout(*this, "Simple Portscanner");
	InBtnScan <<= THISBACK(Button);
}

void PortScan::Button(){
	int sPort 	= InStartPort;
	int ePort 	= InEndPort;
	String url 	= InStrUrl;
	TcpSocket socket;
	
	
	socket.Connect(url, sPort);
	socket.Timeout(200);
	
	if(!socket.IsError()){
		PromptOK("Port is open");
	}
	
	socket.Close();
}

GUI_APP_MAIN {
	PortScan().Run();
}


PortScan.h
#ifndef _PortScan_PortScan_h
#define _PortScan_PortScan_h

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

#define LAYOUTFILE <PortScan/PortScan.lay>
#include <CtrlCore/lay.h>

class PortScan : public WithPortScanLayout<TopWindow> {
	public:
		typedef PortScan CLASSNAME;
		PortScan();
		void Button();
};

#endif


PortScan.lay
LAYOUT(PortScanLayout, 524, 152)
	ITEM(EditInt, InStartPort, HSizePosZ(68, 212).TopPosZ(80, 19))
	ITEM(EditInt, InEndPort, LeftPosZ(68, 244).TopPosZ(104, 19))
	ITEM(EditString, InStrUrl, LeftPosZ(68, 244).TopPosZ(56, 19))
	ITEM(Label, dv___3, SetLabel(t_("URL")).LeftPosZ(4, 56).TopPosZ(56, 20))
	ITEM(Label, dv___4, SetLabel(t_("Start Port")).LeftPosZ(4, 56).TopPosZ(80, 20))
	ITEM(Label, dv___5, SetLabel(t_("End Port")).LeftPosZ(4, 56).TopPosZ(104, 20))
	ITEM(Button, InBtnScan, SetLabel(t_("Scan")).LeftPosZ(4, 56).TopPosZ(132, 15))
END_LAYOUT

The URL Input field defines the host, Start Port defines the Port to check. End Port isnt used yet (Want to create a simple Portscanner).

I compiled and tested my program under OpenSuSE 12.1
uname -a:
Linux linux-m3d8.site 3.1.10-1.16-desktop #1 SMP PREEMPT Wed Jun 27 05:21:40 UTC 2012 (d016078) x86_64 x86_64 x86_64 GNU/Linux


The program compiles without an error, but it says every port on every host is open. How can i change this? I allready tryed to use "if(socket.IsOpen())" - but this didnt change anything.

[Updated on: Sat, 23 March 2013 16:04]

Report message to a moderator

Re: Cant connect but dont get an error [message #39560 is a reply to message #39500] Sat, 30 March 2013 09:47 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
This is a bit more tricky than it seems - socket API (I mean, Linux/Win32, not U++ encapsulation) is highly optimized and queued, for example Connect just starts connection to the port, without actually finishing it - and OS might detect that to connection is not available a couple of operations later.

Anyway, I have tried and this seems to work:

	TcpSocket socket;
	for(int i = 1; i < 120; i++)
		LOG(i << ' ' << (socket.Timeout(2000).Connect(url, i) && socket.Wait(WAIT_WRITE) && socket.Put("\n") == 1));


Please note that scanning ports is generally considered as form of attack...

Mirek
Re: Cant connect but dont get an error [message #39565 is a reply to message #39560] Sat, 30 March 2013 11:40 Go to previous message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Hello Mirek,
thanks for your help.

Quote:

Please note that scanning ports is generally considered as form of attack...

I know. I'm a whitehat. I want to understand how different tools work. I only use them on my own servers.

Previous Topic: How to call a C++ function in Witz template?
Next Topic: Witz internationalization
Goto Forum:
  


Current Time: Thu Apr 25 23:14:23 CEST 2024

Total time taken to generate the page: 0.06607 seconds