Community discussion forum

Socket Programming in C# - Part 2

This is a comment thread discussing Socket Programming in C# - Part 2
  • 9 years ago

    This thread is for discussions of Socket Programming in C# - Part 2.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 5 years ago

    Hi! I'm testing your code. If you launch several client applications, only the first connect with the server. Which is the problem? Perhaps you don't release socket server / client and the resource keep blocked.


    Grettings!

  • 5 years ago

    Firstly I would just like to say thanks for the great example code. Very helpful. I am now trying to have more than 1 client listen/communicate with the server. I am getting some very strange results though. Can anybody please give me some advice. I have altered the code on the server side to go back to "listening" after the first client connects. This works, but when the second client connects the first client gets kicked out (after a few seconds?) somehow.


    Look forward to any information.


    Thanks
    Jason

  • 5 years ago

    First, let me thank you for the great article - it has proved really valuable for me in my current project.




    I am currently trying to extend the example to enable transfer of XML-data (or any data). However, I am not sure how to manipulate the recieved data as a complete string to parse using another function.


    Your example revieces a byte at a time and appends this to the content of a text box. Your example uses an "iterative" function to recieve each byte and then calls WaitForData() to wait for the next byte. But to be able to manipulate this data I need to somehow collect it in a string and when the data/command from the client has been received - do something like sending a answer back.


    The setup could be:


    Client sends command: "GetAmountInAccount"
    Server recieves this, checks the  and replyes: "You are broke"


    but how do I detect when the entire command from the client has been received?


    Thanks,




  • 5 years ago

    hi .
    i want to ask if you can send me the program work ( multi client /server )
    can you send to me .


    my email : hamzahwh@yahoo.com

  • 5 years ago

    hi .
    i want to ask if you can send me the program work ( multi client /server )
    can you send to me .


    my email : hamzahwh@yahoo.com

  • 5 years ago

    hi .
    i want to ask if you can send me the program work ( multi client /server )
    can you send to me .


    my email : hamzahwh@yahoo.com

  • 5 years ago

    Did any of you get the program to work with multiple clients?


    If so, how did you do it?

  • 5 years ago

    I get an error when the client closes the connection, and then no new connections can be made. Does anybody have a solution


    Also. Did anybody get it to work with multiple clients?

  • 4 years ago

    If I have a system which has several local IP's  is there a way where I can designate which IP the connection is sent from?

  • 4 years ago

    I'm new to C#, and due to tome constaints on a project at work, had to learn as much as I could from books in order to test an SDK for a Pocket PC device.  Your sample and walk-through in the article were extremely helpful

  • 4 years ago

    everything looks fine. i liked the article except one bit. i ran the server and the client and on the client side, i believe it leaks memory. instead of taking the message from the server and putting it in the text box, i ignored it (to avoid the memory from the text stored). i opened the task manager and sent a bunch of messages to the client. the memory for the client kept increaing as long as i sent it more messages. any solutions to this?


    regards,


    tuco

  • 4 years ago

    Hi all,


    I am currently trying to use the client code to simultaneously send icmp packets to several IPs from different threads. i.e. I have the client code as a class and I instantiate a new instance for each different IP. Within the class I implement an asynchronous delegate to run the client code, so that in the mean time I can return to the main  thread and implement a new instance  of the class and send a different icmp packet.


    Thus at any one time I have several instances of the client code running on different threads, waiting for icmp packets to be received on the OnDataReceived callback.


    My problem is that I am finding icmp reply packets from one thread in the receive databuffer of another thread. i.e.
    the CSocketPacket  object returned as a reference in "IAsyncResult asyn" is not coherent. In other words the data in theSockId.dataBuffer does not correspond to the theSockId.thisSocket socket.


    Is this happening because this code is not threadsafe?? or does anyone have any idea of how I can resolve this problem??


    Regards
    Rekkie

  • 4 years ago

    Managed to figure out the problem with the help of Zane on the microsoft.public.dotnet.languages.csharp newsgroup. Basically it comes down to ping being a UDP packet that does not have a socket connection and so have to use .BeginReceiveFrom rather than BeginReceive.


    Ciao
    Rekcut

  • 4 years ago

    Hi,
    I can run the server application successfully with port 8221 but I get an error when I changed the port number to 80, 8100 or others.


    Following is the exception:
    Only one usage of each socket address (protocol/network address/port) is normally permitted.


    Does anyone have any idea of how I can resolve this problem??

  • 4 years ago

    I tried to running the server app and client app.  Worked great, when I only had one client.  If I run two clients and try to connect to the server I only see messages from the first client to connect.  In fact, if I run one client and connect, then disconnect, and then reconnect, I get no connection refused error, but I do not get any messages comming across to the server.  Is there some socket clean up that is not happening?  I like this scheme of using async sockets but if only one person can connect, well thats a big problem, please enlighten me.

  • 4 years ago

    Sorry I did not look at the complete response list and see that this question was already posted.

  • 4 years ago

    I guess that's because in the code, there is:


    IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);


  • 4 years ago

    Sorry ... where is a response ?? .. thanks

  • 4 years ago

    Not responses as answers, responses as people asking the same question that I asked.  This is a neat looking solution but it has a logicl problem and will only work with one client, that is not exceptable for an async socket.  Here is a good solution that I have used for an async sockets.
    http://www.theukwebdesigncompany.com/articles/asynchronous-socket-utility-classes.php

  • 4 years ago

    Can you help me with a telnet connection coding problem? My code can connect and Send, but Receive cannot get the replied messsage.


    My client is a Windows machine. From this I create a socket connection to an IP and a port on a server machine. On the client machine I can manually telnet to the server, Send/Receive message like this:
    Send: telnet IP Port
    Receive: Connected to IP...
    Send: Operation=TotalRecords
    Receive: TotalRecords=1000


    The fact that I can connect/Send/Receive suggests there is no access issue (the server is a linux machine).


    Now here is my socket code. I can Connect and Send ("Operation=TotalRecords"). But Receive keep on waiting but never getting the message "TotalRecords=1000".


    How do I code to receive the reply? Here is my code in C#:


    IPAddress remoteIPAddress = IPAddress.Parse(LinuxIPAddress);
    EndPoint ep = new IPEndPoint(remoteIPAddress, 4321);
    Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    string query="Operation=TotalRecords";
    sock.Connect(ep);
    Encoding ASCII = Encoding.ASCII;
    Byte[] ByteGet = ASCII.GetBytes(query);
    Byte[] RecvBytes = new Byte[256];
    int iTx=sock.Send(ByteGet, ByteGet.Length, 0); //the code worked to this point
    Int32 bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0); //this will wait forever...


    Thank you in advance.


    Ken


  • 4 years ago

    I modified a part of the Form1:
    /* declare a public variable */
    // Recording "cmdListen" button is "Start Listening" or "Stop Listening" so far.
    private bool isListening = false;


    private void cmdListenClick(object sender, System.EventArgs e) {
       try {
           if ( isListening ) {
               /* closing m
    socListener /
               m_socListener.Close();
               cmdListen.Text = "Start Listening";
               btnSend.Enabled = false;
               isListening = false;
           } else {
               /
    constructing msocListener */
               //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...
               msocListener.Bind( ipLocal );
               //start listening...
               m
    socListener.Listen(4);
               // create the call back for any client connections...
               m_socListener.BeginAccept(new AsyncCallback ( OnClientConnect ),null);
               cmdListen.Text = "Stop Listening";
               btnSend.Enabled = true;
               isListening = true;
           }
       } catch(SocketException se) {
           MessageBox.Show ( se.Message );
       }
    }

  • 4 years ago

    Hi!
    is it possible that the server manage more accesses in parallel?


    I'm sorry for my bad English

  • 4 years ago

    Hi Ken,


    I am having the same problem...
    Did you get it to work?


    Thanks,
    Andre

  • 4 years ago

    If you want to know that


    1)
    Server sends data and many different clients get that data at same time instead of sending data to each client one by one then use the UDP (Universal datagram protocol) socket instead of TCP socket


    Read or search about "connection less UDP socket connections"


    2) Server receives data from more that one client at the same time then YES! this can be done using threads or multitasking


    3) Server receives data in from different clients from different ports then again Yes! you have to set another socket to listen state in you want server to get data from different port from different users

  • 4 years ago

    When ever you put a socket on the listening state you have to bind that socket with ip address and port number so you can mention the ip address and port before starting the listning process

  • 4 years ago

    Code:

    private void cmdListenClick(object sender, System.EventArgs e)
           {
               try
               {
                   //create the listening socket...
                   m
    socListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);        
                   [B]IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);[/B]
                   //bind to local IP Address...
                   msocListener.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 );
               }
           }



    see the red bold line change the

    Code:
    IPAddress.Any
    to the ip address you want

  • 3 years ago

    Rekcut-


    You're not very good at the hacking game, mate! We can find you wherever you go.

  • 3 years ago

    I have also the same problem.
    Do you have any solution?

  • 3 years ago

    It turned out the above code worked correctly. I did not supply an end of line character at the end of the query. That caused the socket to wait forever. After adding a EOL character it worked

  • 3 years ago

    In your topic, suppose that I don't want to send charater, but I want to send a binary file. To do this, I read an image from harddisk and then transfer it to binary array.


    When I send the Image binary array, I got a problem. When I know the server finish receiving data ?

  • 3 years ago


    Hello -


    Using your code - i sometimes come across an issue.


    During the operation of the application, the CPU will
    max out at 99%.


    It stays like that until i end the app (of course)


    I'm not sure where the code block is maxing out at.


    has anyone come across this issue before?


    thanks
    tony

  • 2 years ago

    Hi,


    Iam doing a socket application,facing some problem in that.The issue is the remote host is sending some 10 messages means the client machine is able to capture only 3 to 4 messages.


    i need help terribly.anyone pls help.thanks in advance

  • 2 years ago

    That's Great! But, How can you show me the way to transaction betwen two computer over internet. Especially, in this example! Can you modify code for me to have connection betwen Server and Client over Internet!  :(


    Any more, I have some question want to have any help from you for design : :)


    First: Game Online! Tell me the way to solve this kind of Programming! Server - Client!


    Second: Mobile sendding data between PC and Mobile like Yahoo! Please help me explain my question in detail! :rolleyes:


    Thank you so much for you reading!

  • 2 years ago

    hi,
    I try to build a network sniffer in .net framework 2
    and use the socketname.beginReceive(buffer,0,bufferLength,......);
    like raw socket.
    but when I convert the value in the buffer in to string ,there are meaningless staffs.
    Can you help me please..








  • 2 years ago

    The following source code example has some un-safe thread issues.

    Within the public method OnDataReceived(IAsyncResult asyn)

    txtDataRx.Text = txtDataRx.Text + szData;

     

  • 2 years ago

    Nice tutorial on Sockets.
    i've been developing and small network app and this article was very helpfull but i have problems implementing the comunication part, i can connect, receive and send data the problem arise when i try to send several message or objects(via serialization) i found that i get half of the object on the receiving stream, making imposible de deserialization of the object, i don't know if there is a workaround to this or i need to implement tokens in the message to know when to deserialize the object, any help or ideas will be apreciate :)

    also i like to know if there is a way to detect when the remotehost have been disconnected after calling the Socket.BeginReceive() method.

    Sorry for the bad english... Duke

  • 1 year ago

    Hi,

    I am new to this forum but I am trying to do exactly what you may have achieved!

    I am trying to connect multiple clients to Server!

    Any Help is appriciated

    AG

  • 1 year ago

    Hello,

    I'm currently working on the both the server and multiple clients with tcp port connections. A port has been opened at server side for listening. Have tried multiple clients connections upto 120 clients. But in certain times, new client connections to the server are failed, but the existing client connection to server side are stil working fine if they're still connected, once they disconnected it, they cant do reconnection.
    Meaning to say that the server side doesnt respond to any new client tcp connection after some times. No specific error message could be found. Do you have any idea on this? Any possibilities for this matter to happen?
    Look forward to your helps. Thanks in advance.

    Cheers.







  • 1 year ago

    Hi

    how can i acknowledege an sending message in  asynchronous  socket program????

    thank you

  • 1 year ago

    I was having the same problem until some minutes ago. The code below worked for me. See the bold section. Take a look at the help for Socket.Poll. This will return True only if there is still data to be received in the connected socket. If there's not, it sends the OK message.

    I'll also post the whole code for the Client and Server classes. It may be useful for someone. I spend several hours researching and testing to come up with this...

    I know we are in a C# area, but the code is very easy to translate.






    Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
            Try
                'Retrieve the state object and the client socket
                'from the asynchronous state object.
                Dim state As StateObject = CType(ar.AsyncState, StateObject)
                Dim client As Socket = state.workSocket
                'Read data from the remote device.
                Dim bytesRead As Integer = client.EndReceive(ar)
                If bytesRead > 0 Then
                    'There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
                    'Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)












                    'Check if there is no more data to be received. If not an OK message is sent to the client.
                    If Not client.Poll(1000000, SelectMode.SelectRead) Then
                        Send(client, "OK" & vbCrLf)
                    End If



                Else
                    'All the data has arrived;
                    'Signal that all bytes have been received.
                    receiveDone.Set()
                    InvokeDelegate(state.sb.ToString)
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try








        End Sub


    ###See the complete solution below:
    ________________________


    Public Delegate Sub StringReceivedHandlerDelegate(ByVal sRemoteAddress As String)

    Public Class Server

        Private _PortNumber As Integer
        Private DataReceived As StringReceivedHandlerDelegate
        Private listener As Socket

        Sub New(ByVal PortNumber As Integer)
            _PortNumber = PortNumber
        End Sub

        Public Sub StartServer()
            Listen()
        End Sub

        Public Sub StopServer()
            If Not listener Is Nothing Then
                listener.Close()
            End If
        End Sub

        Public Class StateObject
            'Client socket.
            Public workSocket As Socket = Nothing
            'Size of receive buffer.
            Public Const BufferSize As Integer = 8192
            'Receive buffer.
            Public buffer() As Byte = New Byte(BufferSize - 1) {}
            'Received data string.
            Public sb As New StringBuilder
        End Class

        'ManualResetEvent instances signal completion.
        Private Shared connectDone As New ManualResetEvent(False)
        Private Shared sendDone As New ManualResetEvent(False)
        Private Shared receiveDone As New ManualResetEvent(False)

        Private Sub Listen()
            Try
                Dim remoteEP As New IPEndPoint(IPAddress.Any, _PortNumber)
                listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                listener.Bind(remoteEP)
                listener.Listen(10)
                listener.BeginAccept(New AsyncCallback(AddressOf ConnectCallback), listener)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub ConnectCallback(ByVal ar As IAsyncResult)
            Try
                'Retrieve the socket from the state object.
                Dim client As Socket = CType(ar.AsyncState, Socket)
                'Complete the connection.
                client = client.EndAccept(ar)
                Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString())
                'Start Receiving
                Receive(client)
                'Signal that the connection has been made.
                connectDone.Set()
                listener.BeginAccept(New AsyncCallback(AddressOf ConnectCallback), listener)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub Receive(ByVal client As Socket)
            Try
                'Create the state object.
                Dim state As New StateObject
                state.workSocket = client
                'Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
            Try
                'Retrieve the state object and the client socket
                'from the asynchronous state object.
                Dim state As StateObject = CType(ar.AsyncState, StateObject)
                Dim client As Socket = state.workSocket
                'Read data from the remote device.
                Dim bytesRead As Integer = client.EndReceive(ar)
                If bytesRead > 0 Then
                    'There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
                    'Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
                    'Check if there is no more data to be received an OK message is sent to the client
                    If Not client.Poll(1000000, SelectMode.SelectRead) Then
                        Send(client, "OK" & vbCrLf)
                    End If
                Else
                    'All the data has arrived;
                    'Signal that all bytes have been received.
                    receiveDone.Set()
                    InvokeDelegate(state.sb.ToString)
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub Send(ByVal client As Socket, ByVal data As String)
            Try
                'Convert the string data to byte data using ASCII encoding.
                Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
                'Begin sending the data to the remote device.
                client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub SendCallback(ByVal ar As IAsyncResult)
            Try
                'Retrieve the socket from the state object.
                Dim client As Socket = CType(ar.AsyncState, Socket)
                'Complete sending the data to the remote device.
                Dim bytesSent As Integer = client.EndSend(ar)
                Console.WriteLine("Sent {0} bytes to server.", bytesSent)
                'Signal that all bytes have been sent.
                sendDone.Set()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Public Sub SetStringInputHandler(ByVal pMethod As StringReceivedHandlerDelegate)
            Try
                Monitor.Enter(Me)
                If DataReceived Is Nothing Then
                    DataReceived = pMethod
                End If
                Monitor.Exit(Me)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

        Private Sub InvokeDelegate(ByVal sData As String)
            Try
                DataReceived.Invoke(sData)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub

    End Class

    ________________________

    Public Class Client

        Private _RemoteHost As String
        Private _RemotePort As Integer
        Private _NetworkStream As NetworkStream
        Dim _TCPClient As TcpClient

        Sub New(ByVal RemoteHost As String, ByVal RemotePort As Integer)
            _RemoteHost = RemoteHost
            _RemotePort = RemotePort
        End Sub

        Public Function SendStringMessage(ByVal Message As String) As String
            Try
                _TCPClient = New TcpClient(
    RemoteHost, RemotePort)
                _NetworkStream = _TCPClient.GetStream()
                '
    NetworkStream.WriteTimeout = 10000
                'NetworkStream.ReadTimeout = 10000
                Dim strResponse As String
                ' Send a string (newline terminated) to the server.
                Dim writer As New System.IO.StreamWriter(
    NetworkStream)
                Dim reader As New System.IO.StreamReader(_NetworkStream)
                writer.Write(Message)
                writer.Flush()
                ' Read server response (up to a newline).
                Try
                    strResponse = reader.ReadLine
                Catch ex As Exception
                    strResponse = Nothing
                End Try
                'Close
                writer.Close()
                reader.Close()
                _NetworkStream.Close()
                Return strResponse
            Catch ex As Exception
                Return Nothing
            Finally
                If Not _TCPClient Is Nothing Then
                    _TCPClient.Close()
                    _TCPClient = Nothing
                End If
            End Try
        End Function

    End Class

    Holpe it helped.

    Regards,

    Afas.



























































































































































































































  • 1 month ago

    the programme is giving error as cross thread error the application is accessed from the thread other then it is created

    Within the public method OnDataReceived(IAsyncResult asyn)

    txtDataRx.Text = txtDataRx.Text + szData;

    how can I avoid it

  • 24 days ago

    Hi, Thanks for your article.Very useful! I haven't been able to think how a continuous communication which server does different actions when client sends a data in, can be provided.Namely,if I don't misunderstand the logic of the program,when a client connects,on server WaitForData function waits for receiving data from client.When server receives,then the asynchronous callback function OnDataReceived is invoked, and the related processes are done. Now, my question is this: After receiving data from client and invoking OnDataReceived function and then sending server's reply, for receiving new data,again it is necessary to call WaitForData fuction, is't it?Then after receiving this new data ,again it is called OnDataReceived function, namely the same processes.Then, how can we provide a continuous data exchange as SendTo-ReceiveFrom consecutively? Thanks in advance.

Post a reply

Enter your message below

Sign in or Join us (it's free).