Socket Programming in Java -First Step of Network Programming-

Slides:



Advertisements
Similar presentations
Network Programming and Java Sockets
Advertisements

Network Programming How do Server, Client, and Display applications communicate with each other? Client Server Client Display.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
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. 
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
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.
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.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
Systems Architecture: Client/Server Systems Naim R. El-Far, PhD Candidate TA for SEG3202 Software Design and Architecture with N. El-Kadri (Summer 2005)
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.
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 = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick 1.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
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
 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.
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.
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.
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.
Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
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.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
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.
Socket Programming in Java -First Step of Network Programming-
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();
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Networking OSI (Open Systems Interconnection) model of computer networking, seven layers (the Application, Presentation, Session, Transport, Network, Data.
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.
LAN Chat server BY: VIPUL GUPTA VIKESH SINGH SUKHDEEP SINGH.
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 Ameera Almasoud
Echo Networking COMP
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.
Network Programming Introduction
Socket Programming connecting processes.
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Socket Programming in Java -First Step of Network Programming-

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- 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- 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