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

Chat program

By , 2 Dec 2002
 

Sample Image - CSocket.jpg

Introduction

This very simple Chat program was written by using VC++ 6 under Windows 2000. CSocketCli program creates a client socket. CSocket program creates a server socket. If you run the client program, you must specify the address of the other computer in the IP Address editbox in "the server program".

If the client is running but the server isn't running, you will receive an error in the editbox down in the client program, but if you connect you will receive "Connected" message in the chat editbox in the client program.

When you run the server, you don't need to do any thing; just wait until some client program connects with the server. This program handles multiple clients and you can have private chat with friends.

Important note: If you run the Client and the Server in the same computer, please write in the client IP Address textbox the address 127.0.0.1, then press Connect button and you will be connected to the server.

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

essam el-nagar
Software Developer (Senior)
Saudi Arabia Saudi Arabia
Member
No Biography provided

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   
QuestionThank you for easy to follow code for Client and Server Sides...memberDestiny7779 Feb '13 - 12:46 
The sample code works very well.
 
Thank you very much for posting it here.
 
---
 
The only improvement that could be helpful to others would be to make 1 program to allow either side of the connection to be the host or the client depending on which side initiated the connection.
 
---
 
All in all... very nice Server and Client Chat Programs.
 
Thanks again!
GeneralFirewallmemberAlexEvans21 Aug '10 - 23:22 
Hello,
 
If I want to use this program Client's side on my PC at home, and wish to connect to a "Server" that is behind a firewall somwhere... How do I conncet and get through the firewall?
 
Cheers
Alex
GeneralRe: FirewallmemberDestiny7779 Feb '13 - 12:41 
Alex,
 
If you look at the code you will find that it is set to use port 5000. (TCP/IP Winsock calls.)
 
* See the Client Program on this line of code: cli.sin_port=htons(5000);
 
* See the Server Program on this line of code: serv.sin_port=htons(5000);
 

You simply find the internal intranet address for "Server" PC and open port 5000 for inbound and outbound traffic on that PC via the Router and/or your software firewall program.
 
You would do the same if you want to allow port 5000 on your "Home" PC to connect to the "Server" PC.
 
It is pretty easy to do.
 
Smile | :)
Questionhow are youmembercen_jin_long22 Feb '10 - 12:41 
hope hide...
Optional. This allows you to set your preferences for the discussion boards

GeneralDownload = 0kbmembercodeinelogic8 Jan '09 - 7:53 
this happening to anyone else?
GeneralRe: Download = 0kbmembercodeinelogic8 Jan '09 - 8:02 
url read "secure.codeproject" researched and was able to download
GeneralThank you.memberMember 232736913 May '08 - 22:01 
A easy, useful, powerful code. Big Grin | :-D
Generalhome editionmembervishal dhir24 Feb '07 - 20:16 
hi to all!!
is socket programming possible through xp home edition to professional edition between 2 pcs??
Questionhow to use it in campusmemberMember #368157930 Dec '06 - 0:05 
hi...........
can you please tell me how to use this code to allow
two systems(in the adjacent classrooms) to chat.
 
pranjal
Questionhow to use it in campusmemberMember #368157930 Dec '06 - 0:04 
hi...........
can you please tell me how to use this code to allow
two systems(in the adjacent classrooms) to chat.
Questionwhat is socket programmingmembermahili19 Nov '06 - 21:28 
Sigh | :sigh: I really need to know about socket programming and how to use it while building a chat room and connection.It is very important to me 'cause i am doing my senior project.
 

Thanks.write me soon please!!!!!!Confused | :confused: Confused | :confused: Suspicious | :suss: Suspicious | :suss:
AnswerRe: what is socket programmingmemberkkyaw8823 Feb '07 - 10:45 
Interesting.
 
kkyaw88

Generalvery usefullmemberfserdary9 Aug '06 - 4:51 
Thanks For the code man. I think it is very usefull
 
N/A
Generalgood codememberSujitmatrix2 May '06 - 4:21 
really helpful
 
sujit
Generalexcellent sample codememberGeorge98765432122 Mar '06 - 22:15 
nice work u've submitted
fairly simple to figure out
 
but for some people, it'd be nice if u
- formatted code im a more readable format
- included comments
- gave variables a more meaningful name
(i know it was last updated in 2002, but hey its still pretty useful)
 
apart from that, nice work *thumbs up*
GeneralCMDmemberPablo9836 Mar '06 - 11:37 
How do i get rid of the graphical interface and just run the program from the comand prompt. Help would be greatlly appreciated.
Generalgood job~~memberrosenus12 Feb '06 - 4:58 
I've struggled to make "no server chat system".
 
Your job gave me some senses....
 
I appriciate to you. Laugh | :laugh:
GeneralCheck the status Wether Connect to the internet or notmemberKutti Ra30 Sep '05 - 23:07 
Hello Friends,
Actually I am try to develop the Application to manage my browsing center. I need given points

