Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, the client can only connect to the port. Then, the Server may respond back to the client's connection </p> <p><img src="https://i.stack.imgur.com/uKIb7.png" alt="Client-Server Request and its response"></p> <p><strong>Example</strong></p> <p><sup><strong>Client</strong></sup></p> <pre><code>IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999); Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); try { server.Connect(ip); //Connect to the server } catch (SocketException e){ Console.WriteLine("Unable to connect to server."); return; } Console.WriteLine("Type 'exit' to exit."); while(true) { string input = Console.ReadLine(); if (input == "exit") break; server.Send(Encoding.ASCII.GetBytes(input)); //Encode from user's input, send the data byte[] data = new byte[1024]; int receivedDataLength = server.Receive(data); //Wait for the data string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); //Decode the data received Console.WriteLine(stringData); //Write the data on the screen } server.Shutdown(SocketShutdown.Both); server.Close(); </code></pre> <p>This will allow the client to send data to the server. Then, wait for the response from the server. However, if the server does not respond back the client will hang on for much time.</p> <p><strong>Here's an example from the server</strong></p> <pre><code>IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999); //Any IPAddress that connects to the server on any port Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); //Initialize a new Socket socket.Bind(ip); //Bind to the client's IP socket.Listen(10); //Listen for maximum 10 connections Console.WriteLine("Waiting for a client..."); Socket client = socket.Accept(); IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}",clientep.Address, clientep.Port); string welcome = "Welcome"; //This is the data we we'll respond with byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes(welcome); //Encode the data client.Send(data, data.Length,SocketFlags.None); //Send the data to the client Console.WriteLine("Disconnected from {0}",clientep.Address); client.Close(); //Close Client socket.Close(); //Close socket </code></pre> <p>This will allow the server to send a response back to the client upon the client's connection.</p> <p>Thanks, <br>I hope you find this helpful :)</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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