Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.
Published byModified over 4 years ago
Presentation on theme: "Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication."— Presentation transcript:
Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication
Aalborg Media Lab 15-Jul-15 Interface to EyesWeb EyesWeb is able use a network connection as an interface to any program. EyesWeb is taking the role of the client making data available by sending it to a server ServerClient
Aalborg Media Lab 15-Jul-15 Networking Basics Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP ). TCP provides a point-to-point channel for applications that require reliable communications. The UDP protocol provides for communication that is not guaranteed between two applications on the network.
Aalborg Media Lab 15-Jul-15 Ports & IP IP-addresses are used to identify the computer on the network. Ports are used by TCP and UDP to deliver the data to the right application. In connection-based communication such as TCP, a server application binds a socket to a specific port number.
Aalborg Media Lab 15-Jul-15 Sockets Server-Side 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. On the client-side: 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.
Aalborg Media Lab 15-Jul-15 Sockets Server-Side If everything goes well, 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.
Aalborg Media Lab 15-Jul-15 Sockets Client-Side 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. 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.
Aalborg Media Lab 15-Jul-15 Sockets in Java The java.net package in the Java platform provides a class, Socket, that implements one side of a two-way connection between a Java program and another program on the network. Additionally, java.net includes the ServerSocket class, which implements a socket that servers can use to listen for and accept connections to clients. Data is read from and to sockets using the DataInputStream and DataOutputStream.
Aalborg Media Lab 15-Jul-15 Client Example Socket MyClient; MyClient = new Socket(“IP”, PortNumber); DataInputStream input; input = new DataInputStream(MyClient.getInputStream()); int res = input.readInt();
Aalborg Media Lab 15-Jul-15 References The Java Tutorial: Custom Networking http://java.sun.com/docs/books/tutorial/networking/TOC.ht ml#overview http://java.sun.com/docs/books/tutorial/networking/TOC.ht ml#overview JavaWorld: Writing client/server applications in Java http://www.javaworld.com/javaworld/jw-12-1996/jw-12- sockets_p.html http://www.javaworld.com/javaworld/jw-12-1996/jw-12- sockets_p.html
Aalborg Media Lab 15-Jul-15 EyesWeb and networking EyesWeb is function as a client able to send data to a TCP-Server –Server must be running before client executes application Use the Network.Output.SendToNetwork-Block to send data. Configure Block with proper server address (IP, port)
Aalborg Media Lab 15-Jul-15 Retrieving data from EyesWeb Don’t develop a TCP-Server from Scratch! –Reuse, extend existing solutions The eyeswebsocket class by Rune Andersen (email@example.com) supporting:firstname.lastname@example.org –Intergers –Interger-Arrays
Aalborg Media Lab 15-Jul-15 Creating a eyeswebsocket Create an eyeswebsocket object & run it; myeywtst myeywtst = new myeywtst(); eywsoc = new eyeswebsocket(7777); eywsoc.Verbose = true; eywsoc.blocking= false; eywsoc.waitconn(); // waiting for input
Aalborg Media Lab 15-Jul-15 Read data from eyeswebsocket Reading integers int num = eywsoc.scalarVal; Reading integer arrays int array = eywsoc.matrixData; eywsoc.matrixCols //no. cols in array eywsoc.matrixRows //no. rows in array
Aalborg Media Lab 15-Jul-15 Reading data multiple times If new data is send from EyesWeb: eywsoc.scalarVal, eywsoc.matrixData is overwritten! –Readout data every x ms by using the Timer class (if you cant remember look at reboundPanel in your book) How often data is send from EyesWeb determines how often it must be read in JAVA. –delay due network transmission and computation time
Aalborg Media Lab 15-Jul-15 Exercises Programming Projects –Write a program that reads out the integer array from generated in EyesWeb –Displays it in Console or JTable