Socket Programming Socket Programming Overview Java Socket Programming

Slides:



Advertisements
Similar presentations
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r Sockets are explicitly created, used, released by applications.
Advertisements

Client-Server Paradigm and Performance L. Grewe. 2 Review: Basic Client-Server Request/Reply Paradigm Typical Internet app has two pieces: client and.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Network Programming and Java Sockets
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Application Layer – Lecture.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
9/23/2003-9/25/2003 Sockets & DNS September 23-25, 2003.
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.
2: Application Layer1 Data Communication and Networks Lecture 12 Java Sockets November 30, 2006.
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.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Internet and Intranet Protocols and Applications Lecture 4: Application Layer 3: Socket Programming February 8, 2005 Arthur Goldberg Computer Science Department.
2: Application Layer1 Chapter 2 (continued) Application Layer – part 2 Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Socket programming with UDP and TCP. Socket Programming with TCP Connection oriented – Handshaking procedure Reliable byte-stream.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
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,
1 Network Layers Application Transport Network Data-Link Physical bits.
Protocols Rules for communicating between two entities (e.g., a client and a server) “A protocol definition specifies how distributed system elements interact.
2: Application Layer 1 Socket Programming Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley,
JAVA Socket Programming Joonbok Lee KAIST.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
ECE5650: Network Programming
1 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 2 nd edition. Jim Kurose, Keith Ross.
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,
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.
CS 3830 Day 11 Introduction : Application Layer 2 Server-client vs. P2P: example Client upload rate = u, F/u = 1 hour, u s = 10u, d min ≥ u s.
Discussion 2 Sockets Programming Applets TCP UDP HTTP Delay Estimation
Application Layer 2-1 ESERCITAZIONE SOCKET PROGRAMMING.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5 th edition. Jim Kurose, Keith Ross Addison-Wesley, April A note on the use.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
Java Socket programming. Socket programming with TCP.
Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol.
NETWORK 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,
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Application Network programming Using Java. Application Layer2-2 Socket programming socket: door between application process and end-end-transport protocol.
1 All rights reserved to Chun-Chuan Yang Upon completion you will be able to: The OSI Model and the TCP/IP Protocol Suite Understand the architecture of.
2: Application Layer1 Network applications: some jargon Process: program running within a host. r within same host, two processes communicate using interprocess.
1 Socket Programming Socket Programming Overview –Socket Programming with TCP –Socket Programming with UDP Python Socket Programming Java Socket Programming.
Socket Programming Socket Programming Overview
Transport layer (last part) Application layer
DNS: Domain Name System
Socket programming with TCP
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Socket Programming Socket Programming Overview
Socket Programming.
Java Socket Programming
Socket Programming 2: Application Layer.
DNS: Domain Name System
CPSC 441 UDP Socket Programming
Chapter 2: Application layer
DNS: Domain Name System
DNS: Domain Name System
Socket Programming with UDP
Presentation transcript:

Socket Programming Socket Programming Overview Java Socket Programming Python Socket Programming Hello everyone. Now I’m gonna introduce something about socket programming. First we will try to get a general idea about what socket is. Then we will introduce socket programming with Java and socket programming with Python. The corresponding reading material is the Section 2.7 in Chapter 2, and the project 1 description. Readings: Chapter 2: Sections 2.7 Project I Description CSci4211: Socket API

Refreshing: Client/Server Communication Paradigm Typical network app has two pieces: client and server application transport network data link physical reply request Client: initiates contact with server (“speaks first”) typically requests service from server Before we talk about socket programming, let us recall some materials that we learned before. We know that, the most common network communication model is the client/server model, where clients initiate a conversation with a server by sending some requests. Servers sit on certain machine and provide certain services. Server: provides requested service to client e.g., Web server sends requested Web page, mail server delivers e-mail CSci4211: Socket API

Recall … Host (NIC card) identified by unique IP address Network application/process identified by port number Network connection identified by a 5-tuple (src ip, src port, dst ip, dst port, protocol) Two kinds of Internet transport services provided to applications Connection-oriented TCP Connectionless UDP And here is something we learned. Again, we know that there are two kinds of transport services. CSci4211: Socket API

Socket Programming API API: Application Programming Interface Socket analogous to door sending process shoves message out door sending process assumes transport infrastructure on other side of door which brings message to socket at receiving process host-local, application created/owned, OS-controlled connection between sockets set-up/managed by OS process TCP with buffers, variables socket host or server Internet controlled by OS controlled by app developer Socket API is the programming interface you need to use for transmitting and receiving messages. It is almost like a kind of door, a door to transport layer (note that we are in application layer). CSci4211: Socket API

What APIs Needed? Connection-Oriented TCP Connectionless UDP How to send/recv data How to establish connection Client connects to a server Server accepts client req. How to create socket (door) How to close socket (door) How to identify socket Bind to local address/port How to send/recv data How to create socket How to close socket How to identify socket If you are developing a network program, what services you would like the transport layer to provide? Of course, possibly the first thing come into your mind is that, you want to be able to send something to another party, or receive something from another party. If you want to use connection-oriented TCP, you would also possibly want the system to take care of the connection establishment procedure. CSci4211: Socket API

Socket: Conceptual View This figure shows what socket can help you. It can send and receive messages, and act as a buffer, and also an interface between user and lower layers. CSci4211: Socket API

BSD Socket Programming Flows (connection-oriented) CSci4211: Socket API

BSD Socket Programming (connectionless) CSci4211: Socket API

Socket Programming: Basics The server application must be running before the client can send anything. The server must have a socket through which it sends and receives messages. The client also need a socket. Locally, a socket is identified by a port number. In order to send messages to the server, the client needs to know the IP address and the port number of the server. Port number is analogous to an apartment number. All doors (sockets) lead into the building, but the client only has access to one of them, located at the provided number. CSci4211: Socket API

