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; using System.Windows.Forms; using System.Drawing; using System.Net.Sockets; using System.Threading; using System.IO; using System.ComponentModel; using System.Net; using System.Drawing.Drawing2D; namespace MyTalk { public partial class Chat : DevComponents.DotNetBar.Office2007Form { private TcpClient objClient; private Thread thdListener; private TcpListener objListener; private string strFriend; private string strMe; IPAddress ipadd; public delegate void Invoker(String t); public Chat() { strFriend = "10.0.1.166"; ipadd = Dns.GetHostEntry(strFriend).AddressList[0]; strMe = "Me"; thdListener = new Thread(new ThreadStart(this.Listen)); thdListener.Start(); InitializeComponent(); } private void Listen() { string strTemp = ""; objListener = new TcpListener(ipadd,1000); objListener.Start(); do { TcpClient objClient = objListener.AcceptTcpClient(); StreamReader objReader = new StreamReader(objClient.GetStream()); while (objReader.Peek() != -1) { strTemp += Convert.ToChar(objReader.Read()).ToString(); } object[] objParams = new object[] { strTemp }; strTemp = ""; this.Invoke(new Invoker(this.ShowMessage), objParams); } while (true != false); } private void ShowMessage(String t) { rtbMessage.Text += strFriend + ": " + t + "\n"; } private void btExit_Click(object sender, EventArgs e) { objListener.Stop(); Application.Exit(); Environment.Exit(0); } private void btSend_Click(object sender, EventArgs e) { rtbMessage.Text += strMe + ": " + rtbType.Text + "\n"; objClient = new TcpClient(strFriend, 1002); StreamWriter w = new StreamWriter(objClient.GetStream()); w.Write(rtbType.Text + "\n"); w.Flush(); objClient.Close(); rtbType.Text = ""; }
When you say two people, do you mean exactly two people? or multiple people?
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.