Click here to Skip to main content
Click here to Skip to main content

Peer-to-Peer Communicator and File Transfer

By , 14 Aug 2001
 

Sample Image - SimpleChat.jpg

Prologue

On this web site you would probably find every functionality that was put into this application. I realize that. Hence I'm not going to post the source code just yet. However, I've put it up here to see how much interest this application would attract, specifically if there is any interest in the techniques that I've used to implement it. I'm going to enumerate some of the techniques in this short article. Let me know if there is anything of interest to you and I will probably write up a separate article for whatever is requested.

Introduction

This is a peer-to-peer communicator that allows you to chat with a single person at a time and exchange files simultaneously.

Sockets, ..., sockets, ... Zzzzzap! (::) (Connection established)

A socket library built upon Berkley sockets was designed and implemented. I've taken a well known approach of having an object package itself into a stream on the sender's side and do the opposite on the receiver side. Using the library above, a hierarchy of classes is introduced to accommodate the file transfer.

Multithreading

A set of classes encapsulate the threading/synchronization Windows API. Each thread is responsible for handling logically separated functional areas of the software. That is, I've introduced:

  • A UI thread.

    This thread is the MFC's main thread that handles all UI components. This thread is a subscriber for event notifications from the rest of the threads.

  • A couple of Listener threads.

    One of which is to listen for incoming requests for the first connection with the remote party to be established. Once the connection is up, another Listener is brought alive to await for any possible requests to send a file.

  • A Sender thread.

    This thread handles the file sending functionality.

  • A Receiver thread.

    This thread handles the file receiving functionality.

Event Model

The Sender and Receiver threads notify the UI thread about the progress of the work by adding notification events into the shared memory queue. The UI thread polls the queue for the notifications every few milliseconds. This approach is good enough for such a simple application, but if I was to have a lot of worker (Hello Microsoft! :)) threads, I should have probably created a dedicated worker thread which all it would do is to sit locked on the queue's mutex, until an event is added and then do UI_Thread::PostThreadMessage(), so that UI would have much faster response to the application events.

Epilogue

I hope by now you're not yelling at the monitor, hoping that I might hear it "Where is the Source Code?!". Well, honestly I don't believe that one can learn a lot from a load of not very well documented source code. So if there are people who want to know, let me know what part of this software would be of most interest to you and I'll come up with an article.

08/15/2001

Well, after getting several emails with the source request, I figured I should probably give it to the community :) Unfortunately I don't have time to explain all of the concepts of the NetLib that is included here, but basically this library allows you in a limited form to marshal an arbitrary C++ object(s) across a network. Some threading and synch classes are included and provider/consumer design pattern is applied for communicating with the UI thread. As soon as I get a brake from work, I'll come up with a better article. Actually let me know if there is a need for it.

Well happy coding to you, I hope this will be of some help.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marat Bedretdinov
Web Developer
United States United States
I work as a software engineer for 12 years and currently building IONA's Artix ESB.
 
Also check out my company that implemented a Thin Java Object Broker that remotes over HTTP protocol and implements J2EE APIs with ~100 KB runtime on the client side.
 

http://www.jproxy.com


 
Happy coding,
Marat

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionPeer-to-Peer Communicator and File TransfermemberMember 1003840117-May-13 9:38 
how to make your project in vb.net
GeneralMy vote of 5memberMember 43208447-May-12 9:42 
Thanks for sharing.
Questionhow to run the program...???memberMember 861304130-Mar-12 0:36 
can you tell me how to compile the program. i am really a beginner please.
Questionhow to run the programmembermax_lord21-Oct-10 21:31 
can u tell me how to compile the program...i am really a beginner please
Generalproblemmemberdonon25-Jan-10 20:51 
i need a java source code for Peer To Peer connection. i will need it as many as possible because am using it to do some school stuff.
Generallooking for the more clear articlememberMember 417159017-Nov-09 4:52 
Where are your article more clear about this project
General[Message Deleted]memberit.ragester2-Apr-09 21:53 
[Message Deleted]
GeneralThank youmemberLi Shu30-Oct-08 2:46 
I'm gonna read the code
GeneralVS 2005memberewasta12-May-08 10:15 
has anyone gotten this to compile in VS2005? I'm getting tons of errors.
thanks
AnswerRe: VS 2005memberItai Basel24-Jun-08 9:09 
There are several changes to make for it to work:
 
1.
File: gdi/hoverbutton.cpp
Change: void CHoverButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
To: LRESULT CHoverButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
And Add: return 0;
at the end of the function.
 
2.
File: gdi/hoverbutton.h
Change: afx_msg void OnMouseHover(WPARAM wparam, LPARAM lparam) ;
To: afx_msg LRESULT OnMouseHover(WPARAM wparam, LPARAM lparam) ;
 
