Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCP/IP chat program...

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Greetings to all of you!!

I'm thinking of building a simple TCP/IP chat program! The main funtion is to send instant messages to another computer(real time), anywhre on the net by using the computers IP!
I have never before done any network programs!!

Does anybody know where I should start? What codes are needed and which components I should use (if any at all)!!
I have availability to use BCB5 enterprise, at school!!

Thanks in advance,
Martin!!


P.S. Please dont refer me to BCB projects, I find them hard to break down, and I like to do everything from scratch!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
Look at the examples, there is a very good example of a chat program, it is very straightforward and you can learn a lot from it Wim Vanherp
Wim.Vanherp@myself.com
 
Thanks for your respond!

I've been looking into both hints I recived.
I'm quite sure that reading a book on the subject would help me allot. The question is only which book(s) too read! Does anyone know which one is tht best? And where to buy it!

Thanks again,
Martin! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
I, somehow, manage to create a small chat program. And it worked fine. I started two applications (1-server,1-client) when I connected to the server (ServerSocket had not been set to active yet) I got a message to my client application.. It read "This server is owned by Xaner Software by G6 FTP Server ready ...", this is the very same text I have from my FTP server I've set up at school..
I changed the port number and then didn't get it!!

How do I prevent this in the future? I mean, connecting to the server without sending messages to the FTP!! Or Is there anyway for the server application to see if the port is in use?

Thanks,
Martin!! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
i made my own chat program a few weeks back, and i cant get the ip addresses to work. i have some straightforeward code that isnt very hard to understand if youd like it. it REALLY helped me in creating my program. if youd like it, e-mail me.

thats about the only advice i can give you...sorry!
 
If you work with sockets, ports and ip-connections you need a basic understanding of ip-networking. The ip-adress is the address of the computer you will send the packet to, the port-nr is the way to indicate for wich application the information will serve. A number of ports are pre-defined and descripted as "well known ports". f.i. 21 is the port for ftp-connections, 80 for http 25 for smtp. If you want to use a port choose a portnumber behind 1024. For the rest it is (in Builder) fairly simple. Put a clientocket on your form, fill in the ip-adress of the computer you want to send to, choose a port, try to make a connection and if the connection is made send the packet.
Put a serversocket on your form and set it to listen for a port. If the serversocket detects a packet for the port it is listening to it makes a connection and signals that to the client that the connection is established. The client socket gets an onconnection event and can start to send bits of information. The serversocket can also send back information to the clientsocket.

The clientsocket and serversocket are basically the same, but the clientsocket chooses the ip-adress of the destination and the port while the serversocket listens to a port.


A way that is even easier is to use UDP (there is also an example of it). UDP is a connectionless-protocol, you choose an ip-adress and a port and you send the packet, there is no feedback from the serverside that the packet is arrived and so there is not much need for error-handling. This protocol is heavily used for internetradio, if a packet is lost, there will be no errormessage and no retransfer of the packet.
In TCP there is always a signal back from the server to indicate that a packet is arrived.
You can experiment with this easily on your own computer, use your own ip-adress (if you have a network card) or use the ip-adress 127.0.0.0. This is always the loopback adress that refers to your own computer. So you can launch two sets of the ip-chat program (or the udp-program) and chat to yourself.

Wim Vanherp
Wim.Vanherp@myself.com
 
Thanks wimvanherp!

Though I've already created a small chat program that works (and is farily stable) your respone made the code somewhat clearer, what it actualy did!! I got most of the codes from the prewritten example files in BCB5. However, these were merly a start so I new what I needed and a simple way how to make it work.

I will take your advice under notice and check out the UDP protocol

One other thing, if anyone knows anything about it.
In the program I wrote I used a TClientSocket and a TServerSocket, and application runs as server the rest as clients... There where no limit to how many people that could connect to the TServerSocket, it also keept track of them under ServerSocket1->Socket->Connections[x] , how do I find out which connected user has a certain IP number?
And how can I make the client, upon connecting to transmit an AnsiString to the server which will work as an ID (or password), and if the ID/password is incorrect the server will deny client connection?
Each user will have his / her own ID / password which the server must recieve before any connection will be initiated.
I could not find any function / property that tells the TClientSocket to send a string. So I figure that I most likely will have to send the ID / password after the connection as been established and then the server can kick him out. Is this true?

