Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "www.concordia.ca";

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.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
OCT Information System Management 1 Organizational Communications and Distributed Object Technologies Week 5: Inter-process Communications.
CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „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.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
OCT -- OCT Chapter 4 Coulouris Interprocess Communication.
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.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3, © Addison-Wesley.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Socket Programming in Java -First Step of Network Programming-
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
1 Tuesday, December 16, 2008 The practical scientist is trying to solve tomorrow's problem on yesterday's computer. Computer scientists often have it the.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Slides for Chapter 4: Interprocess Communication
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.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
Chapter 3: Interprocess Communication
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Java Sockets Programming
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.
From Coulouris, Dollimore, Kindberg and Blair Distributed Systems: Concepts and Design Edition 5, © Addison-Wesley 2012 Slides for Chapter 4: Interprocess.
1 Lecture 4: Interprocess Communication Haibin Zhu, PhD. Assistant Professor Department of Computer Science Nipissing University © 2002.
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.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
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.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
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 ( 李德成 )
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Sockets and URLs 17-Sep-18.
Java Network Programming
„Networking”.
CSCD 330 Network Programming
Sockets and URLs 13-Nov-18.
Slides for Chapter 4: Interprocess Communication
Sockets and URLs 3-Dec-18.
سوکت (ارتباط بین کاربردها)
Distributed Objects: Communication and System Support
CSCD 330 Network Programming
Distributed Objects: Communication and System Support
Slides for Chapter 4: Interprocess Communication
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Socket programming 1

getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = " try { InetAddress address = InetAddress.getByName(host); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find "+ host); }

getByName import java.net.*; public class GetNameByAddress { public static void main (String args[]) { try { InetAddress address = InetAddress.getByName(" "); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find "); }

LocalMachineAddress import java.net.*; public class LocalMachineAddress { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find this computer's address."); }

Local machine name import java.net.*; public class LocalMachineName { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println("Local Machine Name is " + address.getHostName()); } catch (UnknownHostException e) { System.out.println("No Name for this machine."); }

Elementary TCP socket ServerSocketServerSocket() BindBind() ListenListen() AcceptAccept() Close() ReadRead() WriteWrite() Process request Blocks until connections from client TCP Server TCP Client Connect() Write(Write() ReadRead() Close() SocketSocket() Connection establishment (TCP 3-way Handshake) Data (request) Data (reply) Well-known port Socket functions for elementary TCP client-server

ServerSocket ServerSocket(int port) Creates a server socket, bound to the specifie port. ServerSocket Back

Listen accept() method of ServerSocket instance object returns Socket instance object. Accept method listens for a connection to be made to this socket and accepts it.acceptServerSocketSocket Back

Read from a socket DataInputStream – A data input stream lets an application read primitive Java data types from an underlying input stream in a machine- independent way. – An application uses a data output stream to write data that can later be read by a data input stream. – readUTF() method return String data typereadUTFString – It reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.

getInputStream() method of Socket class getInputStream() method of class socket Returns an input stream type (InputStream) for this socket.getInputStreamInputStream If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. Back

Write to a socket DataOutputStream class – A data output stream lets an application write primitive Java data types to an output stream in a portable way. – An application can then use a data input stream to read the data back in. – writeUTF() method writes a string to the underlying output stream using Java modified UTF-8 encoding in a machine-independent manner.

getOutputStream() method of Socket class getOutputStream() method of Socket class returns an output stream for this socket. If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. Back

Socket Socket(InetAddress address, int port) Creates a stream socket and connects it to the specified port number at the specified IP address. SocketInetAddress Back

TCPServer import java.net.*; import java.io.*; public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; // the server port ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());} } class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} } public void run(){ try { // an echo server String data = in.readUTF(); // read a line of data from the stream out.writeUTF("Thats what i received from you :"+ data); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("readline:"+e.getMessage()); } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}} }

TCPClient import java.net.*; import java.io.*; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname Socket s = null; try{ int serverPort = 7896; s = new Socket(" ", serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out =new DataOutputStream( s.getOutputStream()); out.writeUTF(" "); // UTF is a string encoding see Sn. 4.4 String data = in.readUTF(); // read a line of data from the stream System.out.println("Received: "+ data) ; }catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("readline:"+e.getMessage()); }finally {if(s!=null) try {s.close();}catch (IOException e){System.out.println("close:"+e.getMessage());} }

References texamples/index.html texamples/index.html Java Docummentation. Sample/Sockets/Text%20Book/ Sample/Sockets/Text%20Book/