Socket Programming in Java -First Step of Network Programming-

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

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.
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. 
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.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
Networking with Java. Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
Socket Programming in Java -First Step of Network Programming-
Babak Esfandiari (based on slides by Qusay Mahmoud)
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
DBI Representation and Management of Data on the Internet.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
 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.
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.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
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 Sockets Tutorial Rahul Malik Nov 12, 2005.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
Field Trip #25 Creating a Client/Server By Keith Lynn.
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.
Java Programming II Java Network (I) Java Programming II.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
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.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
SOCKET PROGRAMMING.
Socket Programming Ameera Almasoud
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking COMP
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.
Socket Programming Cal Poly Pomona Young CS380.
An Introduction to TCP/IP and Sockets
Network Programming Introduction
CSCD 330 Network Programming
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Socket Programming in Java -First Step of Network Programming-

BufferedReader BufferedReader BufferedReader BufferedReader in = new BufferedReader( new InputStreamReader( )); BufferedReader in = new BufferedReader( new InputStreamReader( )); Byte to charatert to buffer for speed Byte to charatert to buffer for speed IP IP Port Port TCP/IP TCP/IP

IP IP Protocol Protocol Number Number Advertise Advertise

Class Socket Class Socket Class Socket Class Socket getInputStream() Returns an input stream for this socket. Getoutputsteram getInputStream() Returns an input stream for this socket. Getoutputsteram getInputStream getOutputStream() Returns an output stream for this socket getOutputStream() Returns an output stream for this socket getOutputStream

How to Open a Socket? -Client- Machine name is the machine you are trying to open a connection to(ex: ip address or workstation name), and PortNumber is the port (a number) on which the server you are trying to connect to is running. Machine name is the machine you are trying to open a connection to(ex: ip address or workstation name), and PortNumber is the port (a number) on which the server you are trying to connect to is running. When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root).

How to Open a Socket? -Client- These port numbers are reserved for standard services, such as , FTP, and HTTP. These port numbers are reserved for standard services, such as , FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023! When selecting a port number for your server, select one that is greater than 1,023!

How to Open a Socket? -Client- Socket MyClient; MyClient = new Socket("Machine name", PortNumber); MyClient = new Socket("Machine name", PortNumber);

How to Open a Socket? -Client- With exception handling, the code look like following: With exception handling, the code look like following: Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How to Open a Socket? -Server- ServerSocket MyService; try { MyServerice = new ServerSocket(PortNumber); MyServerice = new ServerSocket(PortNumber); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How to Open a Socket? -Server- When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. Socket clientSocket = null; try { serviceSocket = MyService.accept(); serviceSocket = MyService.accept(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How Do I Create an Input Stream? -Client- On the client side, you can use the DataInputStream class to create an input stream to receive response from the server: On the client side, you can use the DataInputStream class to create an input stream to receive response from the server: DataInputStream input; DataInputStream input; try { try { input = new DataInputStream(MyClient.getInputStream()); input = new DataInputStream(MyClient.getInputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How Do I Create an Input Stream? -Client- The class DataInputStream allows you to read lines of text and Java primitive data types in a portable way. The class DataInputStream allows you to read lines of text and Java primitive data types in a portable way. It has methods such as read, readChar, readInt, readDouble, and readLine,. It has methods such as read, readChar, readInt, readDouble, and readLine,. Use whichever function you think suits your needs depending on the type of data that you receive from the server. Use whichever function you think suits your needs depending on the type of data that you receive from the server.

How Do I Create an Input Stream? -Server- On the server side, you can use DataInputStream to receive input from the client On the server side, you can use DataInputStream to receive input from the client DataInputStream input; DataInputStream input; try { try { input = new DataInputStream(serviceSocket.getInputStream()); input = new DataInputStream(serviceSocket.getInputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How do I Create an Output Stream? -Client- On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io: On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io:

How do I Create an Output Stream? -Client- PrintStream output; try { try { output = new PrintStream(MyClient.getOutputStream()); } output = new PrintStream(MyClient.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How do I Create an Output Stream? -Client- The class PrintStream has methods for displaying textual representation of Java primitive data types. The class PrintStream has methods for displaying textual representation of Java primitive data types. you may use the DataOutputStream you may use the DataOutputStream

How do I Create an Output Stream? -Client- DataOutputStream output; DataOutputStream output; try { try { output = new DataOutputStream(MyClient.getOutputStream()); output = new DataOutputStream(MyClient.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How do I Create an Output Stream? -Client- The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The method writeBytes is a useful one. The method writeBytes is a useful one.

How do I Create an Output Stream? -Server- On the server side, you can use the class PrintStream to send information to the client. On the server side, you can use the class PrintStream to send information to the client. PrintStream output; PrintStream output; try { try { output = new PrintStream(serviceSocket.getOutputStream()); output = new PrintStream(serviceSocket.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How do I Create an Output Stream? -Server- You can use the class DataOutputStream as mentioned You can use the class DataOutputStream as mentioned DataOutputStream output; DataOutputStream output; try { try { output = new DataOutputStream(serviceSocket.getOutputStream()); output = new DataOutputStream(serviceSocket.getOutputStream()); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How Do I Close Sockets? -Client- You should always close the output and input stream before you close the socket. You should always close the output and input stream before you close the socket. try { try { output.close(); output.close(); input.close(); input.close(); MyClient.close(); MyClient.close(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

How Do I Close Sockets? -Server- try { try { output.close(); output.close(); input.close(); input.close(); serviceSocket.close(); serviceSocket.close(); MyService.close(); MyService.close(); } catch (IOException e) { catch (IOException e) { System.out.println(e); System.out.println(e); }

Examples -Client- When programming a client, you must follow these four steps: When programming a client, you must follow these four steps: 1.Open a socket. 2.Open an input and output stream to the Socket. the Socket. 3.Read from and write to the socket according to the server's protocol. according to the server's protocol. 4.Clean up. These steps are pretty much the same for all clients. The only step that varies is step three, since it depends on the server you are talking to. These steps are pretty much the same for all clients. The only step that varies is step three, since it depends on the server you are talking to. Echo Client Example Echo Client Example Echo Client Example Echo Client Example

Examples -Server- In following example, Basically, the echo server receives text from the client and then sends that exact text back to the client. In following example, Basically, the echo server receives text from the client and then sends that exact text back to the client. Echo Server Example Echo Server Example Echo Server Example Echo Server Example

Thank you…. Discussion & Comments