Java Socket Programming API Class ServerSocket Connection-oriented server side socket Class Socket Regular connection-oriented socket (client) Class DatagramSocket Connectionless socket Class InetAddress Encapsulates Internet IP address structure CSci4211: Socket API

TCP Socket Create a server side TCP socket (class ServerSocket) ServerSocket(int localPort) ServerSocket(int localPort, int queueLimit) ServerSocket(int localPort, int queueLimit, InetAddress localAddress) Accept incoming TCP client request Socket serverSocket.accept() Close an existing TCP socket socket.close() CSci4211: Socket API

TCP Socket (Cont’d) Create a (client side) TCP socket (class Socket) Socket(InetAddress remoteAddr, int remotePort) Socket(String remoteHost, int remotePort) Socket(InetAddress remoteAddress, int remotePort, InetAddress localAddress, int localPort) Read from/write to a socket through stream InputStream socket.getInputStream() OutputStream socket.getOutputStream() Close an existing socket socket.close() CSci4211: Socket API

UDP Socket Create a UDP socket (class DatagramSocket) DatagramSocket(int localPort) DatagramSocket(int localPort, InetAddress localAddress) Receive/send data void receive(DatagramPacket packet) void send(DatagramPacket packet) Close a UDP socket datagramSocket.close() CSci4211: Socket API

UDP DatagramPacket UDP endpoints exchange self-contained message (class DatagramPacket) DatagramPacket(byte[] buffer, int length) DatagramPacket(byte[] buffer, int length, InetAddress remoteAddress, int remotePort) import java.net.*; DatagramPacket sendPacket = new DatagramPacket( byteToSend, byteToSend.length, serverAddress, serverPort); CSci4211: Socket API

A summary of Java Socket protocol localAddr,localPort remoteAddr, remotePort conn-oriented server ServerSocket() accept() conn-oriented client Socket() receive() connectionless server DatagramSocket() send() connectionless client DatagramSocket() CSci4211: Socket API

Java Socket Programming Flows (TCP) Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port=x clientSocket = Socket() wait for incoming connection request connectionSocket = welcomeSocket.accept() TCP connection setup send request using clientSocket read request from connectionSocket read reply from clientSocket close write reply to connectionSocket close connectionSocket CSci4211: Socket API

Java Socket Programming (UDP) Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = DatagramSocket() create socket clientSocket = DatagramSocket() welcomeSocket.receive() Blocks until data received from a client Data (request) send request using clientSocket.send() Process request Data (reply) read reply from clientSocket.receive() welcomeSocket.send() close clientSocket CSci4211: Socket API

Example: Java Client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Create input stream Create client socket, connect to server Create output stream attached to socket CSci4211: Socket API

Example: Java Client (TCP), cont. BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } Create input stream attached to socket Send line to server Read line from server CSci4211: Socket API

Example: Java Server (TCP) import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket CSci4211: Socket API

Example: Java Server (TCP), cont DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } Create output stream, attached to socket Read in line from socket Write out line to socket End of while loop, loop back and wait for another client connection CSci4211: Socket API

Compiling and Running To compile To run javac TCPServer.java javac TCPClient.java To run java TCPServer java TCPClient CSci4211: Socket API

Example: Java client (UDP) import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Create input stream Create client socket Translate hostname to IP address using DNS CSci4211: Socket API

Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } Send datagram to server Read datagram from server CSci4211: Socket API

Example: Java server (UDP) import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Create datagram socket at port 9876 Create space for received datagram Receive datagram CSci4211: Socket API

Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } Get IP addr port #, of sender Create datagram to send to client Write out datagram to socket End of while loop, loop back and wait for another datagram CSci4211: Socket API

Example: Python Client (TCP) from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() create TCP socket for server, remote port 12000 No need to attach server name, port CSci4211: Socket API

Example: Python Server (TCP) from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket) CSci4211: Socket API

Example: Python Client (UDP) include Python’s socket library from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close() create UDP socket for server get user keyboard input Attach server name, port to message; send into socket read reply characters from socket into string print out received string and close socket CSci4211: Socket API

Example: Python Server (UDP) create UDP socket from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress) bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client CSci4211: Socket API

Running Python Program python TCPClient.py python TCPServer.py CSci4211: Socket API

Two Different Server Behaviors Iterative server At any time, only handles one client request Concurrent server Multiple client requests can be handled simultaneously create a new process/thread to handle each request for (;;) { accept a client request; handle it } for (;;) { accept a client request; create a new process / thread to handle request; parent process / thread continues } CSci4211: Socket API

Example of Concurrent Server Python import threading def main(): ……. cx, addr = s.accept() server = threading.Thread(target=dns, name=“…” args=[cx, str(n)+str(addr)] ) server.start() …… def dns(cx,n): CSci4211: Socket API

Example of Concurrent Server Java public class DNSServer{ public static void main(String[]args) throws IOException { …… new QueryThread(sSock.accept()).start(); } class QueryThread extends Thread{ @Override public void run(){ CSci4211: Socket API

Helpful Resources Java Socket Tutorial ‎Python Socket Tutorial http://download.oracle.com/javase/tutorial/networking/sockets/ ‎Python Socket Tutorial http://docs.python.org/2/howto/sockets.html http://docs.python.org/3.1/howto/sockets.html Computer Networking: A Top-Down Approach, 6th Edition. Section 2.7 (Python) Computer Networking: A Top-Down Approach, 5th Edition. Section 2.7-2.8 (Java) CSci4211: Socket API