Socket Programming

Topics: Internet socket, IP address, Transmission Control Protocol Pages: 21 (4392 words) Published: September 5, 2013
sockets

Socket Programming in C/C++
c Mani Radhakrishnan and Jon Solworth September 24, 2004

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

Contact Info

Mani Radhakrishnan Office 4224 SEL email mradhakr @ cs . uic . edu Office Hours Tuesday 1 - 4 PM

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

Introduction

Sockets are a protocol independent method of creating a connection between processes. Sockets can be either connection based or connectionless: Is a connection established before communication or does each packet describe the destination? packet based or streams based: Are there message boundaries or is it one stream? reliable or unreliable. Can messages be lost, duplicated, reordered, or corrupted?

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

Socket characteristics

Socket are characterized by their domain, type and transport protocol. Common domains are: AF UNIX: address format is UNIX pathname AF INET: address format is host and port number Common types are: virtual circuit: received in order transmitted and reliably datagram: arbitrary order, unreliable

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

Socket characteristics (cont’d)

Each socket type has one or more protocols. Ex: TCP/IP (virtual circuits) UDP (datagram) Use of sockets: Connection–based sockets communicate client-server: the server waits for a connection from the client Connectionless sockets are peer-to-peer: each process is symmetric.

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

Socket APIs
socket: creates a socket of a given domain, type, protocol (buy a phone) bind: assigns a name to the socket (get a telephone number) listen: specifies the number of pending connections that can be queued for a server socket. (call waiting allowance) accept: server accepts a connection request from a client (answer phone) connect: client requests a connection request to a server (call) send, sendto: write to connection (speak) recv, recvfrom: read from connection (listen) shutdown: end the call c Mani Radhakrishnan and Jon Solworth Socket Programming in C/C++

sockets

TCP UDP

Connection-based communication
Server performs the following actions socket: create the socket bind: give the address of the socket on the server listen: specifies the maximum number of connection requests that can be pending for this process accept: establish the connection with a specific client send,recv: stream-based equivalents of read and write (repeated) shutdown: end reading or writing close: release kernel data structures

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

TCP client

Client performs the following actions socket: create the socket connect: connect to a server send,recv: (repeated) shutdown close

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

TCP-based sockets
server socket client socket

bind

connect

listen

accept

close

send/recv

send/recv

shutdown

shutdown

close

close

c Mani Radhakrishnan and Jon Solworth

Socket Programming in C/C++

sockets

TCP UDP

socket API
#i n c l u d e 2 #i n c l u d e 4 i n t s o c k e t ( i n t domain , i n t t y p e , i n t p r o t o c o l ) ;

Returns a file descriptor (called a socket ID) if successful, -1 otherwise. Note that the socket returns a socket descriptor which is the same as a file descriptor. The domain is AF INET. The type argument can be: SOCK STREAM: Establishes a virtual circuit for stream SOCK DGRAM: Establishes a datagram for communication SOCK SEQPACKET: Establishes a reliable, connection based, two way communication with maximum message size. (This is not available on most machines.) protocol is usually...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay about Network Programming with Sockets
  • Client / Server Programming with Tcp/Ip Sockets Essay
  • smart socket Essay
  • Essay on Network Programming Project Report
  • Ethernet Port's Socket Programming.Rtf Essay
  • programming Essay
  • It Programming Essay
  • Programming Solutions Essay

Become a StudyMode Member

Sign Up - It's Free