UNIX Socket Programming CS 6378

Slides:



Advertisements
Similar presentations
Socket Programming 101 Vivek Ramachandran.
Advertisements

Sockets: Network IPC Internet Socket UNIX Domain Socket.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
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.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
Network Programming UNIX Internet Socket API. Everything in Unix is a File –When Unix programs do any sort of I/O, they do it by reading or writing to.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
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.
Ashutosh Shukla Lecture 3 Network Programming CS 640: Computer Networking.
1 Programming with TCP/IP Ram Dantu. 2 Client Server Computing r Although the Internet provides a basic communication service, the protocol software cannot.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
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.
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.
---- IT Acumens. COM IT Acumens. COMIT Acumens. COM.
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)
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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
Socket Programming Lab 1 1CS Computer Networks.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Introduction to Sockets
UNIX Internet Socket API
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 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Berkeley Sockets. Client-server model Sockets interface: Address structure, byte ordering, port no Socket primitives: socket, bind,listen… Example code.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Network Programming with Sockets
Tutorial on Socket Programming
Transport layer API: Socket Programming
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
UNIX Sockets Outline Homework #1 posted by end of day
Socket Programming in C
TCP/IP Socket Programming in C
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Presentation transcript:

UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> UNIX System >> Sockets >> System Calls >> TCP Client Server >> UDP Client Server >> Threads

User and System Programs Kernel Support Hardware

Socket API Socket API originated with the 4.2 BSD system released in 1983 Sockets – A way to speak to other programs using UNIX file descriptors. A file descriptor is an integer associated with an open file.This can be a network connection Kinds of Sockets-DARPA Internet addresses(Internet Sockets) , Unix Sockets, X.25 Sockets etc Types of Internet Sockets SOCK_STREAM uses TCP (Transmission Control Protocol) Connection oriented and Reliable SOCK_DGRAM uses UDP (User Datagram Protocol) Connectionless and Unreliable

Structs and Data Handling A socket descriptor is of type int Byte ordering Most significant byte first – Network byte order (Big Endian) Least significant byte first – Host Byte order ( Little ..) Socket Structures in Network byte order struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address }; struct sockaddr_in { short int sin_family; // Address family unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // Same size as struct sockaddr };

Convert the Natives struct in_addr { unsigned long s_addr; // 32-bit long, or 4 bytes }; If ina is of type struct sockaddr_in ina.sin_addr.s_addr references the 4-byte IP address (in Network Byte Order htons() – Host to Network Short htonl() -- "Host to Network Long" ntohs() -- "Network to Host Short" ntohl() -- "Network to Host Long"

IP Addresses socket01.utdallas.edu 129.110.43.11 sol2.utdallas.edu 129.110.34.2 etc Other UTD machines for use socket02 – socket06 , sol1 , jupiter Please do not use apache for Network programming inet_addr() converts an IP address in numbers-and-dots notation into unsigned long ina.sin_addr.s_addr = inet_addr(“129.110.43.11”) // Network byte order Also can use inet_aton() -- “ascii to network” int inet_aton(const char *cp,struct in_addr *inp); inet_ntoa returns a string from a struct of type in_addr inet_ntoa(ina.sin_addr) ;

Useful UNIX Commands netstat –i prints information about the interfaces netstat –ni prints this information using numeric addresses loop back interface is called lo and the ethernet interface is called eth0 or le0 depending on the machine netstat –r prints the routing table netstat | grep PORT_NO shows the state of the client socket ifconfig eth0 – Given the interface name ifconfig gives the details for each interface --- Ethernet Addr , inet_addr , Bcast , Mask , MTU ping IP_addr -- Sends a packet to the host specified by IP_addr and prints out the roundtrip time ( Uses ICMP messages) traceroute IP_addr -- Shows the path from this host to the destination printing out the roundtrip time for a packet to each hop in between Tcpdump communicates directly with Data Link layer UDP Packet fail

System Calls socket() – returns a socket descriptor int socket(int domain, int type, int protocol); bind() – What port I am on / what port to attach to int bind(int sockfd, struct sockaddr *my_addr, int addrlen); connect() – Connect to a remote host int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); listen() – Waiting for someone to connect to my port int listen(int sockfd, int backlog); accept() – Get a file descriptor for a incomming connection int accept(int sockfd, void *addr, int *addrlen); send() and recv() – Send and receive data over a connection int send(int sockfd, const void *msg, int len, int flags); int recv(int sockfd, void *buf, int len, unsigned int flags);

sendto() and recvfrom() – Send and receive data without connection int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); close() and shutdown() – Close a connection Two way / One way getpeername() – Obtain the peer name given the socket file descriptor gethostname() – My computer name int sock_get_port(const struct sockaddr *sockaddr,socklen_t addrlen); Useful to get the port number given a struct of type sockaddr Readn() writen() readline() Read / Write a particular number of bytes Fork() – To start a new process with parents addr space Exec() Load a new program on callers addr space

Client-Server Programming TCP Client Server Example Status of client-server before call to accept daytimetcpsrv1.c Server Client ● connect() listenfd ● Status of clientserver after return from accept ● connect() listenfd ● connfd ●

Status of Client-Server after fork returns Server( Parent) listenfd ● connfd ● Client ● connect() Server ( Child) listenfd● connfd ●

Status of Client Server after parent and Child close appropriate sockets Server ( Parent) listenfd ● Client ● connect() Server(Client) connfd ●

Socket functions of UDP Client Server Bind() Recvfrom() Socket() Sendto() Blocks until datagram received from a client recvfrom Sendto() Close() UDP Client UDP Server

Threads Threads are lightweight process which share the process instructions , global variables , open files , signal handlers and signal dispositions , current working directory and user and group Ids Each thread has its own thread ID , set of Registers, PC and Stack pointer, stack , errno ,signal mask , priority Basic Thread Functions : Creation and Termination Int pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(*func)(void*), void *arg); Int pthread_join(pthread_t tid,void **status); // Wait for a thread to terminate pthread_t pthread_self(void); //Returns thread ID of calling thread Int pthread_detach(pthread_t pid);//We cannot wait for it to terminate, On its termination all resources are released Void pthread_exit(void *status); Threads/tcpserv01.c

Thread Synchronization Shared data can be protected using mutex locks provided by the pthread library int pthread_mutex_lock(pthread_mutex_t *mptr); int pthread_mutex_unlock(pthread_mutex_t *mptr); Threads/example02.c Can use condition variables for signalling mechanism int pthread_cond_wait(pthread_cond_t *cptr,pthread_mutex_t *mptr); int pthread_cond_signal(pthread_cond_t *cptr); Can also use System semaphores from <semaphores.h> See manual ~syrotiuk/pthreads.cc

References UNIX Network Programming , Networking APIs: Sockets and XTI : W Richard Stevens Beej’s Guide to Network Programming www.ecst.csuchico.edu/~beej/guide/net/ Dr Syrotiuk’s web page with sample networking programs www.utdallas.edu/~syrotiuk/cs6378/work.html