Thanks for all help so far, and all you can give in the future! Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
Xaner Software, DB programer
 
use NMMsg->RemoteIP to find out the remote users IP address. about your sending a message:
in the OnConnect event handler for the client socket, have it popup a QueryBox asking for the users Password, when they finish typing the password, send a embedded message to the server with some sort of keyword(Client:passwordComing@) stating that the next word after the @ will be the password, for example:

in the OnConnect event for the client:

AnsiString ThisPassword;

if (InputQuery(" please type password", "password", ThisPassword) != NULL)
{

ClientSocket1->Socket->SendText
("Client[myid]::passwordComing@" + ThisPassword);

}

and on the servers side, in the OnClientRead:

AnsiString Message;
AnsiString ClientPassword;
char * ourvariable;
char * tst

Message = ServerSocket1->Socket->RecieveText();

if(tst = strstr(ourvariable, "Client[mynumber]::passwordComing@"))
{
AnsiString Pass;
if(tst = strchr(ourvariable, '@'))
{
Pass = (++tst);
ClientPassword = Pass;
}
}

that was just off the top of my head. if it comes out with errors, let me know, and ill write up some real code for you. Cyprus
 
Someone said in this thread something about UDP connection. Could you tell me, guys, how could I use this protocol in a chat program? Which methods/components should I use? I'd be gratefull if you could.
 
There is a good component to buy from It is ZSocket, I have not used it however I have used ZComm and found it to be very well done.

You can download it and use it for free, however to distrtibute it you must buy it (however it is cheap)
 
i'm working on the same project as well.
i use NMMsg and NMMsgServ components .you can find them in "fastnet" section on component platte.
NMMsg is for sending a text msg to remote computer.
NMMsgSrev is for recieving messages sent with NMMsg.
you can use bcb5 help if you don't understand the following (just choose "find" and type "NMMsgSrev");

create a new form. put the 2 components NMMsgSrev,NMMsg.
put 2 edits and a memo on the form.
configure the host parameter on NMMsg to 127.0.0.1
that is your own ip address.
now you have to configure events:

on NMMsgSrev find the "onmsg" event .when a message is recieved you want to get the message :
the function gets 2 important parameters:sFrom-the name of the sender.sMsg-the message.

**********************************************************
void __fastcall TForm1::NMMSGServ1MSG(TComponent *Sender,
const AnsiString sFrom, const AnsiString sMsg)
{

Memo1->Lines->Add("["+sFrom+"] says: "+sMsg);
NMMsg1->Host= NMMSGServ1->RemoteIP;

}
**********************************************************
the last line on the code is to update the remote ip of the sender.

you have 2 edits on your form.edit1 is sender name.
edit2 is the message.now lets see how you send a message.
you can put this code on edit1 "onkeypress" event.

***********************************************************
if (Key == 0x0D)//after pressing "enter" message is sent

{
NMMsg1->FromName = Edit1->Text;//sender name
NMMsg1->PostIt(Edit2->Text); //the message
}
***********************************************************

if you did every right you should see you message on the memo.
if you want to send to another computer just change the host parameter on NMMsg1 to the remote computer's ip address.note:both components are configured to port 6711.this is a free port.do not change it unless you what you are doing.ports 6703-6713 are free to use.
both components must have the same port!!!

this was written on bcb5.

 
TCP/IP Sockets in C
Practical guide for programmers

very good book.

please look in to the properties of the socket objects you are using and study the methods. all the answers are in there. Look at my chat program on my web site. if you are interested I can send you the whole project.

time will answer all questions.

tomcruz.net
 
howdy fellas,

I was checking out the thread, and was thinking of trying it out, but was wondering something.

Are there any special considerations necessary for security? i.e. how do i know some third party couldn't use the chat communications to gain access to a user's computer.

I just didn't want to create an "open window" per say to allow someone to crawl through and destroy my machine...

Thanks for any responses,
onrdbandit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top