SOCKET PROGRAMMING.

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
© Amir Kirsh Java Networking Written by Amir Kirsh, Edited by Liron Blecher.
Network Read/Write. Review of Streams and Files java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
System Programming Practical session 10 Java sockets.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
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.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
OOP&M - laboratory lectures1 JPN – GUI and Applets “… I am sure ‘it wont fail as much as the one before …” - Bill Gates about W98 -
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Performance measurements for inter-process communication.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Java Socket programming. Socket programming with TCP.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
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.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
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,
Introduction to Java Network Programming and HTTP
Field Trip #25 Creating a Client/Server By Keith Lynn.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
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 Inetaddress Class When establishing a connection across the Internet, addresses.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
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-
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Web Server Assignment1 Reference ( Main class (for example : MyServer.java) –Initialization.
CHAPTER-3 NETWORKING PROGRAMMING. Network represents interconnection of computers & devices either by cable or satellite link where no cable is needed.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Object-Orientated Analysis, Design and Programming
Threads in Java Two ways to start a thread
CSE 341, S. Tanimoto Java networking-
Network Programming Introduction
Interactive Standard Input/output
Socket Programming Cal Poly Pomona Young CS380.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Sockets and URLs 17-Sep-18.
Socket programming with TCP
Client server programming
„Networking”.
Sockets and URLs 13-Nov-18.
UNIT-6.
Socket programming - Java
Sockets and URLs 3-Dec-18.
I/O and Applet from Chapter 12
Web Design & Development Lecture 8
Programming TCP Sockets
Presentation transcript:

SOCKET PROGRAMMING

Step to Create a server 1. Create Server Socket with Some Port Number ServerSocket ss = new ServerSocket(777); The server wait till a client accept connection Socket s = ss.accept(); Attach output stream to server socket using getOutputStream() method. This method return OutPutStream object. This stream is used by the socket to send data to client. OutputStream obj = s.getOutputStream(); PrintStream to send data till the socket. PrintStream ps = new PrintStream(obj); 5. PrintStream is used by the server to send data to the client. ps.print(msg); 6. Close the connection

PrintStream OutputStream ServerSocket Server

Step to Create a Client Create a socket at client side using Socket class Socket s = new Socket(“ipaddress”,port number); InputStream to the Socket so that the Socket will be able to receive the data on the InputStream. InputStream obj = s.getInputStream(); To read the data from the Socket into the Client. InputStreamReader isr = new InputStreamReader(obj); BufferedReader br = new BufferedReader(isr); Read data from the BufferedReader object using read or readLine() methods. String msg = br.readLine(); Close the Connection by closing all the streams and sockets. br.close(); s.close();

InputStream BufferedReader Socket Client

TWO WAY COMMUNICATION // create server socket ServerSocket ss = new ServerSocket(888); // make the server wait till a client accept connection Socket s = ss.accept(); System.out.println("Connection established"); //to send data to the client PrintStream ps = new PrintStream(s.getOutputStream()); //to read data coming from the client BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); // to read data coming from the key board BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); while(true) //server executes continuously { String str,str1; //repeat as long as client does not send null string while((str=br.readLine())!=null) { System.out.println(str); str1 = br1.readLine(); // read from client ps.println(str1); } // close connection

// create client socket Socket s = new Socket("localhost",888); // to send data to the server DataOutputStream dos = new DataOutputStream(s.getOutputStream()); //to read data coming from the server BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); // to read data from the key board BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String str,str1; // repeat as long as exit is not typed at client while(!(str=br1.readLine()).equals("exit")) { dos.writeBytes(str+"\n"); // send to server str1 = br.readLine(); System.out.println(str1); } // close connection