Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.

Slides:



Advertisements
Similar presentations
Socket Programming.
Advertisements

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.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
CSPP51037 – Advanced Java Lesson 1 Introduction/Warmup Java Socket Programming.
CSPP51037 – Advanced Java Lesson 1 Introduction/Warmup Java Socket Programming.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
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)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Introduction/Warmup Java Socket Programming.  Stream connecting processes running in different address spaces  Can be across a network or on the same.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
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.
Distributed Computing A Programmer’s Perspective.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
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.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
Advance Computer Programming Networking Basics – explores the java.net package which provides support for networking. – Also Called “programming for the.
Network Programming. These days almost all devices.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Object-Orientated Analysis, Design and Programming
Lecture 21 Sockets 1 (Not in D&D) Date.
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
CSCD 330 Network Programming
Sockets and URLs 13-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
Presentation transcript:

Basic Socket Programming with Java

What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across a network or on the same machine).  We say “ create a socket connection between machine A and machine B ”. This means, roughly, create input and output streams for sending data between programs running simultaneously on each machine.  The programs can then “ talk to each other ”.  This is lowest-level form of communication from application developer ’ s view

Sockets, cont.  Sockets represent a low-level abstraction for application communication. –Programmer is aware of a stream that connects two computers. –Programmer fully responible for managing/interpreting flow of bytes between computers  Higher-level techniques –message passing systems (MPI, SOAP, JMS), extensions to web servers (ASP, JSP, servelets, etc), distributed objects (CORBA, RMI), web services, etc.

More about sockets in Java  One of the good things about Java  Supported natively by the standard languages (j2sdk)  Distinction between high and low-level blurred somewhat by ability to wrap streams (ObjectOutputStream, etc.)  Still, socket programming differs from other distributed programming in its low-level nature.

Why is this paradigm useful?  Shared resources (web servers, ftp servers, mail servers)  Online auctions, exchanges, etc.  Data locality  Localize computing power  Crash protection  Software maintainability

Conceptual overview of basic client-server program  Write a program that dials up another program at a specified IP address running on a specified port. Call this program the client.  Second program (server) accepts connection and establishes input/output stream to client.  When server accepts, client can establish input/ouput stream to server  Client makes request of server by sending data. Server sends replies to client. Protocol must be defined so client/server understand can interpret messages.

Conceptual overview of basic peer-to-peer program  Two processes running on specific port of specific machine.  Either process can dial up the other process.  When connection is established, applications talk at a peer level, rather than one making requests and the other serving up those requests.  Will see many examples soon.

Socket Machinery in Java

Java classes for direct socket programming  Good news: This is very simple in Java  Really only 3 additional classes are needed  java.net.InetAddress java.net.InetAddress  java.net.Socket java.net.Socket  java.net.ServerSocket java.net.ServerSocket

Most important classes/methods  java.net.Socket –Socket(InetAddress addr, int port); create a Socket connection to address addr on port port –InputStream getInputStream(); returns an instance of InputStream for getting info from the implicit Socket object –OutputStream getOutputStream(); returns an instance of OutputStream for sending info to implicit Socket object. –close(); close connection to implicit socket object, cleaning up resources.

Important classes, cont.  java.net.ServerSocket –ServerSocket(int port); enables program to listen for connections on port port –Socket accept(); blocks until connection is requested via Socket request from some other process. When connection is established, an instance of Socket is returned for establishing communication streams.

Important class, cont.  java.net.InetAddress –static InetAddress getByName(String name) given a hostname name, return the InetAddress object representing that name (basically encapsulates name and IP associated with name); –static InetAddress[] getAllByName(String name) same as above but for case where many ip ’ s mapped to single name (try e.g.). –static InetAddress getLocalHost() get InetAddress object associated with local host. –static InetAddress getByAddress(byte[] addr) get InetAddress object associated with address addr

Error Handling  Very important to ensure that server is robust and will not crash.  Important Exceptions: –InterruptedIOException –ConnectException  Be sure to close your sockets either after a crash or upon expected completion. Finally clause is useful here.

Examples  Best way to learn this is to study several canonical examples  See many simple course examples under standaloneClient package  Next, do simple EchoServer  Then, Threaded EchoServer  Then, fully synchronized tic-tac-toe  Then, chess game