ZSocket address structure in : struct sockaddr{ u_shortsa_family;/* address family: AF_xxx value */ charsa_data[14];/* up to 14 bytes of protocol- */ /*

Slides:



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

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Socket Programming Application Programming Interface.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Computer Networks Sockets. Outline F Socket basics F Socket details.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
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.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
CS 6401 Introduction to Computer Networks 09/21/2010 Outline - UNIX sockets - A simple client-server program - Project 1 - LAN bridges and learning.
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,
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
Introduction to Sockets
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()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
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.
Elementary UDP Sockets
Socket Programming in C
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
UDP Sockets Programming
Socket Programming in C
TCP/IP Socket Programming in C
Internet Networking recitation #8
Presentation transcript:

zSocket address structure in : struct sockaddr{ u_shortsa_family;/* address family: AF_xxx value */ charsa_data[14];/* up to 14 bytes of protocol- */ /* specific address */ } Socket Programming

zSocket address structure in : (Internet family) struct in_addr{ u_longs_addr;/* 32-bit netid/hostid */ /* network byte ordered */ } Socket Programming

struct sockaddr_in{ shortsin_family;/* AF_INET */ u_shortsin_port;/* 16-bit port number */ /* network byte ordered */ struct in_addr sin_addr;/* 32-bit netid/hostid */ /* network byte ordered */ charsin_zero[8];/* unused */ } Socket Programming

zsocket : specify the type of communication protocol desired (TCP, UDP etc.) #include int socket(int family, int type, int protocol); family:AF_UNIX (UNIX internal protocol) AF_INET (Internet protocols) AF_NS (Xerox NS protocols) AF_IMPLINK (IMP link layer) (AF: address family) Socket Programming

type:SOCK_STREAM (stream socket, TCP) SOCK_DGRAM (datagram socket, UDP) SOCK_RAW (raw socket) SOCK_SEQPACKET (sequenced packet socket) SOCK_RDM (reliably delivered message socket) (not implemented yet) protocol: typically set to 0 for most user applications The output of the system call is the socket descriptor. Socket Programming

zbind: assign a name to an unnamed socket #include int bind(int sockfd, struct sockaddr *myaddr, int addrlen); sockfd: socket descriptor (return by*myaddr: a pointer to a protocol-specific address addrlen: the size of this address structure The output > 0 if success; otherwise, it fails. Socket Programming

zconnect: establish a connection with a server #include int connect (int sockfd, struct sockaddr *servaddr, int addrlen); same as the system call bind. The output > 0 if success; otherwise, it fails. Socket Programming

zlisten: is used by a connection-oriented server to indicate that it is willing to receive connections #include int listen (int sockfd, struct backlog); backlog: specify how many connection requests can be queued by the system while it waits for the server to execute the accept system call (default as 5). The output > 0 if success; otherwise, it fails. Socket Programming

zaccept: take the first connection request on the queue and create another socket with the same properties as sockfd. #include int accept (int sockfd, struct sockaddr *peer, int *addrlen); peer: the address of connected peer process (the client). addrlen: the size of peer. The output of the system call is the socket descriptor. Socket Programming

zsend, sendto, recv and recvform #include int send(int sockfd, char *buff, int nbytes, int flags); int sendto(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); int recv(int sockfd, char *buff, int nbytes, int flags); int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); flag: usually zero Socket Programming

zclose: close a socket #include int close (int fd); zbzero: writes the specified number of null bytes to the specified destination. bzero(char *dest, int nbytes); Socket Programming

zhtonl: convert host-to-network to long integer zhtons: convert host-to-network to short integer #include u_long htonl(u_long hostlong); u_short htons(u_long hostshort); Socket Programming

zinet_addr: convert a character string in dotted- decimal notation to a 32-bit Internet address #include unsigned long inet_addr(char *ptr); Socket Programming

read ( ) connect ( ) socket ( ) write ( ) socket ( ) bind ( ) listen ( ) read ( ) accept ( ) write ( ) blocks until connection from client process request connection establishment data (request) data (reply) Connection-oriented Protocol Server Client Socket Programming

Connectionless Protocol Server Client recvfrom ( ) bind ( ) socket ( ) sendto ( ) data (request) data (reply) socket ( ) bind ( ) recvfrom ( ) sendto ( ) blocks until data received from a client process request Socket Programming

