NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.

Slides:



Advertisements
Similar presentations
NET0183 Networks and Communications Lecture 28 TCP: a transport layer protocol... the story continues... Sagan halda áfram 8/25/20091 NET0183 Networks.
Advertisements

NET0183 Networks and Communications Lecture 23 UDP: a transport layer protocol 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture.
Sockets For Servers Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Generic Transport Service Primitives Listen –notify Transport layer a call is expected Connect –establish Transport layer connection Send (or Write) Receive.
Socket Programming.
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.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Generic Transport Service Primitives Listen –notify Transport layer a call is expected Connect –establish Transport layer connection Send (or Write)
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
I NTRODUCTION OF S OCKET P ROGRAMMING L.Aseel AlTurki King Saud University.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
2: Application Layer 1 Socket Programming TCP and UDP.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
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,
Java Server Programming Web Interface for Java Programs.
Part 4: Network Applications Client-server interaction, example applications.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets, 5e By Douglas E. Comer Lecture PowerPoints.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Java 13. Networking public class SumTest {
Object-Orientated Analysis, Design and Programming
Socket Programming Client/Server.
Echo Networking COMP
Networking COMP
Chapter 3 Internet Applications and Network Programming
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Socket Interface 1 Introduction 11 Socket address formats 2 API 12 13
Socket Programming.
Presentation transcript:

NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from the recommended textbook are by Lami Kaya, Lecture powerpoints are © 2009 Pearson Education Inc. Their content has sometimes been edited by Andy Brooks. socket/tengill

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 2 The recommended textbook is Computer Networks and Internets by Douglas E. Comer (for additional discounts and offers)

Webopedia 31. mars /25/2009 NET0183 Networks and Communications by Dr Andy Brooks 3 “(1) In UNIX and some other operating systems, a software object that connects an application to a network protocol. In UNIX, for example, a program can send and receive TCP/IP messages by opening a socket and reading and writing data to and from the socket. This simplifies program development because the programmer need only worry about manipulating the socket and can rely on the operating system to actually transport messages across the network correctly. Note that a socket in this sense is completely soft - it's a software object, not a physical component.”

4 Socket: a door between an application process and a transport protocol (e.g. TCP) process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet from Kurose and Ross

© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved Network Programming and the Socket API The interface an application uses to specify communication is known as an Application Program Interface (API). One particular API has emerged as the de facto standard for software that communicates over the Internet. This API is known as the socket API, commonly abbreviated sockets. The socket API is available for many OS such as Microsoft's Windows systems as well as various UNIX systems, including Linux. sockets/tenglar

© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved Parameters and the Socket API Socket programming differs from conventional I/O. An application must specify details such as : –the address of a remote computer –the protocol port number –whether the application will act as a client or as a server To avoid having a single socket function with many parameters, designers of the socket API chose to define many functions. An application creates a socket, and then invokes functions.

© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.7 Figure 3.7 A summary of the major functions in the socket API.

© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved Socket Calls in a Client and Server Figure 3.8 illustrates the sequence of socket calls made by a typical client and server that use a stream connection. –The server waits for the client to send data. In practice, some applications arrange for the server to send first (i.e. send and recv are called in the reverse order).

© 2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.9 Figure 3.8 Illustration of the sequence of socket functions called by a client and server using the stream paradigm.

10 How does this all work in Java? A brief overview follows... 8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks

Simple, example client-server in Java. 11 AdditionClient.java RunAdditionClient.java AdditionServer.java RunAdditionServer.java The source code files are on NET0183 website. 8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks The client sends two numbers to the server. The server computes the addition and sends the result back to the client. Window dialogues are used to report behaviour to let us see what is happening.

Socket and ServerSocket classes in Java Socket knows about: – opening a connection – sending data – receiving data – closing a connection ServerSocket knows about: – binding to a port – listening for incoming data – accepting connections 8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 12

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 13 import java.net.*; import java.io.*; import javax.swing.*; public class AdditionServer extends JFrame { private JTextArea textWindow = new JTextArea(); private int port; // the constructor public AdditionServer(int portIn) { port = portIn; setTitle("Addition Server"); add("Center",textWindow); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); startServer(); } AdditionServer.java

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 14 AdditionServer.java private void startServer() { // declare a "general" socket and a server socket Socket connection; ServerSocket listenSocket; // declare low level and high level objects for input InputStream inStream; DataInputStream inDataStream; // declare low level and high level objects for output OutputStream outStream; DataOutputStream outDataStream; // declare other variables String client; int first, second, sum; boolean connected;

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 15 AdditionServer.java while(true) { try { // create a server socket listenSocket = new ServerSocket(port); textWindow.append("Listening on port " + port + "\n"); // listen for a connection from the client connection = listenSocket.accept (); connected = true; // create an input stream from the client inStream = connection.getInputStream(); inDataStream = new DataInputStream(inStream); // create an output stream to the client outStream = connection.getOutputStream (); outDataStream = new DataOutputStream (outStream ); // wait for a string from the client client = inDataStream.readUTF(); textWindow.append("Connection established with " + client + "\n" );

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 16 AdditionServer.java while(connected) { //read an integer from the client first = inDataStream.readInt(); textWindow.append( "First number received: " + first + "\n"); //read an integer from the client second = inDataStream.readInt(); textWindow.append( "Second number received: " + second + "\n"); sum = first + second; textWindow.append( "Sum returned: " + sum + "\n"); //send the sum to the client outDataStream.writeInt(sum); } catch (IOException e) { connected = false; }}}}

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 17 RunAdditionServer.java public class RunAdditionServer { /** args */ public static void main(String[] args) { new AdditionServer(8901); }

8/25/2009 NET0183 Networks and Communications by Dr Andy Brooks 18 Simple, example client-server in Java. Extracts from the Java API. listenSocket = new ServerSocket(port); – “Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port. The maximum queue length for incoming connection indications (a request to connect) is set to 50. If a connection indication arrives when the queue is full, the connection is refused. “ connection = listenSocket.accept (); – “Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.”