Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
AnswerRe: full text search Pin
JohnLBevan4-Nov-10 3:53
professionalJohnLBevan4-Nov-10 3:53 
AnswerRe: full text search Pin
Adam R Harris4-Nov-10 4:53
Adam R Harris4-Nov-10 4:53 
QuestionImpersonation using C# Pin
JohnLBevan4-Nov-10 1:45
professionalJohnLBevan4-Nov-10 1:45 
AnswerRe: Impersonation using C# Pin
Adam R Harris4-Nov-10 4:49
Adam R Harris4-Nov-10 4:49 
AnswerRe: Impersonation using C# Pin
Manfred Rudolf Bihy4-Nov-10 5:06
professionalManfred Rudolf Bihy4-Nov-10 5:06 
GeneralRe: Impersonation using C# Pin
JohnLBevan9-Nov-10 5:43
professionalJohnLBevan9-Nov-10 5:43 
GeneralRe: Impersonation using C# Pin
Manfred Rudolf Bihy12-Nov-10 2:07
professionalManfred Rudolf Bihy12-Nov-10 2:07 
QuestionServer based on AsyncCallback not running as expected Pin
Tichaona J4-Nov-10 1:08
Tichaona J4-Nov-10 1:08 
Hi All

I have a server that starts up as part of my application (when the application InitializeComponent()), as below:

<br />
  public Window1()<br />
        {<br />
            InitializeComponent();<br />
           <br />
<br />
               ...other code<br />
             //Start the server...<br />
            connectionAccept();<br />
<br />
             }<br />


This calls the below code:

<br />
 private void connectionAccept()<br />
        {<br />
            try<br />
            {<br />
                listener = new Socket(AddressFamily.InterNetwork,<br />
                                             SocketType.Stream,<br />
                                             ProtocolType.Tcp);<br />
<br />
                IPEndPoint LocalIP = new IPEndPoint(IPAddress.Any, 2112);<br />
<br />
                listener.Bind(LocalIP);<br />
<br />
                listener.Listen(0);<br />
<br />
                listener.BeginAccept(new AsyncCallback(OnClientConnect), null);<br />
<br />
            }//End of try<br />
<br />
            catch (SocketException se)<br />
            {<br />
                MessageBox.Show(se.Message);<br />
            }//End of catch<br />
<br />
        }// End of connectionAccept method<br />
<br />
        //On client call....<br />
        public void OnClientConnect(IAsyncResult asyn)<br />
        {<br />
            try<br />
            {<br />
                otherListener[numberOfClients] = listener.EndAccept(asyn);<br />
<br />
                WaitForData(otherListener[numberOfClients]);<br />
<br />
                ++numberOfClients;<br />
<br />
                String strData = Convert.ToString(numberOfClients);<br />
                MessageBox.Show(strData);<br />
<br />
                listener.BeginAccept(new AsyncCallback(OnClientConnect), null);<br />
            }<br />
            catch (ObjectDisposedException)<br />
            {<br />
                System.Diagnostics.Debugger.Log(0, "1", "\n OnClientConnection: Socket has been closed\n");<br />
            }//End of first catch<br />
<br />
            catch (SocketException se)<br />
            {<br />
                MessageBox.Show(se.Message);<br />
<br />
            }//End of second catch<br />
<br />
        }//End of OnClientConnect method<br />
<br />
        public class SocketPacket<br />
        {<br />
            public System.Net.Sockets.Socket currentSoc;<br />
            public byte[] dataBuffer = new byte[0];<br />
<br />
        }//End of Socket packet<br />
<br />
        public void WaitForData(System.Net.Sockets.Socket soc)<br />
        {<br />
            try<br />
            {<br />
<br />
                if (pfnListnerCallBack == null)<br />
                {<br />
                    pfnListnerCallBack = new AsyncCallback(OnDataReceived);<br />
<br />
<br />
                    SocketPacket socPkt = new SocketPacket();<br />
                    socPkt.currentSoc = soc;<br />
<br />
<br />
                    soc.BeginReceive(socPkt.dataBuffer, 0,<br />
                                          socPkt.dataBuffer.Length,<br />
                                           SocketFlags.None,<br />
                                           pfnListnerCallBack,<br />
                                           socPkt);<br />
<br />
<br />
                }//End of if statement<br />
<br />
            }//End of try<br />
<br />
            catch (SocketException se)<br />
            {<br />
                MessageBox.Show(se.Message);<br />
<br />
            }//End of catch<br />
<br />
        }//End of wait for data<br />
