CSI 4118 – UNIVERSITY OF OTTAWA

Slides:



Advertisements
Similar presentations
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Advertisements

Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
1 Netcomm Sockets Communication Networks Recitation 1.
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
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
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.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
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.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
Chapter 24 - Socket Interface Introduction API The Socket API Sockets and socket libraries Sockets and UNIX I/O The socket API Summary of socket system.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
FALL 2003CSI 4118 – UNIVERSITY OF OTTAWA1 Part 3.1 Internet Applications Ch. 28,… (Client-Server Concept, Use of Protocol Ports, Socket API, DNS, ,
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
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,
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 3.1 Internet Applications Ch. 28,… (Client-Server Concept, Use of Protocol Ports, Socket API)
Chapter 27 Socket API Interface The interface between an application program and the communication protocols in an operating system is known as the Application.
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
Part 4: Network Applications Client-server interaction, example applications.
Socket Programming.
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,
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
CPSC Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming Ameera Almasoud
Socket Programming Client/Server.
The Pocket Guide to TCP/IP Sockets: C Version
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming with Sockets
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Interacting With Protocol Software
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
CSI 4118 – UNIVERSITY OF OTTAWA
Lecture 2 Socket Programming
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
CSI 4118 – UNIVERSITY OF OTTAWA
Socket Programming with UDP
Presentation transcript:

CSI 4118 – UNIVERSITY OF OTTAWA Part 3.1(supp) Java Socket Details (supplemental materials) (Client-Server Concept, Use of Protocol Ports, Socket API) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA Creating a Socket Application calls socket function OS returns descriptor for socket Descriptor valid until application closes socket or exits Common: protofamily = PF_INET, type = SOCK_STREAM or SOCK_DGRAM In Java, to create a client socket: Socket socket = new Socket(string host, int port) To create a server socket: ServerSocket sSocket = new ServerSocket(int port) desc = socket(protofamily,type,proto); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA Socket Functionality Socket completely general Can be used By client By server With a CO transport protocol With a CL transport protocol To send data, receive data, or both Large set of operations FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA Socket Operations Close Terminate use of socket Permanent In Java, use socket.close(); or sSocket.close(); close(socket); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA Socket Operations Bind Specify protocol port for a socket Specify local IP address for a socket Can use INADDR_ANY for any IP address In Java, use socket.bind(SocketAddress endpoint) to bind the socket to a IP address and port. bind(socket,localaddr,addrlen); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Generic Address Format struct sockaddr { u_char sa_len; /*length of address*/ u_char sa_family; /*family of address*/ char sa_data[14]; /*address itself*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA TCP/IP Address Format struct sockaddr_in { u_char sin_len; /*length of address*/ u_char sin_family; /*family of address*/ u_short sin_port; /*protocol port number*/ struct in_addr sin_addr; /*IP address*/ char sin_zero[8]; /*not used(set to zero)*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Socket Operations (continued) Listen Used by server Prepares socket to accept incoming connections Accept Waits for next connection and returns new socket In Java, method accept() in java.net.ServerSocket class listens for a connection and accepts it. listen(socket,queuesize); newsock = accept(socket,caddr,caddrlen); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Socket Operations (continued) Connect Used by client Either Performs a TCP connection Fully specifies addresses for UDP In Java, connect(SocketAddress server) to connect the socket to a specific server. connect(socket,saddr,saddrlen); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Two Purposes of the Connect Function The connect function, which is called by clients, has two uses. With connection-oriented transport, connect establishes a transport connection to a specified server. With connectionless transport, connect records the server’s address in the socket, allowing the client to send many messages to the same server without specifying the destination address with each message. FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Socket Operations (continued) Send, sendto, and sndmsg Transfer outgoing data from application In java, java.io.PrintStream is used to send data. e.g. method println() send(socket,data,length,flags); sendto(socket,data,length,flags, destaddr,addrlen); sendmsg(socket,msgstruct,flags); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA Format of msgstruct struct msgstruct { struct sockaddr *m_saddr; /*dest address*/ struct datavec *m_dvec; /*message (vector)*/ int mdvlength; /*size of vector*/ struct access *m_rights; /*access rights*/ int m_alength; /*size of access rights*/ } FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Socket Operations (continued) Recv, recvfrom, and recvmsg Transfer incoming data to application In Java, java.io.BufferReader used to receive data. E.g. method readline() recv(socket,buffer,length,flags); recvfrom(socket,buffer,length,flags, senderaddr,saddrlen); recvmsg(socket,msgstruct,flags); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Java Code Equivalent to Example in Chapter 30 FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Example Server Java Code import java.io.*; import java.net.*; public class server{ public static void main(String[] args) { // variable declaration int visits = 0; ServerSocket srv = null; PrintStream os; Socket clientSocket = null; String msg = ""; //check command-line argument ... //(omited) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA /* create socket */ try { srv = new ServerSocket(Integer.parseInt(args[0])); } catch (IOException e) { System.out.println(e); /* Main server loop - accept and handle requests */ while(true){ try {// listen for a connection from client and accept it. clientSocket = srv.accept(); catch(IOException e) { System.out.println("accept failed"); System.exit(1); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA try{ os = new PrintStream(clientSocket.getOutputStream()); visits++; msg = "This server has been contacted " + visits + " times"; os.println(msg); //sends msg os.close(); clientSocket.close(); } catch(IOException e) { System.out.println(e); FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

Example Client Java Code import java.io.*; import java.net.*; public class client { public static void main(String[] args) //variable declaration Socket socket = null; BufferedReader in = null; //check command-line argument... //(omited) FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA

CSI 4118 – UNIVERSITY OF OTTAWA try { /* create a socket and connect to the specified server. */ socket = new Socket(args[0], Integer.parseInt(args[1])); } catch (UnknownHostException e) { System.exit(1); } catch (IOException e) { System.out.println("connect failed"); } /* read data from socket and write to user's screen. */ try{ in = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(in.readLine()); in.close(); socket.close(); catch(IOException e) { System.out.println(e); }}} FALL 2005 CSI 4118 – UNIVERSITY OF OTTAWA