Você está na página 1de 8

Error!

Use the Home tab to apply


Pg. 1 Heading 1 to the text that you want to

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:

Set of Rules to perform an operation

In this Context:

protocols define format, order of msgs sent and received


among network entities, and actions taken on msg
transmission, receipt

TCP Protocol:
Definition: TCP (Transmission Control Protocol) is a
connection-based protocol that provides a reliable
flow of data between two computers.

It provides a point-to-point channel for applications that


require reliable communications. The Hypertext Transfer
Protocol (HTTP), File Transfer Protocol (FTP), and Telnet
are all examples of applications that require a reliable
communication channel

UDP Protocol:
Error! Use the Home tab to apply
Pg. 3 Heading 1 to the text that you want to

Definition: UDP (User Datagram Protocol) is a protocol


that ends independent packets of data, called
datagrams, from one computer to another with no
guarantees about arrival. UDP is not connection-based
like TCP.

The UDP protocol provides for communication that is


not guaranteed between two applications on the
network

It sends independent packets of data,


called datagrams, from one application to another

CLIENT-SERVER MODEL:
Error! Use the Home tab to apply
Pg. 4 Heading 1 to the text that you want to

Network Programming in C#:

Namespaces:

System.Net
Error! Use the Home tab to apply
Pg. 5 Heading 1 to the text that you want to

System.Net.Sockets

Client Server Application Demo:


Error! Use the Home tab to apply
Pg. 6 Heading 1 to the text that you want to

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);

//Binding Socket ---> Parameter is endpoint ?


server_socket.Bind(Server_end);
//Starting to Listen on Server_Socket
server_socket.Listen(10);
//Passing accept connection to another socket(handler Socket)
Socket handler_socket = server_socket.Accept();
//byte array is to receive incoming byte of information
byte[] receive_byte_array = new byte[1024];
//counting the no of bytes of incoming information
int no_of_bytes = handler_socket.Receive(receive_byte_array);
//writing coming information ---> ASCIIEncoding class provides methods to convert bytes
to string and string to bytes
Console.WriteLine(ASCIIEncoding.ASCII.GetString(receive_byte_array,0,no_of_bytes));

//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)
{

//Creating EndPoint of Server --> Parameters-->


IPAddress=Loopback[127.0.0.1],Port=8080
IPEndPoint Client_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 client_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

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);

//Disabling the socket connection -- SocketShutdown.Both means disabling both


send and receive
client_socket.Shutdown(SocketShutdown.Both);
//closing the socket to get release the socket
client_socket.Close();
}
}
}

OUTPUT:
Error! Use the Home tab to apply
Pg. 8 Heading 1 to the text that you want to

Você também pode gostar