Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Network Sockets
I have two clients (A, B) and 1 Server (C).How do A or B continuously connected to C.When the first request to the server, no need to connect to that still ongoing and communicate with Server.C order to understand what is A or B will respond and client connection is terminated when the client closes the connection and server is still running normally.
 
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Windows.Forms;
namespace example
{
// Server A
    class ExampleS
    {
        private IPEndPoint _ipEndPoint;
        private Socket _socket;
        private Socket _aSocket;
        private NetworkStream _network;
        private StreamReader _streamReader;
        private StreamWriter _streamWriter;
        void StartServer()
        {
            _ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2009);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Bind(_ipEndPoint);
            _socket.Listen(1000000000);
 
            while (true)
            {
                _aSocket = _socket.Accept();
                _network = new NetworkStream(_aSocket);
                _streamReader = new StreamReader(_network);
                _streamWriter = new StreamWriter(_network);
                if (_socket.Connected) continue;
                string _messenger = _streamReader.ReadLine();
                if (_messenger.Equals("X1"))
                {
                    _streamWriter.WriteLine("Hello");
                    _streamWriter.Flush();
                }
                else
                {
                    _streamWriter.WriteLine("Goodbye");
                    _streamWriter.Flush();
                }
            }
        }
    }
 
// Client B
    class ExampleCB
    {
        private IPEndPoint _ipEndPoint;
        private Socket _socket;
        private NetworkStream _network;
        private StreamWriter _streamWriter;
        private StreamReader _streamReader;
        void StartClient()
        {
            _ipEndPoint = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0],2009);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Connect(_ipEndPoint);
            if (!_socket.Connected)
            {
                 return;
            }
            _network = new NetworkStream(_socket);
            _streamWriter = new StreamWriter(_network);
            _streamReader = new StreamReader(_network);
            _streamWriter.WriteLine("X1");
 
            _streamWriter.Flush();
            var readLine = _streamReader.ReadLine();
            if (readLine == null)
            {
                MessageBox.Show("Error messenger");
                return;
            }
            else
            {
                MessageBox.Show(readLine.ToString());
            }
            _streamWriter.Close();
            _streamReader.Close();
            _network.Close();
        }
    }
// Client C
 class ExampleCC
    {
        private IPEndPoint _ipEndPoint;
        private Socket _socket;
        private NetworkStream _network;
        private StreamWriter _streamWriter;
        private StreamReader _streamReader;
        void StartClient()
        {
            _ipEndPoint = new IPEndPoint(Dns.Resolve(Dns.GetHostName()).AddressList[0], 2009);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Connect(_ipEndPoint);
            if (!_socket.Connected)
            {
                 return;
            }
            _network = new NetworkStream(_socket);
            _streamWriter = new StreamWriter(_network);
            _streamReader = new StreamReader(_network);
            _streamWriter.WriteLine("X1");
 
            _streamWriter.Flush();
            var readLine = _streamReader.ReadLine();
            if (readLine == null)
            {
                MessageBox.Show("Error messenger");
                return;
            }
            else
            {
                MessageBox.Show(readLine.ToString());
            }
            _streamWriter.Close();
            _streamReader.Close();
            _network.Close();
        }
    }
}
Every time a request to the server, I have to reconnect.Now how without creating connections that retains the session with the server and the server responds to the correct client is sending requests.
----
Image Example Here[^]">
Posted 18-Mar-13 19:37pm
Edited 19-Mar-13 21:52pm
v6
Comments
Nasir M@hmood at 19-Mar-13 0:47am
   
If you are using TCP then connection remain open until you intentionally close socket from either side, kindly clear out what you want to do.
Sergey Alexandrovich Kryukov at 19-Mar-13 9:43am
   
Hm. Where are your communication threads? Do you think you can use just one?
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100