In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.

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.
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.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
CSCE 515: Computer Network Programming Socket Wenyuan Xu Department of Computer Science and Engineering.
Tutorial 8 Socket Programming
CS 311 – Lecture 19 Outline Internet Sockets – gethostname utility – struct hostent – inet_addr – Machine byte to Network byte order translation and vice.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
ISP – 9 th Recitation Socket programming – Client side.
Introduction to Project 1 Web Client and Server Jan 2006.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
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.
Technical Details for sockaddr_in Structure Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
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,
Socket address structures Byte ordering IP address conversion
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
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.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
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.
Lecture 3 TCP and UDP Sockets 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.
Client-Server model. Socket programming 
Assignment 3 A Client/Server Application: Chatroom
Socket programming Péter Verhás August 2002
Socket Programming (Cont.)
Network Programming CSC- 341
Chapter4 Elementary TCP Socket
Network Programming with Sockets
Tutorial on Socket Programming
Sockets.
Transport layer API: Socket Programming
Things that are nice to know when you’re doing this project
תקשורת ומחשוב תרגול 3-5 סוקטים ב-C.
ECS152b Behrooz Khorashadi
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Outline Communications in Distributed Systems Socket Programming
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

in unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.

in netdb.h, and needing sys/socket.h : struct hostent { char * h_name; // the official name char * * h_aliases; // list of alternate names, NULL at end int h_addrtype; // what kind of address: expect AF_INET int h_length; // bytes in each address: expect 4 char * h_addr; // primary address - not a string char * * h_addr_list; // all addresses, not strings, NULL at end }; in netdb.h : hostent * gethostbyname(char * name);

needs sys/types.h and sys/socket.h int socket(int domain, int type, int protocol) Create a file-like object capable of internet communications domain = AF_INET if you want to use IP, there are many others. type = SOCK_STREAM if you want TCP, or SOCK_DGRAM if you want UDP, there are a few others protocol = 0, except in rare circumstances 0 means use the default protocol for this type. for SOCK_STREAM, that should be TCP, for SOCK_DGRAM, it should be UDP, but you can be explicit and say IPPROTO_TCP or IPPROTO_UDP, they are defined in netinet/in.h

in netinet/in.h struct sockaddr_in { char sin_len; // total size of the struct in bytes char sin_family; // usually AF_INET short int sin_port; // port number, in network format, use htons in_addr sin_addr; // IP address for the connection char sin_zero[8]; // unused. }; typedef uint32_t in_addr_t; // uint32_t is elsewhere defined as unsigned int struct in_addr { in_addr_t s_addr; }; so an in_addr is a four byte struct that contains just an int

so for a client struct sockaddr_in server_address; server_address.sin_len = sizeof(server_address); server_address.sin_family = AF_INET; server_address.sin_port = htons(portnumber); server_address.sin_addr.s_addr = the address from a hostent, or htonl( ipaddress ); and for a server struct sockaddr_in server_address; server_address.sin_len = sizeof(server_address); server_address.sin_family = AF_INET; server_address.sin_port = htons(portnumber); server_address.sin_addr.s_addr = htonl(INADDR_ANY);

Also only for servers int bind(int socket, struct sockaddr * description, int size) socket = a socket as created by socket() description = a pointer to a sockaddr_in, but typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in int listen(int socket, int queue_length) socket = the socket that was just bound to a port queue_length = keep it small These are for servers only. listen fails int bind(int socket, sockaddr * description, int size) socket = a socket as created by socket() description = a pointer to a sockaddr_in, but typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in if the desired port number is already in use.

Also only for servers int accept(int socket, struct sockaddr * client, int size) socket = a socket that listen() has just been performed on description = a pointer to another sockaddr_in, typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in Waits until a client makes a connection. The sockaddr_in called client tells you the IP address and port number of that client. The result is a new socket that is to be used to communicate with the client, or an error value. The error EINTR is not really an error, just wait and try again. When you are finished with the client, just close the new socket. The original remains to accept connections from other clients.

For clients int connect(int socket, struct sockaddr * client, int size) socket = a newly made socket description = a pointer to a sockaddr_in, typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in A non-error result means you are connected. Use socket as a file to communicate with the server. close() it to terminate the connection.

from struct pollfd { int fd; // a file that you’d like to be informed about int events; // the events the you’d like to be informed of when they happen to that file int revents; // set to zero, this is where you get the results The only event that you’re really likely to care about is POLLRDNORM - It is now possible do read input from this file: some has arrived. If a connection is closed, you still get this event, but when you do the read, you receive nothing. Just like reaching the end of a normal file Other events can be combined with the | operator, you can see them all in poll.h, they include POLLWRNORM - It is now possible do write to this file:it usually is. POLLERR - An error has occurred int poll(struct pollfd Array[], int length_of_array, int milliseconds); Array contains as many pollfd objects as you want. The function waits until at least one of the events you specified occurs, or for the given number of milliseconds, whichever happens first. Result negative for error, 0 if time limit expired.

If you want to read from file a or file b, whichever is ready first: struct pollfd interesting[2]; interesting[0].fd = a ; interesting[0].events = POLLRDNORM | POLLERR; interesting[0].revents = 0; interesting[1].fd = b; interesting[1].events = POLLRDNORM | POLLERR; interesting[1].revents = 0; r = poll(interesting, 2, 1000); if (r < 0) { perror("poll"); sleep(1); } if (interesting[0].revents & POLLRDNORM) // you can now read from file a if (interesting[0].revents & POLLRDNORM) // you can now read from file b

char * inet_ntoa(struct in_addr address) converts a 4 byte IP address into readable form. If you’ve got a sockaddr_in called si, then use inet_ntoa(si.sin_addr) If you’ve got a hostent * called hep, then use inet_ntoa(* (struct in_addr *) hep->h_addr)