3.
File: gdi/mimage.cpp
Function: CMImage::CreateLogicalPalette()
Replace:
  DIBSECTION ds;
  BITMAPINFOHEADER &bmInfo = ds.dsBmih;
  m_pbmSource->GetObject(sizeof(ds), &ds);
 
  int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
 
With:
  BITMAP bm;
  int rc = m_pbmSource->GetBitmap(&bm);
  int nColors = 1<<bm.bmBitsPixel;
 
4. [Optional]
If you want - you can define WINVER on stdafx.h (e.g. #define WINVER 0x0500) to prevent vista from being selected automatically
 
5.
On: netlib/safevector.cpp
Replace: template <class t=""> list<t>::reference safe_list<t>::back()
With: template <class t=""> typename list<t>::reference safe_list<t>::back()
And
Replace: template <class t=""> list<t>::reference safe_list<t>::front()
With: template <class t=""> typename list<t>::reference safe_list<t>::front()
 
6.
On: netlib/safevector.h
Replace: list<t>::reference    front();
With: typename list<t>::reference    front();
And
Replace: list<t>::reference    back();
With: typename list<t>::reference    back();
 
7. [Optional]
On: SimpleThreadedChat/msgconverter.cpp
Replace: for (int i=0; i<pnetlist->GetElemN(); i++) {
With: for (unsigned int i=0; i<pnetlist->GetElemN(); i++) {
 
8.
On: SimpleThreadedChat/simplechat.cpp
Replace: vector* pVec;
With: vector<int>* pVec;
And
Replace: pVec = (vector*)&vecInt;
With: pVec = (vector<int>*)&vecInt;
And [Optional]
Replace:	Enable3dControls();			// Call this when using MFC in a shared DLL
With: //	Enable3dControls();			// Call this when using MFC in a shared DLL

9.
On: SimpleThreadedChat/rtfstream.cpp
Replace: SetText( CString( ch ) );
With: SetText( CString( (char)ch ) );
And
Replace: return streambuf::overflow( (char)ch );
With: return streambuf::overflow( ch );
 
10.
On: SimpleThreadedChat/SystemTray.cpp
Replace: Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID);
With: BOOL Create(CWnd* pParent, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID);
 
11.
Add to the SimpleThreadedChat project properties Linker>Input>Additional Dependencies: NetLib.lib
[Taht Is:
Replace: ws2_32.lib GDI.lib
With: GDI.lib NetLib.lib
//Remark: the ws2_32.lib is not used - I guess it is what they were expecting the NetLib.lib to be called
]
 
12.
Add to the NetLib project properties C/C++>Preprocessor>Preprocessor definitions: ;_AFXDLL
[That is:
Replace: WIN32;_DEBUG;_LIB
With: WIN32;_DEBUG;_LIB;_AFXDLL
]</int></int></pnetlist-></pnetlist-></t></t></t></t></t></t></class></t></t></class></t></t></class></t></t></class>

 
Yours,
<><<br mode="hold" />Itai

Generalquestionmembersajjad alizadeh9-May-08 6:47 
hi
i need vb or delphi image processing source code for cnc
please help me.
thank you.
Questionfile transfer with peer to peer network ?memberbenard4-Mar-08 4:11 
i have been having problems coding a file tranfer program in java. i have suceeded in creating a peer to peer connection but i need help help on implementing file transfer into the code
QuestionNat Traversal?memberbluewater02092-Mar-08 21:36 
Thanks for your work!I want to know the detail about nat traversal!Hope your article!
Generalfatal error C1083memberleenkinpark30531-Oct-07 19:12 
hey...
 
i've downloaded the source code. when i tried to compiled, it says that "fatal error C1083: Cannot open include file: 'SimpleChatDlg.h': No such file or directory". so, where can i get the file??
 
thanx..

Questiontransfer filememberhwzmail8-Oct-07 23:27 
question:
Can do transfer much files at one time? I want to implement transfering several files at one time like qicq.
Generalthe progrem use too mach memory!membertwblue24-Jun-07 23:13 
Thank you for your work, this knwoledge is useful of me. but there is a problem on my computer: the progrem use too mach memory!
my environment: memory: 512M cup: Celeron 2.66G.
GeneralRe: the progrem use too mach memory!memberjan bont31-Jul-08 3:10 
I have xp with 3GB of memory, 2,5 GB free. When starting this program, it says: out of memory.
So look at it as a usefull learning project, but is is practically unuseable.
AnswerRe: the progrem use too mach memory!memberDr. B.M. Linden11-Sep-09 9:03 
In MImage.cpp, CMImage::Load(UINT nBm):
Replace:
HBITMAP hBm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), lpszBm, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR/*LR_CREATEDIBSECTION*/);
With
HBITMAP hBm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), lpszBm, IMAGE_BITMAP, 0, 0, /*LR_DEFAULTCOLOR*/LR_CREATEDIBSECTION);
Questiondoes it resolve domain names?memberbreakpoint27-Mar-06 20:32 
Great library. I just want to ask it I can use domain name instead of IP address to connect. Example: I will use "c1.mynetwork.com" instead of "192.168.0.1" in my connection.
 
