CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: multiclient

  1. #1
    Join Date
    Oct 2007
    Posts
    84

    multiclient

    hi,
    i have a chat app and it runs.
    but i notice that i made a single client only to send message to the host
    how can i make my chat a multiclient receiver.
    is it the host or the client that i have to change.

    please advice
    thanks

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: multiclient

    Depends, possibly both. How should we know what you have to change when we don't know anything about your application?

    Well, I suppose you want your chat application to allow clients to talk to all users or to a particular user. Your server should be capable of accepting connections from multiple clients. The clients should be notified when someone logs in or out and display a list of available chat users. The server should be able to redirect the received messages to all users or only to one (or a group).

    But first thing is that you define what you want for your application, and then you can focus on how to do it.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2007
    Posts
    84

    Re: multiclient

    my script is very simple,
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    
    namespace printerManager.V._1._0
    {
        public partial class SocketServerForm : Form
        {
            public AsyncCallback pfnWorkerCallBack;
            public Socket m_socListener;
            public Socket m_socWorker;
    
            public SocketServerForm()
            {
                InitializeComponent();
            }
    
            
            private void cmdListen_Click_1(object sender, EventArgs e)
            {
                try
                {
                    //create the listening socket...
                    m_socListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 8221);
                    //bind to local IP Address...
                    m_socListener.Bind(ipLocal);
                    //start listening...
                    m_socListener.Listen(4);
                    // create the call back for any client connections...
                    m_socListener.BeginAccept(new AsyncCallback(OnClientConnect), null);
                    cmdListen.Enabled = false;
    
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
            }
            public void OnClientConnect(IAsyncResult asyn)
            {
                try
                {
                    m_socWorker = m_socListener.EndAccept(asyn);
    
                    WaitForData(m_socWorker);
                }
                catch (ObjectDisposedException)
                {
                    System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
    
            }
            public class CSocketPacket
            {
                public System.Net.Sockets.Socket thisSocket;
                public byte[] dataBuffer = new byte[1];
            }
            public void WaitForData(System.Net.Sockets.Socket soc)
            {
                try
                {
                    if (pfnWorkerCallBack == null)
                    {
                        pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                    }
                    CSocketPacket theSocPkt = new CSocketPacket();
                    theSocPkt.thisSocket = soc;
                    // now start to listen for any data...
                    soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
    
            }
            delegate void FixCallBack(IAsyncResult asyn);
            public void OnDataReceived(IAsyncResult asyn)
            {
                try
                {
                    if (this.InvokeRequired)
                    {
                        FixCallBack dd = new FixCallBack(OnDataReceived);
                        this.Invoke(dd, new object[] { asyn });
                    }
                    else
                    {
                        CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
                        //end receive...
                        int iRx = 0;
                        string mess;
                        iRx = theSockId.thisSocket.EndReceive(asyn);
                        char[] chars = new char[iRx + 1];
                        System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                        int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
                        System.String szData = new System.String(chars);
    
                        mess = txtDataRx.Text + szData;
    
                        txtDataRx.Text = mess;
                        WaitForData(m_socWorker);
                    }
                }
                catch (ObjectDisposedException)
                {
                    System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    Object objData = txtDataTx.Text;
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                    m_socWorker.Send(byData);
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message);
                }
            }
    
            private void txtDataRx_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            
        }
    }
    its the server script

    what part of the script should i change?

    tia
    Last edited by homer.favenir; February 3rd, 2010 at 09:30 PM.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: multiclient

    You're not paying attention. How can we figure what parts of the scripts should be modified when you didn't explain how you expect your application to work?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2007
    Posts
    84

    Re: multiclient

    i would like the server accepts multiclient, as of now it is accepting single client only...

    thanks

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: multiclient

    Quote Originally Posted by homer.favenir View Post
    my script is very simple,

    its the server script

    what part of the script should i change?

    tia
    It would be better if you edit that post and use the [code] tag instead of [quote].

  7. #7
    Join Date
    Oct 2007
    Posts
    84

    Re: multiclient

    hi,
    i already changed it

    tia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured