Socket Programming
with C#
Usama Siddique Tehreem Qamar
Talha Shafique Wisha Faheem
Yasir Latif Uswah Zubair
Yasir Ali Zahra Ali
What is Socket?
A socket is one endpoint of a two-way
communication link between two
programs running on the network.
Windows Socket
• Windows Sockets 2 (Winsock) enables programmers to create
advanced Internet, intranet, and other network-capable
applications to transmit application data across the wire,
independent of the network protocol being used.
Server vsClient
• Server: Listen on aport
• Client: Connect toserver
If the server is not “Listening” then client can not connect to it.
Server steps
Create a socket.
Bind the socket.
Listen on the socket for a client.
Accept a connection from a client.
Receive and senddata.
Disconnect.
Client steps
Create a socket.
Connect to the server.
Send and receive data.
Disconnect.
C# socket class
class TcpListener : Listens for connections from TCP network clients.
class TcpClient :Provides client connections for TCP network services.
class UdpClient : Provides User Datagram Protocol (UDP) network services.
NetworkStream : Provides the underlying stream of data for network access.
SocketException The exception that isthrown whenasocket error occurs.
Synchronous andAsynchronous
Many applications call methods asynchronously because it enables
the application to continue doing useful work while the method call
runs.
An asynchronous client socket does not suspend the application while waiting
for network operations to complete.
Synchronous call return immediately without waiting for the method to
finished.
Coding in C#