"We don't see things how they are. We see things as we are." -Talmud
GeneralRe: peer to peer file transfermemberFabianSauer21-May-06 21:06 
Hi.
I have similar problem you have posted. Have you found any answer?


Questionhow to compile and create an exe filememberhow jack21-Feb-06 15:48 
Laugh | :laugh: Thanks
 
jack
QuestionSending unicode characters over the network?memberJockeP21-Nov-05 4:17 
How to I transfer unicode characters over the network using Windows sockets
in C++? I'm only aware of how to send ascii characters (e.g.
 
//SOCKET socket; char str[];
send(socket, str, (int) strlen(str) + 1, 0)
 
).
 
The send function does only take ascii characters. I'm not using MFC.
QuestionFile Transfer via Serial Port between 2 pc'smembermayurshsawant8110-Nov-05 6:17 
Can someone send me a source code of file Transfer via serial port (COM1) using vc++ (MFC).
 
Mayuresh
Generaldata transfer receive and sendsussNIdoh5-Sep-05 21:11 
data transfer receive and send using VB6 do you have source code or programs sending a ms access file or .mbd file to Server from a client via HTTP or internet. pls help me with this.
GeneralPlease help me!I get a access violation in &quot; m_data.m_bDir = h.IsDirectory();&quot;memberMichael sun25-Jul-05 3:20 
Dead | X| the code is in 178 row of NetFile.cpp,if you have resolved the question,please help me,thanks again!
Generalhelp me ,out of memory, I am looking for this java appmemberchenningcheng10-Dec-04 1:00 
I am looking for this java app too,can I get it ,
thanks you
when I run it,
"out of memory" other PC with 256 RAM ,it still "out of memory"

Questionhow to assign a specific ip address to server and clientmembersyaks28-Oct-04 1:54 
i am a beginer to socket programming, i read your article i think it the best for beginers like me, i have two questions;
1. my question is how can i assign a specific ip address to server and client for example 193.125.100.10 and 193.125.100.11 for server and client respectively.
2. 2nd question is i want the application to act as server and client simultaneously, and can chat with multiple peers simultaneously.
i will be greatful for your help, and i am sure you will help beginner like me.
thanks and bye.
 
ShehrYar.





QuestionCan anyone Send me the compiled versionsussdreammiku24-Aug-04 9:10 
i want to see this, but i dont know C++ how can i have look ...will you please compile it and send me so that i can see !
 
also i need one comercial version, if i likw i will give you the order to make one for me.
 
i want to use it in my <a href="http://www.datingfunda.com">Dating Site</a>
 
Mithilesh Keshari
mithilesh@dreamszsop.com
www.dreamszsop.com
GeneralThanks...memberbellerofonte13-Aug-04 3:42 
Hi,
Thank yuou for your job;
I want to ask a question; It is possible to implement a new functionality for to connect with more persons?
 
Thanks for your attention.
by
crismer
GeneralRe: Thanks...memberMarat Bedretdinov16-Aug-04 9:05 
There is nothing that restricts you to do so in NetLib right now. The only thing is that you'll have to reimplement the UI classes.
 
Cheers,
Marat
 
Kagetsya dogd' nashinaetsya Smile | :)
QuestionHow to increase the speed of transmission?memberwiskey15-Jun-04 16:40 
It is much slow than copying the same file through WINDOWS.Confused | :confused:
 
Net loading is always about 1%.
QuestionHow to read a file from remote machinememberKmAshif27-Apr-04 4:00 
I want to read a file from a remote(LAN)machine from my program.The machine Where the program will run is also connected with the LAN. The program also knows the remote machine's address(i.e \\server1), user name and password. Can anybody help me....Thanks
GeneralbobsussAnonymous30-Mar-04 16:57 
hi how you doing i luv u
GeneralIt's great!memberrong.xia6-Mar-04 16:03 
but..where's the exe file? your project is so huge,it's hard to understand.. : )
GeneralPeer to Peer communication in network sharingmemberAtif Mushtaq29-Aug-03 21:10 
Hi Everyone
I am developing a server based chat application , there is also a direct client to client file transfer , just MSN File Transfer.
This is easy when two parties are directly connected to internet with dial up in this case they both will have unique ip.But the problem is if supose one client is using internet with LAN sharing and other is connected to internet through dial up then , so for the first one we can only get the ip adress assigned to net server to which we are connected with internet , not the machine which is connected with it through LAN , so no direct client to client connection.
But i know MSN messenger is doing it so there must be some way doing this . CAN SOMEONE HELP ME ABOUT THIS
GeneralRe: Peer to Peer communication in network sharingmemberBharath Chopra12-Apr-04 1:25 
I have similar problem you have posted. Have you found any answer?
 

 

