Javatpoint Logo
Javatpoint Logo

C# Socket Programming


C# is a powerful and popular programming language that has been used to develop a wide variety of applications. One of its main strengths is its ability to perform Socket programming, which enables developers to create applications that can communicate with other devices or applications over a Network. In this article, we will dive deep into the basics of C# Socket programming and how it can be used to create Networked applications.

What are Sockets?

If two programs are running on the same Network and if they share a two-way Communication Link, then their endpoint will be denoted by Socket. A Socket is characterized by its IP Address, Port Number, and Protocol. It is used to set up the connection, send and receive the information and close the connection.

Types of Sockets:

There are generally two types of Sockets: one is Stream Sockets and the second one is Datagram Sockets. Stream Sockets are connection-oriented and provide reliable, two-way Communication between devices. They make sure the order of data should be the same as it was sent. Datagram Sockets are connectionless and provide an unreliable, one-way Communication between devices. They do not make sure that the order of received data is the same as the order of sent.

What is Socket Programming?

Socket programming is a technique used to establish Communication between two applications over a Network. It involves creating a Socket, which is essentially a software endpoint that can send and receive data over the Network. The two applications communicate by sending data through their respective Sockets, which are connected via a Network.

C# Socket Programming Basics:

Before diving into the specifics of C# Socket Programming, it's important to understand some basic concepts of Socket programming in general. A Socket is a type of software endpoint that symbolizes one end of a two-way Communication channel between two Network-based programs. A Socket is defined by an IP Address and a Port Number, and these two values uniquely identify the Socket.

In C#, the Socket class provides a way to create and manipulate Sockets. The Socket class is part of the System.Net.Sockets namespace, and it provides a set of methods and properties that enable you to create, configure, connect, send, and receive data over a Network.

To use Sockets in C#, you must first import the System.Net.Sockets namespace. The Socket class is used to create and manipulate Sockets.

The following code represents how to create a new Socket in C#:

The AddressFamily parameter specifies the addressing scheme used by the Socket. The type of the Socket is represented by the SocketType parameter. The ProtocolType parameter describes the protocol used by the Socket.

Connect() method is taken into consideration for setup the connection between two devices. The following code shows how to connect to a remote device using C# Socket Programming:

The IPAddress parameter specifies the IP Address of the remote device. The IPEndPoint parameter specifies the endpoint of the remote device.

To send data over a Socket, the Send() method is used. The following code shows how to send data over a Socket using C# Socket Programming:

The byte[] parameter specifies the data to be sent. The Encoding.ASCII.GetBytes() method is used to convert the string "Hello World" to a byte array.

To receive data over a Socket, the Receive() method is used. The following code shows how to receive data over a socket using C# Socket Programming:

The byte[] parameter specifies the buffer that receives the data. The int parameter specifies the maximum number of bytes to receive. The Encoding.ASCII.GetString() method is used to convert the byte array to a string.

To close a connection, the Close() method is used. The following code shows how to close a connection using C# Socket Programming:

C# Socket Programming Example

C# Code:

In this example, the server listens for incoming connections on port 1234, and the client connects to the server on the loopback address (localhost) and port 1234. Once the connection is established, the client sends information to the server, and the server responds with a message. Finally, both the client and server close their Sockets.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA