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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
1 TCP socket application Architecture of Client-Server Applications Java Socket Programming Client Application Server Application.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
System Programming Practical session 10 Java sockets.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
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.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Welcome to CIS 235 Computer Networks Fall, 2007 Prof Peterson.
An Introduction to Internetworking. Algorithm for client-server communication with UDP (connectionless) A SERVER A CLIENT Create a server-socket (listener)and.
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
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)
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
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.
Socket Programming in Java -First Step of Network Programming-
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 = "
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
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.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
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
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
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.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
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.
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.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Java Server Programming Web Interface for Java Programs.
Fall 2007cs4251 Distributed Computing Umar Kalim Dept. of Communication Systems Engineering 26/09/2007.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
Socket Programming in Java -First Step of Network Programming-
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
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.
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.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Threads in Java Two ways to start a thread
Block 15 Developing the Calculator application
Networking COMP
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
„Networking”.
Presentation transcript:

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. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes  Socket – implement the server side of the connection  ServerSocket – that implement the client side of the connection

The Socket Server Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.

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

Communication through Socket Upon acceptance, the server gets a new socket bound to a different port On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server

Client’s Responsibilities 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.

Set up Connection Open  a socket.  an input stream and  output stream to the socket. try{ aSocket = new Socket(hostName, portNumber); out = new PrintWriter(aSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( aSocket.getInputStream())); } catch (IOException ex) { //error handling … }

Talk Based on Protocol Using the out writer object to read text messages out.println(userInput); in.readLine(); Using the in reader object to print text messages

Clean up Close the connection objects so that they are subject to garbage collection out.close(); in.close(); aSocket.close();

Server’s Responsibilities Open a ServerSocket Wait until a client request arrives  Accept the client  open an input stream and output stream designated to this client Read from and write to the stream according to the server's protocol. Close the streams, when shutting down Close the socket.

Open a ServerSocket Open a ServerSocket which listens to a designated port Avoid using well-known port numbers, such as 80 for web servers try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); }

Accept a Client Request Waiting for client request and accept it Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); }

Set up Connection to a Client Gets the socket's input and output stream and opens readers and writers on them.  To facilitate text-based communication PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream()));

Client-Server Communication Design pattern: generate response by invoking the processInput method of the protocol class // initiate conversation with client KnockKnockProtocol kkp = new KnockKnockProtocol(); outputLine = kkp.processInput(null); out.println(outputLine); // conduct conversation with client until the finish is signaled by client while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Bye.")) break; }

KnockKnockProtocol This class provides  All constants for this application  Clues and corresponding answers  The processInput method which Reply with an answer if theInput is in the expected format Reply with the expected input public String processInput(String theInput);

Team Formation The whole class is divided into 3 teams Each team has  2 developers works in pair on the client app in the Windows (or RedHat Linux) environment  2 developers works in pair on the server app on Cobra

Part I – The Knock Knock App Build up confidence Activities  Set up environment for server and client  Compile and execute  Add a couple of new clue-answer pairs (optional)

Part II – A Simple ATM App Purpose: upgrade an existing client-server application to include a GUI Code bases available  A bank application with a client and a server talking through socket  A simple GUI app managing phone directory entries, A working GUI ready for modification A method facilitating file access

The Bank Server App The server app includes  A BankServer class (with main )  A Bank class (holds an array of BankAccounts)  A BankService class (takes charge of socket communication)  A BankAccount class (facilitates balance, deposit, and withdraw)

Association – The Bank Server App BankServerBank BankAccountBankService doService() processCommand()

The Existing GUI A list to hold names Two text fields to display and accept a name and a number Three buttons for clearing fields, and add and delete the current entry A method accessing loading entries from a file

Tasks – client side Develop a bank client with a GUI  Change the List to a TextArea to display messages from server  Change the labels Name and Number to Account # and Amount, respectively  Change the button labels to Balance, Deposit, and Withdraw, respectively; and change the control logic  Write a class designated to socket connection with the server

Tasks – server side Modify the server so that account information can be read from a file  Write a utility class which has a static method designated for file accessing, or  Write a method for BankServer to access the account file  Create a file with 10 account records

Association – The Bank Client App BankClientMainBankClientFrame BankClientConnector request(String cmd):String

A Sample Screenshot

A Possible Approach Set up environment and run the whole application Read the existing code base Modify the client app and the server app separately  The server team may take over the connector class from the client team Integrate and test