Skip to main content
gamedev.net
🔒 Locked

Instant Messanging Program

Started by DigitalChaos 27 replies 3.7k views
Original Post
DigitalChaos
DigitalChaos
Hello everyone and happy new year! My resolution this year is to hopefully create a working Instant Messanging program. I have a working knowledge of C/C++, but the problem is i''m not sure where to start. Is C/C++ or C# the only languages I will need to know to create an IM program. I''m not sure all the details of how to create one or what it involves, i''m sure it''s gonna take a year to get a complete and good working build, and that''s find, I have time. I want this to be a good learning experience as I go along. Hopefully helped along in times. I have searched google but no luck. So in case this is a bit confusing my main questions are: 1.) What language(s) should it be written in? 2.) Where should i begin? 3.) Does anyone have experience in this? Ever since i''ve gotten into programming I''ve wanted to do something liek this, but I just haven''t gotten around to it. So i''ve made it a project for the new year. So any and all help would be appreciated. Thank you!
smart_idiot
smart_idiot
C/C++/C# are fine. This is an okay place to start learning about network programming.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
intransigent-seal
intransigent-seal
To try and answer your second question...
You can begin how you want, of course, but I would suggest that you start by deciding what features will definitely be included. For example:
Is it going to be plain text? Or will you let people send formatting information?
Will it always be one-on-one? Or will you provide a mechanism for group chat?
Are you going to let people send non-text data like images or other files?
Are you going to have contact lists?
Are you going to provide a mechanism to notify users when someone on their contact list comes online?

Now, you may of course decide that you want all these features, but you want to get a system working with just plain text, one-on-one chat to start with and then gradually extend it to include the extra features (getting a minimal system working would be a good idea, in my opinion). But you should be aware of the directions of development that you're likely to proceed in, after the minimal version is done. That way, you hopefully won't get caught out and find that you have to scrap a lot of what you've got already, because it is incompatible with the new feature you want to add.

Next, I would suggest you start making some extremely high-level architectural decisions. Things like:
Are clients going to send data through the server? Or will it just act as a "meeting point", with client chat being sent peer-to-peer?
Are you going to store information about users on the server? Or will you make it an open-chat system where anyone can log in without needing an account or password?

When you've made some of those high level decisions, it would probably be a good idea to work out your protocol. For an instant messaging system, I would suggest a text based protocol, for two reasons:
1) It means you can use something like a telnet client to test things, before you've even started writing the real client.
2) Text based protocols (when done right) are very flexible and backwards compatible. So when you want to add all those interesting new features, you will be able to do it without breaking earlier versions of your client.

Anyway. I've written far too much already. The next steps are fairly obvious (design lower level architecture... write code... test code... fix code... write code... test code... etc).

Oh, and as far as technical networking help goes, then if you're going to be targeting windows (and using WinSock), The WinSock Programmer's FAQ might be useful.

Hope that helps,

John B
"Please excuse the length of this letter; I do not have time to be brief." -- Unknown

[edited by - JohnBSmall on January 3, 2004 11:04:29 AM]
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Ruudje
Ruudje
using cbuilder to create it will definately make it easier for you since you will be able to use its TServerSocket and TClientSocket...
antareus
antareus
It is my current pet project and currently I have contact list importing, a bare UI, medium icons, and message receiving done on the free AOL TOC protocol.

It is an open, component-based architecture; an OOP realization of Miranda IM. I am aiming to kill Trillian and its stupid skins.

Do have a design beforehand, or you''ll get swamped. Now that I have one I am pretty much just typing code in and testing features out. I need a UI programmer, but so does every open source project.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
DigitalChaos
DigitalChaos
I read those socket guides and they are great. Thank you. I want to make a simple text sender first. Nothing fancy yet. Just get a text sender working. Then build off that. I''m not sure if it''s better to be p2p or go through a server. Which one would be better? Any suggestions?
dathui
dathui
quote:
Original post by DigitalChaos
I read those socket guides and they are great. Thank you. I want to make a simple text sender first. Nothing fancy yet. Just get a text sender working. Then build off that. I''m not sure if it''s better to be p2p or go through a server. Which one would be better? Any suggestions?


i would suggest a server-structure, i think it would be easier since you won''t have many users in the beginning.
reality is only an option
DigitalChaos
DigitalChaos
I really hate sounding like a n00b since i''m experienced in other things. But how do I create a server structure? And how do i send messages to it, and then go to the other person?
dathui
dathui
if client a sends a message to the server, you just check what clients is chatting with client a (this would mean some simple kind of contact-lists) and then send the message to those.

or you should do a half p2p/server thing...the server just sets the chatters up, when that is done they just send the messages to each other.

EDIT:
like JohnBSmall said

