Java Sockets Tutorial Rahul Malik Nov 12, 2005.

Slides:



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

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
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.
1 Java Networking – Part I CS , Spring 2008/9.
Network Programming. The biggest difficult part in networking programming lies in understanding networking not in using java networking package. Since.
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.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
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)
Socket Programming in Java -First Step of Network Programming-
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Socket Programming References: redKlyde ’ s tutorial set Winsock2 for games (gamedev.net)
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.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
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.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Establishing communication with Envirobat using TCP/IP Presented by Apourva Parthasarathy Date : 18/06/13.
 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.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Java Socket programming. Socket programming with TCP.
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.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
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.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
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
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-
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
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.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Echo Networking COMP
Networking COMP
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
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.
Thread per client and Java NIO
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Java Sockets Tutorial Rahul Malik Nov 12, 2005

Outline What is a Socket? Benefits of Using Java Client Sockets Server Sockets Programming Example

What is a Socket? Definition:  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 number so that the TCP layer can identify the application that data is destined to be sent. Okay……so what does that mean? And how does it differ for the Client and Server?

What is a Socket ? : Client The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port. If the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server

What is a Socket? : Server The server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. The client and server can now communicate by writing to or reading from their sockets.

Java Sockets Pros/Cons Classes are already in java.net package that make lower level Beej’s usage unnecessary. There are separate classes for servers and clients. Simplicity : It’s very functional and relatively easy to learn in a short amount of time. Allows us to focus on the development and bugs of our program versus Inner workings are similar to Beej’s Guide Cons: Similar to Beej’s Guide

Outline for Sockets Program: Open a socket. Open an input stream and output stream to the socket. Read from and write to the stream according to the server's protocol. Close the streams. Close the socket.

Opening a Socket : Client How do we create a socket? Socket mySocket = new Socket(“hostname”, port); Slightly easier than Beej’s right? This goes from defining a socket and connecting all in one line!!! “Hostname” is the host you are trying to connect The “port” is the port you wish to bind to. 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). ) Simple enough?? This is only for clients, servers actually use ServerSockets which listen on specific ports and handle connections versus making them.

Client : Open an input stream to the socket How do I create an Input Stream?? Start with creating a socket Socket mySocket = new Socket(“hostname”, port); For input streams (to receive data from the server) we must utilize the DataInputStream class. DataInputStream in = new DataStreamInput ( mySocket.getInputStream() ); The Data Input Stream Class allows you to read lines of text and Java primitive types. It contains methods like read, readChar, readInt, readDouble, and readLine, etc.

Client : Open an output stream How do I create an Output Stream?? Continuing with the same socket We must use the DataOutputStream or PrintStream Class to do this. PrintStream Output = new PrintStream (mySocket.getOutputStream() ); PrintStream allows for .write() and .println() functions that are very useful. DataOutputStream however prints one primitive type at a time and contains functions like .writebytes();

Closing When we are done, we must make sure to close sockets AND input/output streams. Java has made this very simple. Using the previous examples Input and Output streams : input.close(); and output.close(); Socket : mySocket.close();

Server : Creating the Socket On the Server side we have our own class, ServerSocket. To open a Socket to listen on a specific port we would use : ServerSocket myServer = new ServerSocket(“Port #”); A server socket is useless without the input and output streams associated with it.

Server : I/O Stream’s 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 = new Socket(); clientSocket = myServer.accept(); .accept() returns the socket that is connecting to the server. The advantage of that is that we can use .getInputStream() and .getOutputStream() from the socket class to handle our communication

Server : Closing Up The fact that we interact with clients on individual sockets is good because : We only close sockets that are connected to clients The server is still listening and can receive connections.

Extra Notes : Required Libraries You must use exception handling try{….}catch{……}finally{……} try { ServerSocket ss = new ServerSocket(80); } catch (IOException e) {System.err.println(e);} To do Java Sockets programming you must import(include) : java.io.* java.net.*

Practice Java Sockets : Our Task Task: To create a Echo Client/Server model. The Echo server simply receives data from its client and echoes it back. The Echo server is a well-known service that clients can rendezvous with on port 7. Sun's Java Echo Client Example