SOCKETS IN C - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

SOCKETS IN C

Description:

A way to communicate with other programs using standard file descriptors ... user friendly ... Give user friendly address to Dns.Resolve. Returns a list of ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 12
Provided by: Magy8
Category:

less

Transcript and Presenter's Notes

Title: SOCKETS IN C


1
SOCKETS IN C
  • Heading towards the Chat Program

2
Basic Introduction to Sockets
  • What is a socket?
  • A way to communicate with other programs using
    standard file descriptors
  • Types of Sockets
  • Datagram and Stream Sockets

3
Basic Client Side Algorithm
  • Create a socket
  • Get the IP address and port number of the server
    (create a data pipe)
  • Send Data
  • Receive acknowledgement/data
  • Send Data

4
Basic Server Side Algorithm
  • Create a Listening Socket
  • Get IP Address and Port number of the Client that
    is communicating
  • Receive Data sent
  • Send Data/Acknowledge data
  • Receive Data

5
Absolute Basics
  • All socket services in System.Net.Sockets
    namespace
  • System.Net provides services for DNS, IP
    Addresses, HTTP services etc

6
Creating a Socket
  • Must specify address family, socket type and
    protocol type
  • Address Family InterNetwork (Addr for IP Ver
    4).. InterNetworkV6 (ver 6)
  • Socket Type Stream, Dgram, Raw etc
  • Protocol Type Tcp, Udp
  • Socket s new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp)

7
Next Step..Creating a data pipe
  • What we need IP address and port number
    162.312.324.4008000
  • What we have a more user friendly web address
  • What we need is known in C as an Endpointthere
    is an Endpoint Class for each family, for IP
    addresses the class is known as IPEndPoint

8
Creating the EndPoint...
  • Give user friendly address to Dns.Resolve
  • Returns a list of addresses select one
  • Use the address to create the EndPoint
  • IPHostEntry iphost Dns.Resolve
    (http//ist.psu.edu)
  • IPAddress ipiphost.AddressList0
  • IPEndPoint EndPointnew IPEndPoint(ip,
    portnumber)
  • s.Connect(EndPoint)
  • (note that port number must be Int16..use
    System.Convert.ToInt16)

9
Sending Data
  • The Send method can send data through a CONNECTED
    socket
  • byte msg System.Text.Encoding.ASCII.GetBytes("
    This is a test")
  • int bytesSent s.Send(msg)

10
Receiving Data
  • We use the Receive method (similar to Send)
  • byte bytes new byte1024
  • s.Receive(bytes)
  • //Displays to the screen.
  • Console.WriteLine(Encoding.ASCII.GetString(bytes
    ))

11
Shutdown sockets
  • The SocketShutdown enumeration defines constants
    that indicate whether the socket should be closed
    for sending, for receiving, or for both.
  • Call the Close method to free all managed and
    unmanaged resources associated with the Socket
  • s.Shutdown(SocketShutdown.Both)
  • s.Close()
Write a Comment
User Comments (0)
About PowerShow.com