Network Programming-C#
Network Programming-C#
Network Programming
Definiation:
Computers running on the Internet communicate to each
other using either the Transmission Control Protocol
(TCP) or the User Datagram Protocol (UDP), as this
diagram illustrates:
Error! Use the Home tab to apply
Pg. 2 Heading 1 to the text that you want to
Protocol:
In this Context:
TCP Protocol:
Definition: TCP (Transmission Control Protocol) is a
connection-based protocol that provides a reliable
flow of data between two computers.
UDP Protocol:
Error! Use the Home tab to apply
Pg. 3 Heading 1 to the text that you want to
CLIENT-SERVER MODEL:
Error! Use the Home tab to apply
Pg. 4 Heading 1 to the text that you want to
Namespaces:
System.Net
Error! Use the Home tab to apply
Pg. 5 Heading 1 to the text that you want to
System.Net.Sockets
Server Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
//Creating EndPoint of Server --> Parameters--> IPAddress=Loopback[127.0.0.1],Port=8080
IPEndPoint Server_end = new IPEndPoint(IPAddress.Loopback, 8080);
//Creating Socket of Server ---> Parameters AddressFamily.InterNetwork(IPv4),
SocketType.Stream(We use it For TCP transmission), TCP protocol as protocol type.
Socket server_socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
//Disabling the socket connection -- SocketShutdown.Both means disabling both send and receive
server_socket.Shutdown(SocketShutdown.Both);
//closing the socket to get release the socket
server_socket.Close();
handler_socket.Shutdown(SocketShutdown.Both);
handler_socket.Close();
Console.ReadKey();
}
}
}
Error! Use the Home tab to apply
Pg. 7 Heading 1 to the text that you want to
Client Code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
client_socket.Connect(Client_end);
string msg = "Connection Sucessful";
//converting string msg to bytes and passing it to byte array
byte[] bytes_to_send = ASCIIEncoding.ASCII.GetBytes(msg);
//sending byte array
client_socket.Send(bytes_to_send);
OUTPUT:
Error! Use the Home tab to apply
Pg. 8 Heading 1 to the text that you want to