1. How many system currently connect to the internet
2. If the no of system connected, how long the system continue this connect.(when login/logout)
3. If the system is conneted, list out the user name.
4. If the user used printer, How many pages they printed.
5. If the user used cd-writer, How many CDs they writed.
 
Please send any helps tips my id or display FAQ Page
Generalover the internetmembermesut_pak12 Mar '05 - 5:44 
i learn my ip form www.whatsmyip.org and i wrote it to client program's server ip textbox.
 
but it cannot connect to the server although server runs. normally, if i use 127.0.0.1 they can communicate with each other. whats the problem? i suppose the problem is these programs use a port that windows don't open it to internet..
 
can anybody help me?
GeneralRe: over the internetmemberXiaozhou Ye6 Nov '05 - 21:10 
Maybe your program is behind the firewall.Smile | :)
Questionhow to make one application work as a server and client simultaneouslymembersyaks29 Nov '04 - 7:19 
i am a beginer to socket programming, i read your article i think it the best for beginers like me,
my question is i want the application to act as server and client simultaneously, and can chat with multiple peers simultaneously. what should i do to make the application work in the above mentioned way,
thanks and bye.
 

 
ShehrYar.
AnswerRe: how to make one application work as a server and client simultaneouslymemberDestiny7779 Feb '13 - 12:54 
While learning, it would be best to do ONLY 1 server and 1 client.
 
When you start to go beyond 1 to 1 connections, that gets a bit tricky to send and receive the messages between multiple connections. Especially when you have people coming and going in the Text Chat. It can be done... but takes a lot of attention to detail to get that done. Unless you want to spend a lot of time, you might want to use programs like ICQ that do that already.
 
The easiest way to handle multiple connections on the server would be to have a list of connected IP Addresses, then: (1) when a message is sent out from Server send that message to each connected IP Address; or (2) when a message is received (incoming) at the Server, then send out that message to each connected IP Address except for the one that just sent the message to the server.
 
Keeping a list of connected IP Addresses wouldn't necessarily be difficult but you have to consider what happens when a connection is dropped, and much more.
 
Smile | :)
GeneralTransmit Image FilememberKeel15 Jan '04 - 20:15 
Hi, I have a question? How to transmit image file using CSocket? I cannot figure it out.. Pls help me....Sigh | :sigh: Thx
GeneralIPnr in list boxmemberJockeP24 Aug '03 - 18:41 
Instead of having an edit box, as in the picture, I have the IPnr 0.0.0.0 in the list. What is wrong?
GeneralBind ErrormemberHazem Nasereddin7 Jan '03 - 2:54 
Very nice piece of code.
 
I tried the code on Win 98 and It worked, but It gave me a bind error on Win XP.
 
Any idea what the problem might be?
 

GeneralRe: Bind Errorsusskaizer25 Jun '03 - 5:45 
The problem is cause i think windows xp binded the port 5000. so what you could do to get it working is to change the port to something else rather then 5000 for both client and server

GeneralRe: Bind Errormemberasimrehan131 Mar '13 - 8:47 
I am using the given application but when i uses the code in my application the socket never binds and given socket = 4294967295.
But when i uses the given application it works fine, can you please guide me?
 
asim
GeneralThanks...memberMustafa Bayer4 Dec '02 - 8:45 
Thaks...;)Rose | [Rose]
 
Mbayer
GeneralChat With US DImemberDany Cantin26 Nov '02 - 16:56 
An other great chat!
 
http://www.codeproject.com/useritems/ChatWithUS.asp
 
- The hell is paved good intentions.
GeneralRe: Chat With US DI <<< Liggity LamesussAnonymous4 Dec '02 - 6:57 
Hah - Look at this fool plugging his own submission in another contributors message board. lame man, lame.
 
By the way, Essam - I didn't dl the project but from the brief it looks usefull especially for beginners - Thanks for making codeproject a little bit better. Smile | :)
GeneralRe: Chat With US DI <<< Liggity LamememberDany Cantin5 Dec '02 - 3:30 
It is your own opignion Anonymous!!!
Any way, fy!
 
- The hell is paved good intentions.
GeneralRe: Chat With US DI &lt;&lt;&lt; Liggity LamesussAnonymous7 Jun '05 - 15:59 
Hello are you single?
GeneralThread Datamemberkmaz5 Aug '02 - 11:52 
I really love this sample code on how to write a chat. Greate winsock sample. One thing though. When you write a program to communicate with a port and use threads its typically a good idea to set it's Process Priority setting high. To do this inside the thread() function in this case, near the begining put the following code:
 