GeneralRe: Peer to Peer communication in network sharingmemberAtif Mushtaq12-Apr-04 4:39 
Well a streight forward solution is install a proxy server on machine which is giving to the LAN PC's(Or user is already getting sharing through proxy server) and client use HTTP/Sock4/Socks5 proxy to connect with a PC attatched with private network , if u use any of three mentioned proxy , proxy server will open a direct connection for u.
 

But if LAN Pc's are not getting sharing through proxy server , then comes the NAT type of sharing , in this case one will have to implement NAT tunneling , currently i am working on it ,if i succeeded in it i 'll inform u.So for the time being proxy is only applicable solutions.
Atif Mushtaq.
 


 
Unmanaged in a .NET world
GeneralRe: Peer to Peer communication in network sharingmemberyanping wang15-Jul-05 1:10 

 
i am now facing the same problem. can you provide me some code about NAT tunneling? i will be very gratefull.
GeneralHELPsussfjour1-Jul-03 18:24 
Need help of the code!@
I download already but i don know how to run it?
Use????
Pls tell me the stpes
I m a college student!
TY urgent for project now ty again

Generala question about NATsussAnonymous11-Jun-03 22:55 
thanks for your work,it's useful to me,but i have a question:
can we implemente peer-to-peer communication in the presence of NAT?(suppose all clients lie behind diffrent NAT, this application can work yet?)

GeneralRe: a question about NATmemberMarat Bedretdinov12-Jun-03 9:57 
You need to be more specific here. But in general if you're talking about NAT's not only mapping public-private IPs but also blocking incomming traffic and all ports, then this surely will not work.
 
If say port 80 is open, then you can set up a port forwarder.
If you have a proxy that parses the application traffic, then the only solution is a HTTP tunneler.
 
For a standalone tunneler check this out.
 
http://www.htthost.com/
 
For J2EE tunneler:
 
http://jproxy.com
 
Hope this helps,
Marat
 
Kagetsya dogd' nashinaetsya Smile | :)
GeneralProblems with MemorymemberLeDaemon3-Jun-03 23:20 
Hi,
 
I tried to fix the Memory Problem by using the ZeroMemory-command, but now i cannot link and execute the tool. i get 8 failures and 30 warnings. and even if i comment the ZeroMemory line, i'm unable to get the tool running.....
 
can anybody help me?!?!
 
thanx
JP
GeneralExcellent work!memberaldenhung30-Apr-03 4:25 
You're work is excellent.
Many feature is useful for my software development!
After little modified, it complied succefully in VC7.
Best regard.
 
By the way
in
BOOL CSimpleChatApp::InitInstance()
{
//vector* pVec;
vector vecInt;
vecInt.push_back(0);
vecInt.push_back(1);
//pVec = (vector*)&vecInt;
//int n1 = (*((vector*)pVec))[0];
//int n2 = (*((vector*)pVec))[1];
}
The marked line make error in VC7 (.net)
I just mark it, and I guess it do nothing!?
GeneralRe: Excellent work!memberMarat Bedretdinov30-Apr-03 5:07 
You're right, I was just playing with pointers and forgot to remove this garbage code. Sorry Smile | :)
 
Happy Coding,
Marat
 
Kagetsya dogd' nashinaetsya Smile | :)
GeneralAnybody know of such an app in JAVAmemberfindneville12-Apr-03 16:09 
I am looking for JAVA code for exactly the same application.
Any ideas?
 
Neville
GeneralRe: Anybody know of such an app in JAVAmemberE.T.28-Apr-03 18:05 
I know a project in java witch is more like a share file peer-to-peer project(like kazaa)...
 
is that what you're looking for?
GeneralRe: Anybody know of such an app in JAVAsussAnonymous29-Apr-03 2:44 
Yeah exactly that is what i am looking for. Let me know if you can help me with the code you have asap.
Thanks! NEVILLE
GeneralRe: Anybody know of such an app in JAVAmemberchenningcheng4-Dec-04 16:00 
I am looking for this java app too,can I get it ,
thanks you
GeneralRe: Anybody know of such an app in JAVAmemberanjolie13-Feb-09 2:56 
me too....can some one pls send me the java code for this application
 
anjolie.nair@gmail.com
GeneralVC++ 7memberMeshal15-Nov-02 2:02 
Hi,
 
I tried to compile the code with VC++ 7. It doesn't work. It generates some errors. Confused | :confused: Did any one try to compile it with VC++ 7? I will appreciate your help. Rose | [Rose]
 
Regards

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 15 Aug 2001
Article Copyright 2001 by Marat Bedretdinov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid