Library tutorials & articles
Socket Programming in C# - Part 2
- Introduction
- Getting Started
- Multiple Sockets
- Server Side
- Conclusion
Introduction
This is the second part of the previous article about the socket programming. In the earlier article we created a client but that client used to make blocking IO calls ( Receive ) to read data at regular intervals (via clicking the Rx button). But as I said in my earlier article, that model does not work very well in a real world application. Also since Windows is an events-based system, the application (client) should get notifications of some kind whenever the data is received so that client can read it rather than client continuously polling for data.
Well that is possible with a little effort. If you read the first part of
this article, you already know that the Socket class in the Systems.Net.Sockets namespace has several methods like Receive and Send which are blocking calls.
Besides there are also functions like BeginReceive , BeginSend etc. These are
meant for asynchronous IO . For example , there are at least two problems with
the blocking Receive:
When you call Receive function the call blocks if no data is present, the
call blocks till some data arrives.
Even if there is data when you made the receive call , you don't know when
to call next time. You need to do polling which is not an efficient way.
Related articles
Related discussion
-
Query Tool to Excel using C# and .NET
by BarbaMariolino (1 replies)
-
looking for help on asp
by cladironbeard (2 replies)
-
Socket Programming in C# - Part 1
by graumanoz (23 replies)
-
LINQ in Action
by naser1 (0 replies)
-
Creating a Windows Service in VB.NET
by Templario55 (107 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
Events coming up
-
Aug
28
St. Louis Day of .NET
St. Charles, United States
Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend
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.
Hi
how can i acknowledege an sending message in asynchronous socket program????
thank you
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.
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
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
The following source code example has some un-safe thread issues.
Within the public method OnDataReceived(IAsyncResult asyn)
txtDataRx.Text = txtDataRx.Text + szData;
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..
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!
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
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
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 ?
Do you have any solution?
Rekcut-
You're not very good at the hacking game, mate! We can find you wherever you go.
private void cmdListenClick(object sender, System.EventArgs e)
{
try
{
//create the listening socket...
msocListener = 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...
msocListener.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
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
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
Hi Ken,
I am having the same problem...
Did you get it to work?
Thanks,
Andre
Hi!
is it possible that the server manage more accesses in parallel?
I'm sorry for my bad English
/* declare a public variable */
// Recording "cmdListen" button is "Start Listening" or "Stop Listening" so far.
private bool isListening = false;
private void cmdListen_Click(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 m_socListener */
//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.Text = "Stop Listening";
btnSend.Enabled = true;
isListening = true;
}
} catch(SocketException se) {
MessageBox.Show ( se.Message );
}
}
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
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
Sorry ... where is a response ?? .. thanks
IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,8221);
Sorry I did not look at the complete response list and see that this question was already posted.
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.
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??
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
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
regards,
tuco
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?
Did any of you get the program to work with multiple clients?
If so, how did you do it?
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
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
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
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,
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
Grettings!
This thread is for discussions of Socket Programming in C# - Part 2.