Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication.

Similar presentations

Presentation on theme: "Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication."— Presentation transcript:

1 Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication

2 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

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

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

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

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

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

8 Aalborg Media Lab 15-Jul-15 Sockets in Java The 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, 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.

9 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();

10 Aalborg Media Lab 15-Jul-15 References The Java Tutorial: Custom Networking ml#overview ml#overview JavaWorld: Writing client/server applications in Java sockets_p.html sockets_p.html

11 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)

12 Aalborg Media Lab 15-Jul-15 EyesWeb Example

13 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 ( –Intergers –Interger-Arrays

14 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

15 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

16 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

17 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

Download ppt "Aalborg Media Lab 15-Jul-15 Java & EyesWeb Lecture 14 Java Client / Server Communication."

Similar presentations

Ads by Google