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

Slides:



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

@2011 Mihail L. Sichitiu1 Android Introduction Hello Socket Programming TCP and UDP.
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.
COEN 445 Communication Networks and Protocols Lab 4
Network Programming Chapter 11 Lecture 6. Networks.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) 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.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
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)
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 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.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
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.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Rick Mercer Multi Threaded Chat Server. 2 Client – Server with Socket Connections We've seen how to establish a connection with 1 client Review a simple.
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.
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.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
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.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
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,
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.
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.
Field Trip #25 Creating a Client/Server By Keith Lynn.
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.
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-
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
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.
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes.
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
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Socket Programming Ameera Almasoud
Echo Networking COMP
Lecture 21 Sockets 1 (Not in D&D) Date.
Networking COMP
Computing with C# and the .NET Framework
MCA – 405 Elective –I (A) Java Programming & Technology
Client-server Programming
Socket Programming Cal Poly Pomona Young CS380.
Hello Socket Programming TCP and UDP
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

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.  Then we will show some examples of how to write client and server applications.

 If you are programming a client, then you would open a socket like this: Socket MyClient; MyClient = new Socket("Machine name", PortNumber); Machine name :is the machine you are trying to open a connection to PortNumber : is the port (a number) on which the server you are trying to connect to is running Note : that port numbers between 0 and 1,023 are reserved for privileged users, select one that is greater than 1,023!

Socket MyClient; try { MyClient = new Socket("Machine name", PortNumber); } catch (IOException e) { System.out.println(e); } In the previous example, we didn't make use of exception handling, however, it is a good idea to handle exceptions. The above can be written as: (From now on, all our code will handle exceptions!)

 If you are programming a server, then this is how you open a socket: ServerSocket MyServer; try { MyServer = new ServerSocket(PortNumber); } catch (IOException e) { System.out.println(e); }

 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 = null; try { clientSocket = MyServer.accept(); } catch (IOException e) { System.out.println(e); }

 On the client side, you can use the ObjectInputStream class to create an input stream to receive response from the server: ObjectInputStream input; Object Data; try { input = new ObjectInputStream(clientSocket.getInputStream()); Data = input.readObject(); } catch (IOException e) { System.out.println(e); }

 On the server side, you can use ObjectInputStream to receive input from the client: ObjectInputStream input; Object Data; try { input = new ObjectInputStream(MyServer.getInputStream()); Data = input.readObject(); } catch (IOException e) { System.out.println(e); }

 On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream or ObjectInputStream of java.io: ObjectOutputStream output; try { output = new ObjectOutputStream(clientSocket.getOutputStream()); output.writeObject(ObjectIWantToSend); } catch (IOException e) { System.out.println(e); }

 You should always close the output and input stream before you close the socket.  On The client Side.. try { output.close(); input.close(); clientSocket.close(); } catch (IOException e) { System.out.println(e); }

 On the server side: try { output.close(); input.close(); MyServer.close(); } catch (IOException e) { System.out.println(e); }

 When programming a Server/Client, you must follow these four steps:  Open a socket.  Open an input and output stream to the socket.  Read from and write to the socket according to the server's/client’s protocol.  Clean up: close the output stream close the input stream close the socket

 Now we will write two applications: a simple SMTP (simple mail transfer protocol) client, and a simple echo server.  SMTP client Let's write an SMTP (simple mail transfer protocol) client -- one so simple that we have all the data encapsulated within the program.  Echo server Basically, the server receives text from the client and then sends that exact text back to the client. This is just about the simplest server you can write. Note that this server handles only one client. Try to modify it to handle multiple clients using threads.

 In this Example, we use socket programming in Java to implement a pair of client and server that can achieve simple password verification.  The client will send a pair of username and password to the server and the server will verify whether the pair of username and password is legitimate or not.  This Example handle multiple clients using threads.