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.
Hmm, about the new TcpClient object for every message. You should not need to create a new one for everyone message, after it has been created you just use the streams to send and receive data. So after your intial TcpClient object you create there should be no need to create anymore.
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).