::SetPriorityClass(::GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
 
This makes the program much less likely to lose, or corrupt data. The previous way rarely loses information, but to be safe you should add it.
 
Great work though. Thanks. Cool | :cool:
 
Ken Mazaika
QuestionHow to make it workmemberGautam9826 Jun '02 - 20:31 
i ran the server process on one machine and the client program on a different machine and did as it is told but it didnt work the client keeps waitin for the connection to establish....can u plz tell me how to make it runFrown | :(
AnswerRe: How to make it workmembertomdekok21 Jul '02 - 1:31 
The prog works perfect!(perhaps maybe a fixable memory leak).
You should check:
 
Are you using the right IP addess and port on both the Host and Client?
Are you using a firewall? If so, turn it off or make the use of the port in question possible.
 
I assure you it DOES work like a charm, it is on your side.
 
Tom
 
____________________________________
Using Visual Studio .NET Architect, including J++.
 

AnswerRe: How to make it workmemberahkeR8 Mar '07 - 7:45 
:(i ran both the sever and client programme on the same machine ,it does not work.The client machine keeps waiting.... Please tell me how to make it work.
GeneralClient Server ChatmemberMunty5410 May '02 - 19:37 
Need help in designing a client server chat application
 
Now, I am summing it all up. 1. The client server need to be in different processes. 2. Communicate with TCP/ip sockets. 3. Communication is secured with SSL or S-HTTP. 4. Messages recorded in a server side database by the server. 5. A time stamp for each recorded message. 6. The client can request its messages from the server and display it in its own window. But can not change the messages. Cry | :((
 
abhishek
GeneralRe: Client Server Chatsussbamaruckmani27 Sep '04 - 19:11 
Now, I am summing it all up. 1. The client server need to be in different processes. 2. Communicate with TCP/ip sockets. 3. Communication is secured with SSL or S-HTTP. 4. Messages recorded in a server side database by the server. 5. A time stamp for each recorded message. 6. The client can request its messages from the server and display it in its own window. But can not change the messages.
 

GeneralThank you!!!!memberAnonymous12 Mar '02 - 22:20 
Thank you;Thank you;Thank you!!!!!!!!!!!!!;P Poke tongue | ;-P
GeneralRe: Thank you!!!!membertomdekok21 Jul '02 - 1:33 
LOL I agree with you 1000%.
 
Tom Big Grin | :-D
 
____________________________________
Using Visual Studio .NET Architect, including J++.
 

GeneralYou have memory leaksmemberJacques11 Dec '01 - 16:00 
HI!
I was searching within your project why you had memory leaks when the program exited. You use AfxBeginThread, that create a thread, but create a memory leak if you use C Run-Time Library.
 
In MSDN, they tell to use _beginthread() / _endthread()instead of AfxBeginThread / AfxEndThread()
 
I tried it in your app, and it worked, so if you would like to correct it, it would be great!
 
bye
 
Jack
papejack@hotmail.com
Generalhelp if you canmemberconwaytowers16 Oct '01 - 5:18 
please i am learning vb6 code and have been trying to make a chat program if eny one could help me please. or even bet give me a program to do it we are at college and it is boring and the teacher told use to not go on the e-mail when we are in class and i want i new way if you can help here is my e-mail address (a5296@tcat.ac.uk) please sent programs to here thanks i lot.
 
Mad | :mad:
 
conwaytowers
GeneralquestionmemberJordan25 Sep '01 - 19:11 
Can these project realize multi people communication at
the same time?
GeneralThank youmemberUday T23 Jul '01 - 4:44 
Thank you sir,
This really worked. I'd tested it.
 
Uday T.
(Development Engineer)
MSE (I) Pvt. Ltd. India.
Generalnumber of users on this applicationmemberradical8 Jun '01 - 4:35 
I wanna know how many people can communicate together in the same timeConfused | :confused:
GeneralExplainationmemberMaria Jothi29 May '01 - 19:46 
Hello Sir ,
I'm new to this Socket Programming . Could you please explain it in detail.
On executing the Program , i'm getting both the dialog Boxes. But Dont Know How to proceed ???
 
Maria
 

Maria Jothi (Software Engineer)
MAIL-ID:maria@sdsindia.com
maria_jothi@usa.net
GeneralRe: Explainationmemberzarzor3 Dec '02 - 15:07 
Socket programming and samples code can be found in BC++/Help/Windows API.Rose | [Rose] Rose | [Rose] Rose | [Rose]
 
RJS
GeneralThank You very muchmemberAmit Malik4 Apr '01 - 20:56 
Thank You very much sir. I have been searching the code but couldn't find one. I am a beginner level programmer and find it difficult with multi-threading.
Thanking You again.
Yours sincerely
Amit MalikBlush | :O
QuestionHow to know when the ConnectionRequest occursmembermhs20 Mar '01 - 18:54 
How to know when the ConnectionRequest occurs in the thread?
and your code has bug when client disconnected. your code always receive
from buff.
AnswerRe: How to know when the ConnectionRequest occurssussXin3 Aug '02 - 16:08 
I agree that a bug occurs on disconnecting the socket. Maybe calling CloseSocket is too early. If you simply terminate the client program, the server side handles this perfects. This means some instances or threads are not correctly released.

Antradar Software
 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 3 Dec 2002
Article Copyright 2001 by essam el-nagar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid