Networking concept with chat server programming
-
Upload
kanishk-raj -
Category
Education
-
view
156 -
download
2
Transcript of Networking concept with chat server programming
S.NO.
PROGRAM NAME REMARK
1.What is the network.
2.Overview of OSI model.
3.Define the IP & IPv4Why IP address
4.IP header , IPv4 range
5.What is the socket , Type of socket
6.Write the socket program .
1. What is the networks ?
A collection of two or more computers interconnected by the telephone lines, co-axial cable, satellite links, radio and microwave transmission and some other communication techniques. A computer network is a group of computers that are connected together and that communicate with one another for a common purpose.
Although the computer industry is young compared to anther industries (e.g., automobiles air transportation), computer have made spectacular progress in a short time. During the first two decades of their extrinsic, computer system highly centralized, usually a single large room. A medium size company or university might have had one or two computers, while large instructions had at most a few dozen. The idea that with in 20 years equally powerful computers smaller than postage stamps would be mass produced by the millions was pure science fiction.
The merging of computers & communications has had a profound influence on the way computer systems are organized. The concept of the computer center as a room with a large computer to which users bring their work for processing is now totally obsolete. The old model of single computer serving all of the organizations computational needs has been replaced by one in which a large number of separate but interconnected computers do the job. These systems are called computer networks.
1 .Overview of OSI model ?
The Open Systems Interconnection model (OSI model) is a conceptual model that characterize standardizes thecommunication functions of a telecommunication or computing system without regard to their underlying internal structure and technology. Its goal is the interoperability of diverse communication systems with standard protocols.
The Seven Layers of the OSI Model
2. Define the IPv4 ?
Internet Protocol Characteristics
Operates at network layer of OSI Connectionless protocol Packets treated independently Hierarchical addressing Best-effort delivery No data-recovery features
Why IP Addresses?
They uniquely identify each device on an IP network.
Every host (computer, networking device, peripheral) must have a unique address.
Host ID:
– Identifies the individual host
– Is assigned by organizations to individual devices
IP PDU Header
IP Address Format: Dotted Decimal Notation
IP Address Ranges
Class A addresses begin with 0xxx, or 1 to 126 decimal.
Class B addresses begin with 10xx, or 128 to 191 decimal.
Class C addresses begin with 110x, or 192 to 223 decimal.
Class D addresses begin with 1110, or 224 to 239 decimal.
Class E addresses begin with 1111, or 240 to 254 decimal.
*127 (01111111) is a Class A address reserved for loopback
testing and cannot be assigned to a network.
What is the socket ?
A network socket is one endpoint in a communication flow between two programs running over a network.Sockets allow communication between two different processes on the same or different machines.
A Unix Socket is used in client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client & server and then for exchanging data.
If everything goes well, the server accepts the connection
Socket Types
There are four types of sockets available to the users. The
first two are most commonly used and the last two are
rarely used.
Stream Sockets − Delivery in a networked environment
is guaranteed. If you send through the stream socket
three items "A, B, C", they will arrive in the same order
− "A, B, C". These sockets use TCP (Transmission Control
Protocol) for data transmission. If delivery is impossible,
the sender receives an error indicator. Data records do
not have any boundaries.
Datagram Sockets − Delivery in a networked
environment is not guaranteed. They're connectionless
because you don't need to have an open connection as
in Stream Sockets − you build a packet with the
destination information and send it out.
Raw Sockets − These provide users access to the underlying
communication protocols, which support socket
abstractions. These sockets are normally datagram oriented,
though their exact characteristics are dependent on the
interface provided by the protocol. Raw sockets are not
intended for the general user; they have been provided
mainly for those interested in developing new
communication protocols, or for gaining access to some of
the more cryptic facilities of an existing protocol.
Sequenced Packet Sockets − They are similar to a
stream socket, with the exception that record
boundaries are preserved.
WAP to implement Chat server using Socket Programming.
using System;using System.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Net.Sockets;using System.Net;
namespaceSocketProgramming{publicpartialclassForm1 : Form {Socketskt;EndPointclientep, remoteep;public Form1() {
InitializeComponent();skt = newSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);skt.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
textBox1.Text = callerip();
textBox2.Text = callerip(); }privatestringcallerip() {IPAddress host;IPHostEntry host1; host1 = Dns.GetHostEntry(Dns.GetHostName());foreach (IPAddressipin host1.AddressList) {if (ip.AddressFamily == AddressFamily.InterNetwork) {
returnip.ToString(); }
}return"172.251.1.0";
}
privatevoid listBox1_SelectedIndexChanged(object sender, EventArgs e) {
}
privatevoid button2_Click(object sender, EventArgs e) {
clientep = newIPEndPoint(IPAddress.Parse(textBox1.Text),Convert.ToInt32(textBox3.Text));
skt.Bind(clientep);
remoteep = newIPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(textBox4.Text))skt.Connect(remoteep);byte[] b = newbyte[1500];skt.BeginReceiveFrom(b, 0, b.Length, SocketFlags.None, refremoteep,newAsyncCallback(mymessagecaller),b); button2.Enabled = false; button2.Text = "Connected";textBox5.Focus();
}publicvoidmymessagecaller(IAsyncResultaResult) {try {int size = skt.EndReceiveFrom(aResult, refremoteep);if (size > 0) {
byte[] recieveddata = newbyte[1464];
recieveddata = (byte[])aResult.AsyncState;ASCIIEncodingeEncoding = newASCIIEncoding();stringrecievemessage = eEncoding.GetString(recieveddata);listBox1.Items.Add("Freind:" + recievemessage); }byte[] buffer = newbyte[1500];skt.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, refremoteep, newAsyncCallback(mymessagecaller), buffer);
}catch (Exception) { } }
privatevoid button1_Click(object sender, EventArgs e) {System.Text.ASCIIEncodingenc = newSystem.Text.ASCIIEncoding();byte[] b = newbyte[1500]; b = enc.GetBytes(textBox5.Text);skt.Send(b);listBox1.Items.Add("Me:"+textBox5.Text);textBox5.Clear(); } }
}
BY – Kanishk Raj (CCNA)