Sockets - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Sockets

Description:

Computer has one or more hardware network interface card (NIC) ... For the client, it names the socket with the IP address and port number of the server. ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 19
Provided by: phi140
Category:
Tags: address | computer | ip | my | sockets

less

Transcript and Presenter's Notes

Title: Sockets


1
Sockets
  • Socket is an endpoint of communication.
  • Software construct that allows applications to
    access network.
  • Computer has one or more hardware network
    interface card (NIC).
  • This is the gate to the external network.
  • May be shared by many sockets.
  • Socket Name consists of IP address and Port
    number.

2
Winsock
  • Windows networking library called winsock.
  • Windows Sockets
  • When client socket connects to a server socket
    form an association.

3
Network Program Sketch (Client)
  • Open a socket.
  • Name the socket.
  • Connect to server socket.
  • Send and receive messages between client and
    server sockets.
  • Very similar to message passing between windows.
  • Close the socket.

4
Step 1 Open a Socket
  • SOCKET sock
  • sock socket (AF_INET, SOCK_STREAM, 0)
  • SOCK_DGRAM for UDP.
  • AF_INET Internet family
  • SOCK_STREAM Indicates TCP protocol
  • Third value ignored for TCP and UDP.

5
Step2 Naming a Socket
  • Name consists of three attributes
  • Protocol
  • IP Address
  • Port Number
  • Generic Socket Address Structure
  • struct sockaddr
  • u_short as_family
  • char sa_data14 //Value depends on address
    family

6
Socket Address Structure
  • struct sock_addr is generic. For TCP/IP use
  • struct sockaddr_in data structure.
  • To name a socket, fill in the three fields of
    this struct.
  • struct sockaddr_in
  • short sin_family // always AF_INET
  • u_short sin_port // in network order
  • struct sin_addr // IP Address.

7
Port Number
  • Port 0 1023 reserved for well known services
  • HTTP 80
  • FTP 23
  • Users can assign numbers 1025 5000.

8
IP Address
  • struct sin_addr holds 32 bit IP Address
  • Access s_addr field of sin_addr.
  • Example (Almost correct)
  • struct sockaddr_in sa
  • sa.sin_family AF_INET
  • sa.sin_port 3000
  • sa.sin_addr.s_addr 32.33.14.55

9
Host/Network Order
  • sa.sin_port htons(3000)
  • htons converts short to network byte order.
  • sa.sin_addr.s_addr inet_addr( 32.33.14.55)
  • inet_addr takes string (dotted notation) and
    converts is to a long in network order.

10
Client Fills in Address and Port of Server
  • struct sockaddr_in sa
  • sa.sin_family AF_INET
  • sa.sin_port htons (3000)
  • sa.sin_addr.s_addr inet_addr(32.33.14.55 )

11
Step 3 Association with Another Socket
  • Client and server set up the association
    differently.
  • For the client, it names the socket with the IP
    address and port number of the server.
  • Client performs a connect() function call to
    connect to the server.

12
Connect
  • Fill in sockaddr_in structure with IP address and
    port number of server.
  • sa.sin_family AF_INET
  • sa.sin_port htons (Server_Port)
  • sa.sin_addr.s_addr inet_addr(xxx.xxx.xx.XX)
  • int ret_val connect(sock, (struct sock_addr )
    sa, sizeof(sa))
  • Success Returns 0
  • Fail Returns SOCKET_ERROR

13
  • connect will fail if the server is not in the
    listening state and accepts the connection.
  • On success, the client has established a virtual
    TCP connection on which it can send and receive
    data reliably.

14
Step 4 Send/Receive Messages
  • Must use a connected socket.
  • char buf1024
  • int ret_val recv(sock, buf, 1024, 0)
  • Success Returns number of bytes actually
    received
  • Fail Returns SOCKET_ERROR
  • NOTE May not return all requested data.

15
Step 5 Close Socket
  • Should call closesocket() when socket no longer
    needed.
  • ret_val closesocket(sock)
  • if (ret_val ! 0)
  • // Some error condition. I am going to call my TA.

16
Other Program Requirements
  • Applications must
  • include ltwinsock2.hgt
  • Link in the winsock dlls.
  • Initialize the socket dlls

17
Linking with Winsock Dlls
  • Go to the project tab and click on settings.
  • Choose the link tab and add WS2_32.lib to the
    object/library modules.

18
Initialization
  • WSAData wsadata
  • int ret_val
  • ret_val WSAStartup( MAKEWORD(2,0), wsadata )
  • if (stat ! 0)
  • I have a problem
  • This initialization must precede all other socket
  • functions.
  • Need to call WSACleanup() when finished with
    DLLs.
Write a Comment
User Comments (0)
About PowerShow.com