Writing a C++ program that listens on a port.

What's needed for a C++ program that listens on an open computer p

Page 1 of 1

8 Replies - 9314 Views - Last Post: 25 August 2010 - 09:37 AM Rate Topic: -----

#1 macb6497  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 80
  • Joined: 25-December 09

Writing a C++ program that listens on a port.

Posted 23 August 2010 - 04:49 PM

Hello everyone,

What I’m trying to do is make a C++ program that will listen on a port, port 6000 to be exact, and capture all the requests from that port. I have a program called wireshark that shows all the “transactions” that take place throughout an Ethernet port or entire modem. This will help a little in learning how a specific program that I want to capture requests from sends out its requests. Firstly, what exactly would I be looking for when it comes to making a C++ program that will listen and then take in all the requests that come in through that port?

What I have now is a simple program that runs in an infinite “while” loop. In that loop it will go through an “if” statement that checks the port for requests. If it picks up a request then it will gather that data and process it and then “cout” the answer/data back to the requesting program. The way my program will work is sort of how secretaries in big companies do. You’re trying to get in contact will the boss, in this case the harddrive, and my program says, “I have detected your request. Let me take this information, give it to the boss and then give any information I receive back to you (the request on the port).”

I’m not looking for anyone to make a program for me; I won’t tell if you do ;) I’m just looking for what will be needed when making a program that listens on a port.

P.S. I already know that computers do this and that I can download a program to do it. But, what would be the point of learning a computer language if I can’t type my own?

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: Writing a C++ program that listens on a port.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: Writing a C++ program that listens on a port.

Posted 23 August 2010 - 04:53 PM

standard C++ does not support comports, But there are many third party comport libs out there. The easiest one i have ever used was with the QT library:

http://qextserialport.sourceforge.net/

Makes things very simple

This post has been edited by ImaSexy: 23 August 2010 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: Writing a C++ program that listens on a port.

Posted 23 August 2010 - 05:36 PM

Beej's Guide: A Simple Stream Server.

If you're using Windows, there's this tutorial.
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Writing a C++ program that listens on a port.

Posted 23 August 2010 - 07:31 PM

Port 6000 is a TCT port, right? I have a simple solution, maybe too simple.

Use inline assembly (Visual C++ has nice a simple inline assembly functions) and use the IN instruction.

For example, how to input a byte from port 6000 using visual c++
	unsigned char PortData;
	__asm
	{
		mov dx, 6000
		in al, dx
		mov PortData, al
	}
	// PortData now contains a byte of data that was at port 6000


EDIT: As Salem_c pointed out, this instruction allows you to access x86 memory mapped IO ports for accessing internal hardware and such, not useful for accessing TCP ports as far as I'm aware.

This post has been edited by Aphex19: 24 August 2010 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Writing a C++ program that listens on a port.

Posted 23 August 2010 - 07:47 PM

Here is how I would go about this (for Windows)

I would use Winsock


Look At
http://www.madwizard...orials/netcpp/4
For Help With Winsock


Here Is Pseudo-code For The Server Side, It's your job to write the C++ code

Socket listeningSocket

Start Winsock

Infinite Loop
{
   Create The Socket

   Bind To Port 6000

   Listen For Client

   Output "Connection Has Been Established"
  
   Close Socket
}

Cleanup Winsock



This post has been edited by Splatocaster: 23 August 2010 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 macb6497  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 80
  • Joined: 25-December 09

Re: Writing a C++ program that listens on a port.

Posted 24 August 2010 - 10:05 AM

Thanks for the answers.

Answer 3 is more pinpointed to what I was looking for. I'll just use WireShark to study how the program I want to gather information from sends out requests.

Answer 4 is similar to what I'm doing except I have a few "if" statments and "else" statements that would either "continue" or "break".

Thanks,
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1706
  • View blog
  • Posts: 3,268
  • Joined: 30-May 10

Re: Writing a C++ program that listens on a port.

Posted 24 August 2010 - 10:19 AM

> For example, how to input a byte from port 6000 using visual c++
This kind of port has NOTHING to do with IP protocol ports.

Start with "Beej" if you want to access resources "a la internet".

Internet Protocol ports are listed here
http://www.iana.org/...ts/port-numbers

X86 I/O ports (as used by in instructions and the like) are for accessing specific bits of hardware on your local machine (in the days before PCI and USB came along).
http://www.linux.com...ort-Programming
http://www.cs.cmu.edu/~ralf/files.html

This post has been edited by Salem_c: 24 August 2010 - 10:27 AM

Was This Post Helpful? 0
  • +
  • -

#8 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Writing a C++ program that listens on a port.

Posted 24 August 2010 - 03:23 PM

View PostSalem_c, on 24 August 2010 - 09:19 AM, said:

> For example, how to input a byte from port 6000 using visual c++
This kind of port has NOTHING to do with IP protocol ports.


I apologise, I seem to have got mixed up with memory mapped IO there. Thanks for pointing that out. That's seriously stupid of me :blush:

This post has been edited by Aphex19: 24 August 2010 - 03:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Writing a C++ program that listens on a port.

Posted 25 August 2010 - 09:37 AM

If you are fairly new to programming networked applications you might want to use a library like http://sfml-dev.org

It has a networking module and simplifies networking tenfold (maybe not tenfold but you get the idea).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1