<br />
        public void OnDataReceived(IAsyncResult asyn)<br />
        {<br />
            try<br />
            {<br />
                SocketPacket socketData = (SocketPacket)asyn.AsyncState;<br />
                string receivedValue = string.Empty;<br />
<br />
                byte[] receivedBytes = new byte[1024];<br />
                int numBytes = socketData.currentSoc.Receive(receivedBytes);<br />
<br />
                receivedValue = Encoding.ASCII.GetString(receivedBytes);               <br />
               <br />
               MessageBox.Show(receivedValue);               <br />
                WaitForData(socketData.currentSoc);              <br />
<br />
            }//End of try<br />
<br />
            catch (ObjectDisposedException)<br />
            {<br />
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");<br />
<br />
            }//End of catch1<br />
<br />
            catch (SocketException se)<br />
            {<br />
                MessageBox.Show(se.Message);<br />
            }//End of catch2 <br />
<br />
        }//End of OnDataReceived method..<br />
        <br />
    }<br />
<br />


Ok so the server works, but it seems to only work once. When I send a string in bytes across to it, it shows you that one client has connected and the message recieved. Then if I try doing the same thing again, it only shows the another client has connected but does not show the second message MessageBox.Show(receivedValue);. I want it to always show the message from the client. Where am I going wrong???Confused | :confused: Oh by the way I got some of the code for the server from

Codeguru.com
AnswerRe: Server based on AsyncCallback not running as expected Pin
jschell4-Nov-10 8:32
jschell4-Nov-10 8:32 
QuestionGot a working ICMP listener; but how does it work? Pin
nebbukadnezzar4-Nov-10 0:28
nebbukadnezzar4-Nov-10 0:28 
QuestionRetrieve Value from TextBox Which is created in the code behind Pin
HatakeKaKaShi3-Nov-10 22:54
HatakeKaKaShi3-Nov-10 22:54 
AnswerRe: Retrieve Value from TextBox Which is created in the code behind Pin
John Gathogo4-Nov-10 1:00
John Gathogo4-Nov-10 1:00 
AnswerRe: Retrieve Value from TextBox Which is created in the code behind Pin
alrosan4-Nov-10 8:05
alrosan4-Nov-10 8:05 
Questionhow to find toolstrip button on a form by name Pin
Tridip Bhattacharjee3-Nov-10 21:27
professionalTridip Bhattacharjee3-Nov-10 21:27 
AnswerRe: how to find toolstrip button on a form by name [modified] Pin
Eddy Vluggen3-Nov-10 22:30
professionalEddy Vluggen3-Nov-10 22:30 
AnswerRe: how to find toolstrip button on a form by name Pin
_Erik_4-Nov-10 5:47
_Erik_4-Nov-10 5:47 
QuestionAdd Custom Utility Toolbar To VS2010 Pin
Kevin Marois3-Nov-10 12:52
professionalKevin Marois3-Nov-10 12:52 
AnswerRe: Add Custom Utility Toolbar To VS2010 Pin
Henry Minute3-Nov-10 12:56
Henry Minute3-Nov-10 12:56 
Questiondebugging mode behaves differently than final build Pin
kruegs353-Nov-10 6:02
kruegs353-Nov-10 6:02 
AnswerRe: debugging mode behaves differently than final build Pin
_Erik_3-Nov-10 6:14
_Erik_3-Nov-10 6:14 
GeneralRe: debugging mode behaves differently than final build Pin
kruegs353-Nov-10 6:41
kruegs353-Nov-10 6:41 
GeneralRe: debugging mode behaves differently than final build Pin
_Erik_3-Nov-10 6:49
_Erik_3-Nov-10 6:49 
AnswerRe: debugging mode behaves differently than final build Pin
Eddy Vluggen3-Nov-10 22:35
professionalEddy Vluggen3-Nov-10 22:35 
QuestionHow to set pixel into bitmap with Pixel format of Format8bppIndexed ? [modified] Pin
Yanshof3-Nov-10 4:14
Yanshof3-Nov-10 4:14 
AnswerRe: Hot to set pixel into bitmap with Pixel format of Format8bppIndexed ? Pin
Covean3-Nov-10 4:27
Covean3-Nov-10 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.