Public Socket (Addressfamily Af, Sockettype ST, Protocoltype PT)
Public Socket (Addressfamily Af, Sockettype ST, Protocoltype PT)
Objectives:
Learn about the Socket class
Learn how to write a TCP server using the Socket class
Learn how to write a TCP client using the Socket class
Learn how to Handle Text in Socket applications
Learn about some problems that can occur in TCP and how to handle
them.
Methods:
The following are some of the methods of the Socket class. Some of
these methods are used by Server sockets while others are for client
sockets.
The following figure shows how some of these methods may be called by
a Tcp client-server application.
2. Creating a TCP Server Using the Socket class
The following example shows how to use the Socket class to create a
server.
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class SimpleTcpSocketServer {
public static void Main() {
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(localEP);
server.Listen(10);
int dataSize = 0;
while(true) {
data = new byte[1024];
dataSize = client.Receive(data);
if (dataSize == 0)
break;
Console.WriteLine(Encoding.ASCII.GetString(data,0, dataSize));
client.Send(data, dataSize, SocketFlags.None);
}
client.Close();
server.Close();
}
}
3. Creating a TCP Client Using the Socket class
The following example shows how to use the Socket class to create a
client.
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
class SimpleTcpSocketClient {
public static void Main() {
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEP = new
IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
try {
socket.Connect(remoteEP);
}catch (SocketException e) {
Console.WriteLine("Unable to connect to server. ");
Console.WriteLine(e);
return;
}
Now does the Socket class has a method similar GetStream()? The
answer is no. However, the NetworkStream class has a constructor
that accepts a Socket instance as argument.
Thus, after accepting a connection from a client (in the case of server) or
after connecting to a server (in the case of a client), the resulting socket
can be used to create a NetworkStream instance, which can then be
used to create StreamReader and StreamWriter instances as before.
In the Server:
Socket client = server.Accept();
IPAddress clientAddress =
((IPEndPoint)client.RemoteEndPoint).Address;
Console.WriteLine("Got connection from "+clientAddress);
In the Client:
IPEndPoint remoteEP = new
IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
try {
socket.Connect(remoteEP);
}catch (SocketException e) {
Console.WriteLine("Unable to connect to server. ");
Console.WriteLine(e);
return;
}
The implication here is that TCP does not respect message boundaries.
That is, there is no one-to-one correspondence between number/size of
individual messages sent and the number/size of individual messages
received.
Of course TCP pair will ensure that no data is lost. The problem is with
applications. How will they know how many times they need to read
before they collect the whole message?
Solutions:
1. For Text messages only, use ReadLine and WriteLine methods of
the StreamReader and StreamWriter classes respectively.
The advantage here is that the WriteLine will insert end-of-line
markers in the message and the ReadLine will read one line at a time
until there is no more lines to read.
Notice here that the problem of too small buffer does not even arise in
this case, since the ReadLine and WriteLine methods will take care
of this.
2. Send the size of the message before sending the message. This is
applicable for any type of data where Send and Receive methods are
used for sending and Receiving.
The idea here is, since the receiver knows the total size of the data, it
will read the data in a loop, each time taking note of the actual size
returned by the Receive method and updating the amount so far read
until the entire size is read.
A loop similar to the following is used.