[edited by - dathui on January 5, 2004 6:26:16 AM]
reality is only an option
DigitalChaos
DigitalChaos
I want there to be usernames and passwords, so I''m assuming i''m gonna need a server and a database. Ok, fine, I know how to set up a database with MySQL. Would a MySQL database be sufficient enough to hold usernames and passwords? I read all the manuals and FAQs that were posted in response to my thread. I''m just not sure how to put it all together codewise. A little more info on what i want to accomplish in my first build.

Build 1 Goals:
- P2P Chatting
- Username and Password Verification
- Check if someone is connected


That''s pretty much all I want for a first build. Let me explain further. P2P chatting, I want the server to set up the "meeting point" with chat being sent peer to peer as JohnBSmall said. I want users to be able to "Log In", basically connect and have the connection either succeed or fail depending on whether the username and password matched or not. Now, i don''t want a fancy buddy list right now, what i want is just a user to be able to type another users name into a box and hit a button and have it say whether the user is connected. And if the user is connected have the option to send a message. I''m not sure how sending messages P2P works tho, is it sent in packets? So those are my goals for my first build. I''m not reaching for the stars...yet. Just want to get a working skeleton before i succeed. I have a feeling once I know how to set up and use a server with the program and be able to send messages, I''ll be able to do the rest without much help. It''s the beginning that''s the hardest and when I need the most help. I thank you guys for helping me. So my basic questions are(just to clarify):

-How do I set up a server for this kind of program?
-How do I set up a "meeting point" when two users want to chat?
-How do I get the server to know if someones online?




Metro_Mystery
Metro_Mystery
You could always write a server that you just hardcode the usernames and passwords into the application, but if you have time to learn new languages and computing concepts- the best approach imo would be to have you application access and manipulate data in a mySQL database.

You aren''t limited in protocols for your data transmission across the web either- you could use one of the very common protocols winsock offers such as TCP/IP. [Which is widely supported on many different platforms]

Also, I assume you''re going for something more than a console based IM- so go graphical; go Windows API or MFC.
DigitalChaos
DigitalChaos
Metro_Mystery,

I''m in luck then, I do know MySQL, but I only know how to manipulate it with PHP. I''ll look for some tutorials on MySQL manipulation with C/C++, but if anyone knows of any I''d be appreciative. And I will go graphical. Not console. I''m new to network programming, so I''ll have to read more into sending data using TCP/IP. I''ll be coding only for Windows at this time. So setting up a server consists of only creating the database? What then is a good method for a user to check if another user is online? How would I set the server so that it shows the user is logged in to the program and able to recieve messages?
DigitalChaos
DigitalChaos
Well, after, a simple google search, I found MySQL++, a C++ API for using MySQL. So I''ll be using MySQL to hold usernames and passwords and storing them on my server. But I''m still lookin for answers on how to set up my server to be a "meeting point" so two users can chat. I''ll begin working on the Graphical User Interface tomorrow. I see progress alreaedy people and it''s a bit exciting! And i''m still kinda stumped on the following questions: What is a good method for a user to check if another user is online? How would I set the server so that it shows the user is logged in to the program and able to recieve messages? Those are big questions i have along with anything else i posted.
Metro_Mystery
Metro_Mystery
So setting up a server consists of only creating the database?

Nothing is as simple as it seems- let me explain my friend...

The first step(s) to creating a very basic server would involve creating your application- be it console or Win32- adding ws2_32.lib to the linker options, including winsock2.h to your client source file(s) and doing this ->


if(0 != WSAStartup(MAKEWORD(2,2), &wsaData))
{
return FALSE;
}


This loads the winsock2 dll so that you can use the functions it provides for data transmission across a protocol such as TCP/IP.

The next step is rather hard to explain if you haven't network programmed before- but basically... the operation of a sever is to first create a thing called socket [in real terms - like putting a new telephone on your kitchen bench]

This 'socket' is then placed into listening mode [like connecting your telephone to the phone-line so that it is connected to the world and ready for an incoming call].


if(INVALID_SOCKET == (ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)))
{

if(SOCKET_ERROR == WSACleanup())
{

}
return FALSE;
}


You're phone is now connected and waiting to receive an incoming call... just like a socket is... You code then goes into a loop on this next function- the accept() function. It will not stop until a client tries to connect() to your IP address.


if(INVALID_SOCKET == (NewConnection = accept(ListeningSocket, (SOCKADDR *)&ClientAddr, ClientAddrLen)))
{
StatusBar.SetText((LPTSTR)lpMsgBuf, 1, 0);
closesocket(NewConnection);

if(SOCKET_ERROR == WSACleanup())
{

}
return FALSE;
}


Once connected, over this socket- you can then send data using:

char SendBuffer[255] = "Hey, how r u?";
send(NewConnection, SendBuffer, nBytes, 0);

or receive it through:

char RecvBuffer[255];
recv(NewConnection, RecvBuffer, nBytes, 0);

You COULD then send the username and password data from the client over to the server- but without encryption, it isn't 'safe'... take a look at public key encryption/cryptography...

