Sockets.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Network Layer Programing Connection-Oriented Sockets SWE 344 Internet Protocols & Client Server Programming.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r Internet gaming r 2.3 FTP r 2.4 Electronic.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
Connectionless Sockets SWE 344 Internet Protocols & Client Server Programming.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Socket address structures Byte ordering IP address conversion
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Client-Server model. Socket programming 
Sockets and Beginning Network Programming
Assignment 3 A Client/Server Application: Chatroom
Socket programming Péter Verhás August 2002
CS 1652 Jack Lange University of Pittsburgh
Socket Programming (Cont.)
Internet Address Routines
Chapter4 Elementary TCP Socket
Network Programming with Sockets
Tutorial on Socket Programming
First glimpse of the Sockets API
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
Recitation 11 – 4/29/01 Outline Sockets Interface
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
Berkeley API Socket Programming
Chapter 2: Application layer
28.
Socket Programming.
Berkeley API Socket Programming
Chapter 2: Application layer
ECS152b Behrooz Khorashadi
Programming with TCP/IP
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Berkeley API Socket Programming
in unistd.h: int gethostname(char * name, int maxlen)
Outline Communications in Distributed Systems Socket Programming
Sockets.
CSI 4118 – UNIVERSITY OF OTTAWA
Chapter 2: Application layer
Presentation transcript:

Sockets

What is Socket? The end point of communication between 2 systems on a network (combination of IP&port on each end of the communication) 4-Tuple Name of API

http://inst. eecs. berkeley http://inst.eecs.berkeley.edu/~cs194-5/sp08/lab2/index_files/image001.gif http://www.keil.com/pack/doc/mw/Network/html/using_network_sockets_bsd.html

Socket in C Stream Socket or Connection Oriented Socket Header files used #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>

Socket: Server side int sockt, newsockt; … sockt = socket(AF_INET, SOCK_STREAM, 0); Address Family: AF_INET or AF_UNIX Socket Type: SOCK_STREAM or SOCK_DGRAM Protocol: 0 for appropriate protocol selected by OS

If sockt > 0 Socket openning successful

struct sockaddr_in s_addr, c_addr; … struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; } AF_INET Not use

Initialize sockaddr_in bzero((char *) &s_addr, sizeof(s_addr)); bzero((char *) &c_addr, sizeof(c_addr));

Set sockaddr_in Must be s_addr.sin_family = AF_INET; s_addr.sin_port = htons(2048); s_addr.sin_addr.s_addr = INADDR_ANY Constant or Symbolic link Represent host’s IP address Convert to Networks Byte Order

Binding socket bind(sockt, (struct sockaddr *) &s_addr, sizeof(s_addr)); bind returns 0 on success returns -1 in case of fail

Waiting for connection listen(sockt, 5); Size of backlog queue Socket to be used for listening

Accept a remote connection int caddr_len = sizeof(c_addr); … newsockt = accept(sockt, (struct sockaddr *) &c_addr, &caddr_len); accept() blocks the process Returns -1 in case of fail Returns non-negative in case of success

Receive data via opened socket char buffer[256]; int n; … bzero(buffer, 256); n = read(newsockt, buffer, 255); read() blocks the process until there is something to read read() returns number of bytes read read() reads 255 bytes or less

Send data via opened socket int n; … n = write(newsockt, “Test 1234”, 9); - write() returns number of sent bytes

Close Socket close(newsockt); close(sockt);

Socket: Client side Library Ip address of remote host sys/types.h sys/socket.h netinet/in.h netdb.h or arpa/inet.h Ip address of remote host Is known -> inet_pton() is define in this lib. Structure hostent and gethostbyname() function are defined in this library

struct hostent. remoteServ … struct hostent { char. h_name; char struct hostent *remoteServ … struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0] }; Host name Alias name list Host address type Address length Address list from name server For backward compatibity

Get remote host by name struct sockaddr_in sock_serv; … remoteServ = gethostbyname(“www.kmitl.ac.th”); bzero((char *) &sock_serv, sizeof(sock_serv)); sock_serv.sin_family = AF_INET; bcopy((char *)remoteServ->h_addr, (char *) &sock_serv.sin_addr.s_addr, remoteServ->h_length); sock_serv.sin_port = htons(2048);

gethostbyname() return null if fail

IP of remote host is known inet_pton(int af, char *src, void *dst); Address family: AF_INET struct in_addr Or converted IP IP address in string form inet_pton returns 1 on success, returns 0 on failure

sock_serv. sin_family = AF_INET; sock_serv sock_serv.sin_family = AF_INET; sock_serv.sin_port = htons(2048); Inet_pton(AF_INET, “161.246.254.213”, &sock_serv.sin_addr);

Connect to remote host int sockHandle; … connect(sockHandle, &sock_serv, sizeof(sock_serv)); connect() returns 0 on success 1 on failure

Datagram Socket or Connectionless Socket int dsock; … dsock = socket(AF_INET, SOCK_DGRAM, 0)

Datagram receive data int n, remoteL; char buf[256]; struct sockaddr_in remoteS; … n = recvfrom(dsock,buf,256,0, &remoteS, &remoteL);

Datagram send data struct sockaddr_in serv; int servL; … n=sendto(dsock, buf, strlen(buf), 0, &serv, servL);

Socket in C# Library using System; using System.Net; using System.Net.Sockets;

Create Socket object Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Binding socket & Listen Stream Socket Binding socket & Listen IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 2048); … newsock.Bind(ipep); newsock.Listen(5);

Connect IPEndPoint ipep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 2048); … server.Connect(ipep);

Accept Socket client = newsock.Accept();

Send & Receive byte[] data = new byte[1024]; int recv; … string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); recv = client.Receive(data);

Close socket client.Shutdown(SocketShutdown.Both); client.Close(); newsock.Shutdown(SocketShutdown.Both); newsock.Close();

Datagram Socket Create Socket object: Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

Send data SendTo(byte[] data, EndPoint Remote) SendTo(byte[] data, SocketFlags Flags, EndPoint Remote) SendTo(byte[] data, int Size, SocketFlags Flags, EndPoint Remote) SendTo(byte[] data, int Offset, int Size, SocketFlags Flags, EndPoint Remote)

Example: byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 9050); … string welcome = "Hello, are you there?"; data = Encoding.ASCII.GetBytes(welcome); server.SendTo(data, data.Length, SocketFlags.None, (Endpoint) ipep);

Receive data ReceiveFrom(byte[] data, ref EndPoint Remote)

Example: int recv; byte[] data = new byte[1024]; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); … recv = newsock.ReceiveFrom(data, ref Remote);