int tcp_serv() /* server program: setup a TCP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_dump(“server: can't open stream socket”); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* INADDR_ANY: any Internet interface on the system */ Socket Programming

serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_dump(“server: can't bind local address”); listen(socfd, 5); clilen = sizeof(cli_addr); if ((newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen)) < 0) err_dump(“server: accept error”); } Socket Programming

int tcp_client() /* client program: setup a TCP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in serv_addr; bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); serv_addr.sin_port = htons(SERV_TCP_PORT); Socket Programming

if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_sys(“client: can't open stream socket”); if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_sys(“client: can't bind local address”); /* do the transmission */ close(sockfd); } Socket Programming

tcp_send(s_act, msg_ptr, msg_len) /* ipc_send: send data message “msg” within stream domain */ int s_act;/* actual socket descriptor of connection */ char *msg_ptr;/* pointer to data msg unit */ int msg_len;/* length of data msg unit */ { int cc = 0, rc; cc = send(s_act, msg_ptr, msg_len, 0); /*UNIX system call */ if (cc == -1) {perror("send: ! "); rc = cc; } /* error report */ else rc = OK (=1); return (rc); } Socket Programming

tcp_receive(s_act, buf_ptr, buf_len) /* receive data within UNIX*/ int s_act;/* socket descriptor of ipc connection */ union u_du *buf_ptr; /* pointer to message buffer */ int *buf_len; /* length of message buffer */ { int cc, rc; cc = recv(s_act, buf_ptr, *buf_len, 0); /* UNIX syst call */ if (cc == -1) { perror("recv: "); rc = cc; } else {*buf_len = cc; rc = OK (= 1); } return (rc); } Socket Programming

int udp_serv() /* server program: setup a UDP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) err_dump(“server: can't open datagram socket”); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* INADDR_ANY: any Internet interface on the system */ Socket Programming

serv_addr.sin_port = htons(SERV_UDP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_dump(“server: can't bind local address”); /* do the transmission */ } Socket Programming

int udp_client() /* client program: setup a UDP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); serv_addr.sin_port = htons(SERV_UDP_PORT); if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) err_dump(“client: can't open datagram socket”); Socket Programming

bzero((char *) &cli_addr, sizeof(cli_addr)); cli_addr.sin_family = AF_INET; cli_addr.sin_addr.s_addr = htonl(INADDR_ANY); cli_addr.sin_port = htons(0); if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) err_dump(“client: can't bind local address”); /* do the transmission */ close(sockfd); } Socket Programming

udp_send( msg_ptr, msg_len, addr_ptr, addr_len ) char *msg_ptr;/* pointer to message */ int msg_len; /* length of message */ struct sockaddr_in *addr_ptr; /* socket address */ int addr_len; /* length of socket address */ { int cc; addr_ptr.sin_addr.s_addr = htonl(st_addr); /*convert address*/ cc = sendto(s_udp, msg_ptr, msg_len, 0 addr_ptr, addr_len ); if (cc == -1) {perror("sendto: "); return(cc);} else return(OK); } Socket Programming

udp_receive(msg_ptr, msg_len, addr_ptr, addr_len) char *msg_ptr; /* pointer to message “msg” */ int msg_len; /* length of message */ struct sockaddr_in *addr_ptr; /* socket address */ int addr_len; /* length of socket address */ { int cc; cc = recvfrom(s_udp, msg_ptr, *msg_len, 0, addr_ptr, addr_len ); if (cc == -1) {perror("recvfrom: "); return(cc); } else return(OK); } Socket Programming

z#define _REENTRANT z#include z#define NUM_THREADS 12 zvoid *change_global_data(void *); /* for thr_create() */ z main(int argc,char * argv[]) { z int i=0; z for (i=0; i< NUM_THREADS; i++) { z thr_create(NULL, 0, change_global_data, NULL, 0, NULL); z } z while ((thr_join(NULL, NULL, NULL) == 0)); z} zvoid * change_global_data(void *null) { z static mutex_t Global_mutex; z static int Global_data = 0; z mutex_lock(&Global_mutex); z Global_data++; z sleep(1); z printf("%d is global data\n",Global_data); z mutex_unlock(&Global_mutex); z return NULL; z} Thread Programming (Try the program) Use online help to find out the information about thread programming. Try: man mutex man thr_create man thr_join …...