SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.

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

Network Programming Chapter 11 Lecture 6. Networks.
Prepared By: Eng. Ola M. Abd El-Latif May/2010.  In this Lab we will answer the most frequently asked questions about programming sockets in Java. 
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Socket Programming.
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.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
1 © 2003, Cisco Systems, Inc. All rights reserved. CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Networking Support In Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Computer Networks Transport Layer Protocols. 2 Application-layer Protocols Application-layer protocols –one “piece” of an app –define messages exchanged.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Process-to-Process Delivery:
Socket Programming in Java -First Step of Network Programming-
Babak Esfandiari (based on slides by Qusay Mahmoud)
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.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
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.
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.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
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,
Chapter 2 Protocols and the TCP/IP Suite 1 Chapter 2 Protocols and the TCP/IP Suite.
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Socket Programming.
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.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
Socket Programming in Java -First Step of Network Programming-
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Java’s networking capabilities are declared by the classes and interfaces of package java.net, through which Java offers stream-based communications that.
1 Network Communications A Brief Introduction. 2 Network Communications.
Client-server communication Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
1 K. Salah Application Layer Module K. Salah Network layer duties.
Network Programming. These days almost all devices.
Socket Programming Ameera Almasoud
Introduction to Networks
Networking COMP
MCA – 405 Elective –I (A) Java Programming & Technology
Network Programming Introduction
Introduction to Networks
Process-to-Process Delivery:
Transport Protocols An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Process-to-Process Delivery: UDP, TCP
Transport Protocols Relates to Lab 5. An overview of the transport protocols of the TCP/IP protocol suite. Also, a short discussion of UDP.
Review Communication via paired sockets, one local and one remote
Presentation transcript:

SOCKET PROGRAMMING WITH JAVA By Collin Donaldson

Definitions  Network Socket: An endpoint of a two-way communication link between two programs running on a network.  Based on the Open-Read-Write-Close format first used in UNIX.  Socket Address: IP address + port number. Used so programs can identify connections and send packets to the appropriate process/thread  Similar to how one end of a telephone connection is identified by a phone number + extension.

Socket Life Cycle  Open-Read-Write-Close  Creation (Open Socket)  Reading and Writing (Receive and Send to Socket)  Destruction (Close Socket)

Socket Communication Protocols  Datagram Communication: User Diagram Protocol (UDP). A connectionless meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket's address. Additional data must be sent each time a communication is made.

Socket Programming Protocols (cont)  Stream Communication: Transfer Control Protocol (TCP). TCP is a connection-oriented protocol. In order to communicate over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions.

Which is better for Client/Server Apps?  No Connection Setup Time  64 kilobyte limit per location  Unreliable (packets could arrive out of order)  Connection Setup Time  No limit  Reliable (packets always arrive in order) UDP (Datagram)TCP (Stream) Connection Setup Time

In Summary  TCP is useful for implementing network services, such as remote login (rlogin, telnet) and file transfer (FTP), which require data of indefinite length to be transferred.  UDP is less complex and incurs fewer overheads. It is best for implementing client/server applications in distributed systems built over local area networks (LANs).

On to the tutorial!  Note! This tutorial only covers Stream/TCP since it is more common.  Start a new project and import the following  import java.io.*;  import java.net.*;  Note: We will be using the client-server model. Processes are partitioned between providers (servers) and requesters (clients)

Open Socket from Client  Socket MyClient;  try { MyClient = new Socket("Machine name", PortNumber); }  catch (IOException e) { System.out.println(e); }  When choosing port number, use on that is above 1,023. Numbers below 1,023 are reserved for privileged users (root/super user) and standard services. For example, HTTP is port number 80 and HTTPS is port number 443.

Open Socket from Server  ServerSocket MyService;  try {  (MyServerice = new ServerSocket(PortNumber);  }  catch (IOException e) {  System.out.println(e); }

Server side Socket Object that listens for and accepts connections from other users  Socket clientSocket = null;  try {  serviceSocket = MyService.accept();  }  catch (IOException e)  { System.out.println(e);  }

Client side Input Stream  DataInputStream input;  try {  input = newDataInputStream(MyClient.getInputStream());  } catch (IOException e)  {  System.out.println(e);  }

Sever Side Input Stream  DataInputStream input;  try { input = new DataInputStream(serviceSocket.getInputStream());  }  catch (IOException e) {  System.out.println(e);  }

Client side Output Stream using Data/PrintStream  DataOutputStream output;  try { output = new DataOutputStream(MyClient.getOutputStream()); }  catch (IOException e) {  System.out.println(e);  }  DataStream = primitives PrintStream = text

Server Side Output Stream using Data/PrintStream  PrintStream output;  try {  output = new PrintStream(serviceSocket.getOutputStream());  } catch (IOException e) {  System.out.println(e); }

Closing Sockets Client Side  try {  output.close();  input.close();  MyClient.close();  }  catch (IOException e)  {  System.out.println(e);  }

Closing Sockets Server Side  try {  output.close();  input.close();  serviceSocket.close();  MyService.close();  }  catch (IOException e) {  System.out.println(e);  }

Full Examples in Eclipse!  Examples include:  1. Simple Mail Transfer Protocol (SMTP) client  2. Echo Server (takes input, echoes it straight back as output to one client along one thread).  Note: It is likely that by default the SMTP will fail to recognize the host and the Echo Server will be blocked by your firewall  java/sockets-programming-in-java-a- tutorial.html?page=2 java/sockets-programming-in-java-a- tutorial.html?page=2