Thread: Can't get to connect clients to server

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Unhappy Can't get to connect clients to server

    Hi, I'm trying to build a media server where 4 clients can connect to it simulateously. Each client can choose a video channel and control its playback. However, the following code does not seems to connect/work. The error is " cannot convert from 'WindowsFormsApplication1.Client' to 'System.Net.Sockets.Socket' ".
    Other than that, I cant get to use the BinaryWriter in my code.



    Code:
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private volatile int channelNo;
            private TcpListener server;
            private List<Client> client = new List<Client>();
          private Thread serverThread;
            private Thread listenThread;
    
            private NetworkStream buffer;
            private ManualResetEvent stopThread = new ManualResetEvent(false);
           private BinaryWriter writer;
          private NetworkStream socketStream;
            private List<AxWMPLib.AxWindowsMediaPlayer> mediaPlayers = new  List<AxWMPLib.AxWindowsMediaPlayer>();
    
            public Form1()
            {
                try
                {
                    InitializeComponent();
    
                    mediaPlayers.Add(axWindowsMediaPlayer1);
                    mediaPlayers.Add(axWindowsMediaPlayer2);
                    mediaPlayers.Add(axWindowsMediaPlayer3);
                    mediaPlayers.Add(axWindowsMediaPlayer4);
                   
    
                    server = new TcpListener(IPAddress.Parse("127.0.0.1"), 3811);
                    server.Start();
    
                    listenThread = new Thread(new ParameterizedThreadStart(listenServer));
                    listenThread.Start();
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
    
            private ManualResetEvent quitEvent = new ManualResetEvent(false);
    
    
    
            public void listenServer(object obj)
            {
    
                while (quitEvent.WaitOne(1000, false))
                {
                    try
                    {
                        if (server.Server.Available > 0)
                        {
                            Client localClient = new Client(server.AcceptSocket());
                            client.Add(localClient);
    
    
                            NetworkStream buffer = localClient.GetStream(); // get NetworkStream associated with Tcpclient
                             NetworkStream serverSockStream = new NetworkStream(localClient);
    
                           socketStream = new NetworkStream(localClient);
                               writer = new BinaryWriter(socketStream);
    
                            serverThread = new Thread(new ParameterizedThreadStart(startServer));
                            serverThread.Start(localClient);
                        }
                    }
    
                    catch (Exception err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
    
            }
    
            private void startServer(object obj)
            {
                
                try
                {
                    Client client = obj as Client;
                    Debug.Assert(client != null);
    
                    string[] cueNo = new string[5];
                    int countNo = 0;
                    int videoPlayingNo = 0;
                    bool isEjectClick = true;
    
                    
                    while (!stopThread.WaitOne(0, true))
                    {
                        int length = client.ClientSock.Available;
    
                        if (length > 0)
                        {
                            byte[] receivedCmd = new byte[length];
                            client.ClientSock.Receive(receivedCmd);
                            string cmdReceived = Encoding.ASCII.GetString(receivedCmd);
    
                            if (cmdReceived == "CRAT00042011\n")// to connect to channel 1
                            //imagine you have the crat command here
                            client.Channel = 0;
                            if (cmdReceived == "CRAT00042012\n")
                                client.Channel = 1;
                            if (cmdReceived == "CRAT00042013\n")
                                client.Channel = 2;
                            if (cmdReceived == "CRAT00042014\n")
                                client.Channel = 3;
                        }
                    }
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
            protected override void OnFormClosed(FormClosedEventArgs e)
            {
                base.OnFormClosed(e); 
                
                quitEvent.Set();
                client = null;
                server.Stop();
               writer.Close();
                stopThread.Set();
    
            }
              
        }
    }
    Code:
    {
        public class Client
        {
            private Socket clientSock;
            
    
          public Socket ClientSock
            {
                get { return clientSock; }
            }
    
            /// <summary>
            /// make sure channel is between 0 and 3
            /// </summary>
            private int channel=0;
    
            public int Channel
            {
                get { return channel; }
                set 
                { 
                    channel = value;
                    Debug.Assert(channel >= 0 && channel <= 3, "Channel out of range");
                }
            }
    
         public Client(Socket sock)
            {
                clientSock = sock;
            }
        }
    }
    Can anyone help? Any help will be much appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    NetworkStream expects a socket, but your object "Client" isn't a socket, although it does contain one. Try this:

    Code:
    NetworkStream serverSockStream = new NetworkStream(localClient.ClientSock);
    socketStream = new NetworkStream(localClient.ClientSock);
    And as for:

    Code:
    NetworkStream buffer = localClient.GetStream();
    ...I don't even see a GetStream method declared in your Client object.

    One other thing, using:

    Code:
    server = new TcpListener(IPAddress.Parse("127.0.0.1"), 3811);
    ...is fine if you want your clients to connect from the same computer as the server, but if you want users from over the internet to connect then you're better off using:

    Code:
    server = new TcpListener(IPAddress.Any, 3811);

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thanks a lot theoobe, and thanks for the trouble.

    They can establish connection now.

    However I still couldnt get the data send by the client from the server side.
    Is there something wrong with my code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Communicating clients that are connected to the same server
    By seforix in forum Networking/Device Communication
    Replies: 6
    Last Post: 05-29-2009, 08:13 PM
  2. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-11-2008, 11:04 PM
  3. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 10:34 AM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM