Note that there are some explanatory texts on larger screens.

plurals
  1. POManage several/multiple tcp connections
    primarykey
    data
    text
    <p>I have a server application and client application with the functionality already working. Let me show you how I connect my client application to my server app:</p> <pre><code> //SERVER // instantiate variables such as tempIp, port etc... // ... // ... server = new TcpListener(tempIp, port); //tempIp is the ip address of the server. // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[MaxChunkSize]; String data = null; // Enter the listening loop. while (disconect == false) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. client = server.AcceptTcpClient(); // wait until a client get's connected... Console.WriteLine("Connected!"); // Get a stream object for reading and writing stream = client.GetStream(); // now that the connection is established start listening though data // sent through the stream.. int i; try { // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // etc.. .... </code></pre> <p>ok now on the client side lets say I want to establish a connection then send some data through the stream</p> <pre><code> //Client client = new TcpClient(serverIP, port); // Get a client stream for reading and writing. stream = client.GetStream(); //then if I wish to send the string hello world to the server I would do: sendString(stream, "Hello world"); protected void sendString(NetworkStream stream, string str) { sendBytes(stream, textToBytes(str)); } protected void sendBytes(NetworkStream stream, Byte[] data) { // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); } protected static Byte[] textToBytes(string text) { return System.Text.Encoding.ASCII.GetBytes(text); } </code></pre> <p>since I am able to send bytes I am able to send files or everything that I want. the technique that I use is that if I send the string file for example to the server then the server will start listening for a file. It will open a stream and write the received bytes to that file. If a different keyword is send the server will start listening on a different method etc..</p> <p>So when dealing with one server and one client everything works great. Now I want to add more clients and need them to also connect to the server. I know that several connections can be establish on the same port just like we do it with por 80 on websites... I do not know how to manage several connections. so one thing I was thinking was to leave everything as it is. if a connection is established then tell the server to start another thread listening for other connections on the same port. with this technique I will have several threads running plus I just know the basics of multrythreading. If this technique is my best option I will start implementing it. You guys out there are really knowledgeable about all this so it will be nice if someone can point me on the right direction. Or maybe I should listen on several ports. if the server is already connected on port 7777 for example then do not accept connections from that port and start listening on port 7778 for example. I mean there could be so many different ways of achieving what I need and you guys probably know the best way. I just know the basics of networking...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload