Björn Landfeldt School of Information Technologies NETS3303 Network Programming Intro.

Slides:



Advertisements
Similar presentations
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Advertisements

Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Socket Programming Application Programming Interface.
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.
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.
1 Java Networking – Part I CS , Spring 2008/9.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Client/Server In Java An Introduction to TCP/IP and Sockets.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
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.
Computer Networks Lecture 1 Adrian Sergiu DARABANT.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
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.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
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.
Socket programming case study NCKU Multimedia Networking Lab Pei Chuan Liu.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Lab 5 Sockets. Useful Sockets Links (courtesy of Stanford University) Programming UNIX Sockets in C - Frequently Asked Questions
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.
Distributed Computing A Programmer’s Perspective.
Introduction to Socket
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.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Java Server Programming Web Interface for Java Programs.
Part 4: Network Applications Client-server interaction, example applications.
Introduction A Simple Daytime Client A Simple Daytime Server
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,
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
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.
Java Programming II Java Network (I) Java Programming II.
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. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Java Packages Thread Example Java Socket Programming.
CPSC Application Layer 1 Trying out HTTP (client side) for yourself 1. Telnet to your favorite Web server: Opens TCP connection to port 80 (default.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
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
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming Client/Server.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
CSI 4118 – UNIVERSITY OF OTTAWA
Internet Networking recitation #8
Multiplexing/Demux.
Presentation transcript:

Björn Landfeldt School of Information Technologies NETS3303 Network Programming Intro

Björn Landfeldt School of Information Technologies Distributed Programming Model Master-Terminal –Fill dumb terminal with code to execute on server (mainly extinct) Client-Server –Two independent pieces of code working together, Different roles (most common) P-2-P –All nodes equal, both client and server (on the rise)

Björn Landfeldt School of Information Technologies Client - Server Process 1 Process 2Matching communication End-points

Björn Landfeldt School of Information Technologies P-2-P

Björn Landfeldt School of Information Technologies Centralised P-2-P

Björn Landfeldt School of Information Technologies De-Centralised P-2-P

Björn Landfeldt School of Information Technologies Fundamentals Processes exchange data –Need pipe from process to stack –Need address to other process –Need knowledge of how data is represented and agreement between processes Things happen in –User space –Kernel space

Björn Landfeldt School of Information Technologies Pipe A process typically open up a pipe with a descriptor (compare file descriptor) as an end-point for communication Process reads from and writes to pipe

Björn Landfeldt School of Information Technologies Addressing How can we find the communication end point (pipe) of the remote process? In TCP/IP three address components –IP address –Protocol –Port Called a Socket!

Björn Landfeldt School of Information Technologies Addressing Protocol IP address 1 Ports Process 1 Process 2 Protocol IP address 2 Ports Process 1 Process 2

Björn Landfeldt School of Information Technologies Data representation So now we know –Send data to IP x, Proto Y, Port z How do we interpret data? How do we know what data to expect? How do we know when things go right or wrong?

Björn Landfeldt School of Information Technologies Protocols Protocols are informal agreements of –When we can send data –How data is represented 8-bit, 16-bit etc. –How to interpret data / react For now –Transport protocols (UDP, TCP) –Application protocols (HTTP, SMTP, GNUTELLA)

Björn Landfeldt School of Information Technologies Client or server? A client actuates initial communication. Before this happens Server has to listen for connection attempts

Björn Landfeldt School of Information Technologies Client Open up connection to remote end point (socket) Send some initial data (Maybe) start listening for a reply In all APIs this lecturer knows of, sockets are two-way, you can both read and write

Björn Landfeldt School of Information Technologies JAVA Client import java.lang.*; import java.io.*; import java.net.*; class Client { public static void main(String args[]) { try { Socket skt = new Socket("localhost", 1234); networkPout = new PrintWriter(skt.getOutputStream(),true); networkPout.println(“Hello World”); } catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); } Not complete!!!

Björn Landfeldt School of Information Technologies Interpretation Class Socket = TCP, DatagramSocket = UDP Socket(“localhost”, 1234), localhost = IPaddress (sort of), 1234 = Port PrintWriter = text filter via OutputStream = pipe to socket. Try, catch = (you know this)

Björn Landfeldt School of Information Technologies JAVA Server import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.lang.System; public class server { public static void main (String args[]) throws IOException { ServerSocket s = new ServerSocket (1234); //TCP server socket Socket sock; BufferedReader datain; System.out.println ("Server starting..."); sock = s.accept (); //Sit and wait datain = new BufferedReader (new InputStreamReader (sock.getInputStream ())); System.out.println (datain.readLine ()); // Read message and print out sock.close (); s.close (); }

Björn Landfeldt School of Information Technologies Iterative - Concurrent Server Last example Iterative –Handle each connection fully before –closing socket and –Go back to listening Concurrent –Handle connection –Spawn new process or thread –Go back to listening

Björn Landfeldt School of Information Technologies Concurrent public class server { public static void main (String args[]) throws IOException { ServerSocket s = new ServerSocket (1234); //TCP server socket Socket sock; BufferedReader datain; while(true) { sock = s.accept (); //Sit and wait new sockDealer(sock).start(): } public class sockDealer extends Thread { sockDealer(Socket sock) { new Socket s = sock; } public void run() { try { read data & process whatever from s; } catch(IOException e){} }

Björn Landfeldt School of Information Technologies Hint on objects How send complex structures? Objects with states? Arrays, vectors, matrices, hashtables? Serialize public class UserData implements java.io.Serializable

Björn Landfeldt School of Information Technologies C - client #include #include /* close */ #define SERVER_PORT 1500 struct sockaddr_in localAddr, servAddr; struct hostent *h; h = gethostbyname(“ if(h==NULL) { printf("%s: unknown host '); exit(1); } /* Fill struct with values *? servAddr.sin_family = h->h_addrtype; memcpy((char *) &servAddr.sin_addr.s_addr, h- >h_addr_list[0], h->h_length); servAddr.sin_port = htons(SERVER_PORT); /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); exit(1); } /* bind any port number */ localAddr.sin_family = AF_INET; // IPv4 localAddr.sin_addr.s_addr = htonl(INADDR_ANY); localAddr.sin_port = htons(0);

Björn Landfeldt School of Information Technologies C - client rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); if(rc<0) { printf("%s: cannot bind port TCP %u\n",argv[0],SERVER_PORT); perror("error "); exit(1); } /* connect to server */ rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if(rc<0) { perror("cannot connect "); exit(1); } rc = send(data); if(rc<0) { perror("cannot send data "); close(sd); exit(1); }

Björn Landfeldt School of Information Technologies C-Server /* create socket */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd<0) { perror("cannot open socket "); return ERROR; } /* bind server port */ servAddr.sin_family = AF_INET; servAddr.sin_addr.s_addr = htonl(INADDR_ANY); servAddr.sin_port = htons(SERVER_PORT); if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) { perror("cannot bind port "); return ERROR; } listen(sd,5); while(1) { printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT); cliLen = sizeof(cliAddr); newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen); if(newSd<0) { perror("cannot accept connection "); return ERROR; } /* init line */ memset(line,0x0,MAX_MSG); /* receive segments */ while(read_line(newSd,line)!=ERROR) { printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);

Björn Landfeldt School of Information Technologies Concurrent C-Server /* // Fork process into 2 processes. // - Parent server process will continue to // listen for other client connection requests. // - Child server process will serve the currently connected client. */ if ((process_id = fork()) < 0) { perror("fork"); exit(EXIT_FAILURE); } /* // Parent server process: // - Close current connection. // - Continue listening for more client connections */ if (process_id > 0) { close(current_socket); continue; } /* // Child server process: // - Close server socket. // - Serve current connection. */ close(server_socket); serve_connection(current_socket); } return EXIT_SUCCESS; } void serve_connection ( int socket_value )

Björn Landfeldt School of Information Technologies Loopback interface Use for local testing Run server and client on same host or “localhost” Hint: Select high port numbers for testing (above 10000)

Björn Landfeldt School of Information Technologies