Unfortunately though, I really can't help you with mySQL- I've never used the language- I only know what it's for...

Just a quick insite... hope it helps mate..



[edited by - Metro_Mystery on January 5, 2004 9:07:09 AM]
DigitalChaos
DigitalChaos
Wow, that is definitely helpful! I'll have to study it more and do some research when I haven't been awake for 40+ hours with no sleep. But I pretty much understand, is that code compatible with the .NET framework? I understand sockets. And I understand now how to send and recieve. I would open a socket, establish a connection, then...? It doesn't know where I'm sending the data, I've established a connection yes, but to the server, not to the person I want to chat to. Once I'm connected to the server, how do I then connect to the person I want to chat with? Great info Metro_Mystery, Thanx!


EDIT: I DON'T HAVE TO USE MYSQL, I WANNA USE WHATEVER WILL BE EASIEST FOR THIS PROGRAM!!!

[edited by - DigitalChaos on January 5, 2004 9:57:27 AM]
Metro_Mystery
Metro_Mystery
quote:
Original post by DigitalChaos
I would open a socket, establish a connection, then...? It doesn''t know where I''m sending the data, I''ve established a connection yes, but to the server, not to the person I want to chat to. Once I''m connected to the server, how do I then connect to the person I want to chat with?


Ok mate, another brief rundown...

WINSOCK SERVER -> socket() -> bind() -> listen() -> accept()

socket() -> is used to create a socket. (Associates with it the Protocol, type of data transmission it will use, etc)

bind() -> is then used to set up information for that socket (what port to open on your computer to wait for incoming client connections)

listen() -> gets the socket ready to accept connections

accept() -> is called in a loop until a client calls connect() with your I.P. and same right port number you''ve specified in bind()



WINSOCK CLIENT -> socket() -> Address Resolution -> connect()


socket() - as described above

Address Resolution -> Steps involved in setting up a SOCKADDR adress structure.. For TCP/IP, this is the server''s IP address and port number its application is listening on

connect() - Initiate connection with server

Now if you''re connected to the server- then you can get information from that server (such as who else is connected to it)... if you then type a message to someone- that message should be sent through the server first which is then passed onto the destined client.

DigitalChaos
DigitalChaos
Awesome, thanx man. I do believe I understand (I never knew network programming could be so complicated), and I may not yet fully understand as I go along. So basically I can get a list of whoever's connected to the server correct? And then choose to send that person a message through the port that the socket that their client is listening on. Makes sense. I'll work on some code then post it probably in the next few days and we'll see what I come up with. Any more input from anyone is definitely welcomed, as I need every bit of information I can get and that will be explained to me like I'm a three year old

[edited by - DigitalChaos on January 5, 2004 10:45:56 AM]
nohbdy
nohbdy
Easiest for your program would be just having flat files. Databases are great for searching billions of records, transaction processing, etc.. but they''re certainly no panacea and aren''t the best solution in a lot of cases. This is especially true when you''re inexperienced with coding for them, as most APIs tend to lead to fairly ugly code, and when the project isn''t going to have to support a million people.

A simple binary file format to hold each user''s information can be extremely easy to read and modify (simple file I/O - the stuff they teach in high school CS classes). File names within a single folder must be unique which can be used for unique usernames (like AIM), or using unique user IDs and allow for people to have the same username (like ICQ). Implementing a buddy list is a matter of stuffing an array of BuddyIDs into a file (and you''d likely want to have a list of people who have you on their buddy list too, for easy online notification). It''ll end up being quite a bit easier than having MySQL do a "SELECT * FROM BuddyTable WHERE BuddyTable.User = " and then parsing the ResultSet or whatever your API gives you. There''s a good chance it''ll be a lot faster too unless you have a miserably slow hard drive (in which case you''ll probably have a miserably slow CPU/memory and bad database performance anyway) or 1000s of people logging off and on every second. And if you get that many people logging on/off, charge money and get a better computer.. and some ultra fast hard drives in a RAID configuration.

-nohbdy
DigitalChaos
DigitalChaos
Flat files. Wow. I never thought of that. So many options. And to be honest, flat files sounds the easiest and quickest. How hard would it be to access the files on a server? Of course they would have to be encrypted and everything....you got any more details on this???? I like exploring my options.
dathui
dathui
you should look into nonblocking sockets, it would look good if your whole app froze everytime you called recv() would it?

theres a couple of options concerning nonblocking sockets.
fnctl() in linux or ioctlsocket() in windows to make it nonblocking and then using select() to check whether you can read from the socket is one method. i know winsock got some ready classes so you don't have to bother so much, ASyncSocket or what they've called. Personaly i prefer the -ctl()+select() method.

edit:
btw...imho you should use tcp between users and udp for the server.

[edited by - dathui on January 5, 2004 11:22:49 AM]
reality is only an option

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.