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

Slides:



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

I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Network Programming and Java Sockets
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Trådad Echo server public class ThreadedEchoServer extends Thread { Socket con; public static void main(String[] args) {... try { ServerSocket ss = new.
Inter-Process Communication: Network Programming using TCP Java Sockets Dr. Rajkumar Buyya Cloud Computing and Distributed Systems (CLOUDS) Laboratory.
1 Network Programming and Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Yoshi
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.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
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
1 Networking with Java 2: The Server Side. 2 Some Terms Mentioned Last Week TCP -Relatively slow but enables reliable byte-stream transmission UDP -Fast.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
1 Programming Section 9 James King 12 August 2003.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
Client/Server In Java An Introduction to TCP/IP and Sockets.
© 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.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Socket Programming in Java -First Step of Network Programming-
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
Network Programming CSE 132. iClicker/WUTexter Question How many of the following statements are true? In the model-view-controller paradigm, a swing.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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.
Lab 2C Primer I/O in Java Sockets Threads More Java Stuffs.
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.
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.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java 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.
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();
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.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
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
Echo Networking COMP
Threads in Java Two ways to start a thread
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

Socket Programming ENTERPRISE JAVA

2 Content  Sockets  Streams  Threads  Readings

3 Socket Programming  TCP/IP based network connection  Packet data arrives in order  Delivery of packets is guaranteed  ServerSocket  Socket  InputStream  OutputStream

4 Server import java.io.IOException; import java.net.ServerSocket; public class Server { public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(5000); } catch(IOException e) { e.printStackTrace(); } Server Port Required Import Statements

5 Server while(true) { try { Socket socket = server.accept(); InputStream is = socket.getInputStream(); StringBuffer mesg = new StringBuffer(); while(true) { int data = is.read(); if(data == -1) { break; } else { mesg.append((char)data); } System.out.println(mesg); } catch(IOException e) { e.printStackTrace(); } Keep serving clients forever Wait for a client to connect Handle client connection with Socket object Get a reference to Socket’s InputStream Read data byte by byte until the end of stream is reached Append data to StringBuffer

6 Client import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class Client { public static void main(String[] args) { try { Socket client = new Socket("localhost",5000); OutputStream os = client.getOutputStream(); os.write("Hello ISCG7425".getBytes()); os.close(); client.close(); } catch(IOException e) { e.printStackTrace(); } Open socket connection to localhost on port 5000 Get a reference to Socket’s Output Stream Use Output Stream’s write(byte[] data) method to send string data to server

7 Data Streams  Client and Server must observe the same protocol  Socket Input/Output is data stream oriented  InputStream and OutputStream objects operate on bytes and byte[] arrays  Using the read(byte[] data) and write(byte[] data) methods  Primitive data types need to be converted to and from bytes to be transferred across the network  Problems arise if there is a data type mismatch. All the data types are bytes so can be interpreted as any other data type.

8 Data Streams  Helper classes that do all the leg work in converting data to and from bytes  DataInputStream  DataOutputStream  Have methods to read / write a variety of primitive data types  Strings  byte  int  short  float

9 Echo Server Socket socket = server.accept(); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); while(true) { try { String mesg = dis.readUTF(); dos.writeUTF("Echo response:"+mesg); } catch(EOFException e) { break; }

10 Echo Client Socket client = new Socket("localhost",5000); DataOutputStream dos = new DataOutputStream(client.getOutputStream()); DataInputStream dis = new DataInputStream(client.getInputStream()); dos.writeUTF("Test Message"); String resp = dis.readUTF(); System.out.println(resp); client.close();

11 Server Threads  Lots of Socket method calls block waiting for input  accept() – blocks waiting for a client connection  read() – blocks waiting for input  In a server with only one thread of execution – the server is blocked and no over tasks can be processed until the blocking method has finished  Only one client can connect to a single threaded server at a time  Takes a long time to close and open TCP / IP based connections – so can’t rely on close() method

12 Server Threads final Socket socket = server.accept(); Thread serverThread = new Thread() { public void run() { try { InputStream is = socket.getInputStream(); StringBuffer mesg = new StringBuffer(); while(true) { int data = is.read(); if(data == -1) break; else mesg.append((char)data); } System.out.println(this.getName() + "\n"+ mesg); }catch(IOException e) { /* … Handle exceptions … */ } } }; serverThread.start();

13 Threads  Threads should be used whenever a task includes some sort of long running and blocking input and output  Servers will use one thread per client  Threads allow multiple things to be processed at the same time  Thread.sleep(100) puts the current thread to sleep for 100 ms  Make sure your server loop includes a Thread.sleep() method to give up priority to other threads  All the code executed in a Thread is included in the run() method – a thread is stopped once the run() method finishes

14 Readings   sockets/ sockets/  /index.html /index.html