![]() |
| ||
| P2P based chat Hi, i need some help please on creating a chat app in VS2005. this can be very plain its liker a one to one chat app via internet. this is my code i have thus far pls tell if im on the right track: oh and i wrote this so you chat with yourself first this is only temporarily tho and then i need to figure it out how to get it working for two people lol using System; |
| ||
| Re: P2P based chat Quote:
If you just mean two people then all you have to do is simply repeat what you have already done. Create a new project and begin making the client, use TcpClient, the first parameter is the IP Address of the server, and the second parameter is the port that the server is running on. I have recently finished creating a multiplayer noughts and crosses game that runs over a network using TcpListener and TcpClient objects. The thing what puzzled me was this (even though I have not read through your code throughly so I am too sure). Why have you created a new TcpClient object in the btSend_Click() method: - objClient = new TcpClient(strFriend, 1002); If this is the server application your coding you should only need one TcpClient object per client, this is the TcpClient object returned from the AcceptTcpClient() method. Likewise if your coding the client you should only need one TcpClient object to connect to the server, this can be created straight by using the TcpClient class etc, there is no need to return the object from the AcceptTcpClient() method, if you get what I mean. Basically the server has one extra process and that is using the TcpListener, after that has been coded, the code for the server is pretty much the same as the client because you have your TcpClient objects. Its just the case that when your coding the server you get the TcpClient object from the AcceptTcpClient() method, and when your coding the client you get it straight from creating a TcpClient object "new TcpClient()" etc. |
| ||
| Re: P2P based chat Hi there Barnz, Thanks for the reply and help i will look into detail of everything you said thanks alot. I have just a few points that i would like to mention and please correct me if im wrong. i mean exactly two people thats it only a private chat app for my friend that stays in a different country and myself. Actually i am creating here the client side and the "server side". i dont have a seperate server app. as you can see i have the tcplistener coded in here aswell, this app needs to be only direct communication, i dont know if this is possible though. the reason for creating a new tcpclient object is: The first parameter of the constructor is the IP address of my friend im going to talk to. In this case, im talking to myself, so i use the standard IP address that points to my local computer. so for everytime me or my friend sends a message its creates a new tcpclient for the message to send to the specified ip and port. hope thats makes sense lol, anyway im kinda stuck but i will see what else i can do. |
| ||
| Re: P2P based chat Quote:
Just use your: - private TcpClient clientobj; Which you have done in your class, but I mean literally use it, there should only be one. Alright also, do you know that the TcpListener is only used for listening to connections from clients, not actually listening for chat messages etc? You use your streams for sending and recieving data like chat messages etc. Here are some examples: - SERVER PROGRAM TcpListener server = new TcpListener(6060); The TcpListener listens for incoming connections from clients, sometimes a first parameter may be needed, this is the local address and NOT the clients address. So sometimes it maybe like this: - TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 5050); Next use the start() method to start listening for incoming connections: - server.Start(); Next use the AcceptTcpClient() method which will return a TcpClient object when a client connects: - TcpClient clientobj = server.AcceptTcpClient(); Right ok, thats a solid connection now, between two TcpClient objects because the client will have been accepted. Basically from here on in, the client and the server are pretty much the same, you will have TcpClient object, and so will the client his / her self. To send and recieve data create some streams: - Network stream = clientobj.GetStream(); StreamReader input = new StreamReader(stream); StreamWriter output = new StreamWriter(stream); Then you can do, ReadLine(); and WriteLine(); etc: - input.ReadLine(); // Reads data from the client. output.WriteLine(); // Sends data to the client. CLIENT PROGRAM The client program is the practically the same as the server, accept instead of using a TcpListener to listen for incoming connections (because there is no need only the server does this) it uses TcpClient directly like so: - TcpClient clientobj = new TcpClient("255.255.255.FakeIp", 5050); The first parameter in the TcpClient constructor is the servers ip followed by the port the server is running on, which is the port you set in the TcpListener constructor in the "server program". Next create the streams, like was done on the server: - Network stream = clientobj.GetStream(); StreamReader input = new StreamReader(stream); StreamWriter output = new StreamWriter(stream); Then you can do, ReadLine(); and WriteLine(); etc: - input.ReadLine(); // Reads data from the server. output.WriteLine(); // Sends data to the server. Hope this helps, basically you should have 1 TcpClient object per program in your case, because none of programs need to accept connections from multiple clients. A pair of TcpClient objects is like a solid connection between client and server etc. One of the programs must act as a server, whether its the same or not, one has to make the connection to the other (client) and one has to recieve a connection from another (server). |
| ||
| Re: P2P based chat In your code I noticed you have strFriend, put into the TcpListener. The TcpListener should have the local address not the remote address. |
| ||
| Re: P2P based chat Ok I took the liberty of whipping up a quick chat console application, its very simple but it works. I have not added any of the try catch things in either. ChatApp.cs This is the main program using System; Person.cs This is a class that is part of the ChatApp etc, "Add Item" class file etc. using System; In this example the server surports one connection and that is from the client. If you want it to support multiple clients then put the TcpListener into an infinite loop in another thread etc. |
| ||
| Re: P2P based chat Hi there and thank you so much for your help. I have been playing around with your console app and am trying to write it as a windows application. im sitting with onw problem though which is that the form doesnt show when i start as the server. the debugging stops at the line TcpClient clientobj = server.AcceptTcpClient(); now i know this happens because because my ap is waiting for incoming clients. but how can i like idle my app while it accepts a client and be able to see the form and then just wait for an incoming client? Im sorry i must sound like such a newbie its just im struggling to get this part of programming :-0 i have learned alot though thanks this is my code so far for my windows app and i dont care bout the rest yet its just what happoens when i start the server which i have problems with. private void Form1_Load(object sender, EventArgs e) |
| ||
| Re: P2P based chat What form is not showing? Do you have one form, with a textbox for IP, a textbox for server messages etc, and a button that starts the server. I do not understand what you mean? The form should already be showing. How many forms do you have? |
| ||
| Re: P2P based chat Quote:
|
| All times are GMT -4. The time now is 4:24 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC