Chapter 2. Network Chat
|
|
- Tabitha Norton
- 6 years ago
- Views:
Transcription
1 Chapter 2. Network Chat In a multi-player game, different players interact with each other. One way of implementing this is to have a centralized server that interacts with each client using a separate socket. This chapter first presents the basics of secure SSL socket programming via naive client and server programs (Section 1), then extends the programs to command-line chat programs where both the client and the server are multi-threaded (Section 2), and finally extends to GUI chat programs (Section 3). 1 Naive Socket Example The code for this section appears in package jwg.ch2.naive. To test-run, first run ServerNaive, then run ClientNaive. Because the console window in MyEclipse only shows system output for the most recently executed program, to view output of both the server and the client you will need to run at least one of the programs using some command-line console outside MyEclipse. For instance, to run the server program, in a command-line console, cd to the jwg code directory, then type java jwg.ch2.naive.servernaive. This section illustrates the basics of socket programming via a naive client program and a server program. The programs follow the protocol as shown in Figure 1. CLIENT Connect to server. Write an integer (=7). Read an integer (=49). SERVER Create a server socket. Accept a client connection. Read an integer (=7). Write its square (=49). Figure 1: The protocol for the naive socket example. Figure 2 shows the naive client program. The client program first connects to a server on a given host and at a given port. In this example, the host is hard coded as localhost (i.e. on the same machine the client runs on), and the port number is hard coded as Here the port number must be between 1024 and This is because a port number less than 1024 can only be created by a superuser, and is the maximum number a 16 bit integer can represent. To connect to the server, Line 15 gets a default SSLSocketFactory, and then Line 16 creates an SSLSocket from the SSLSocketFactory. We suggest to use SSLSocket instead of Socket for security reason. Lines 17 and 18 instruct the client socket to use an anonymous cipher suite, so that a KeyManager or TrustManager is not needed. Lines show how to create input and output streams from the socket. We suggest to use BufferedReader for input and PrintWriter for output, although these are not the only choices. We chose the variable name is to represent input stream, and the variable name os to represent output stream. Finally, Lines 26 and 27 illustrate how to write to the server and read from the server. In this program, the client writes an integer 7 to the server, then reads an integer (the square of 7) from the server and prints it out. 1
2 1. // Figure 2. ClientNaive.java: Naive socket client package jwg.ch2.naive; 4. import java.io.*; 5. import javax.net.ssl.*; public class ClientNaive { 8. public static void main(string[] args) { 9. String hostname = "localhost"; 10. int port = 6789; 11. SSLSocket clientsocket = null; try { 14. // connect to the server and create a client socket 15. SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault(); 16. clientsocket = (SSLSocket)factory.createSocket(hostname, port); 17. final String[] enabledciphersuites = { "SSL DH anon WITH RC4 128 MD5" }; 18. clientsocket.setenabledciphersuites(enabledciphersuites); BufferedReader is = new BufferedReader( 21. new InputStreamReader(clientSocket.getInputStream())); 22. PrintWriter os = new PrintWriter(new OutputStreamWriter( 23. clientsocket.getoutputstream()), true); // write an integer to the server and receive an integer 26. os.println(7); 27. String line = is.readline(); 28. System.out.println("Server says the square of 7 is: " + line + "."); // clean up 31. os.close(); 32. is.close(); 33. clientsocket.close(); 34. } catch (IOException e) { 35. e.printstacktrace(); 36. } 37. } 38. } Figure 2: Naive socket client. Figure 3 shows the corresponding naive server program. Lines create a server socket using parameters that match those used by the client. In particular, the port number and enabled cipher suites should be the same as those used by the client. Line 23 is a blocking call (serversocket.accept()) that waits for a client to connect. Lines read from the client an integer and then write its square back to the client. Note that reading from the client (is.readline()) is also a blocking call. The client-server socket programs in this section are naive for several reasons. First, the server can only accept one client connection. Second, the protocol is sequential. In a chat program, the server should be able to establish sockets with multiple clients, and the protocol should be parallel. For instance, while a client is waiting for its user to input a message to broadcast to other clients (via the server), it should also be waiting for the server to send it messages from the other clients. Obviously, both a chat 2
3 1. // Figure 3. ServerNaive.java: Naive socket server package jwg.ch2.naive; 4. import java.io.*; 5. import javax.net.ssl.*; public class ServerNaive { 8. public static void main(string args[]) { 9. SSLServerSocket serversocket = null; 10. SSLSocket clientsocket = null; 11. int port = 6789; try { 14. // create a server socket 15. SSLServerSocketFactory factory = (SSLServerSocketFactory) 16. SSLServerSocketFactory.getDefault(); 17. serversocket =(SSLServerSocket)factory.createServerSocket(port); 18. final String[] enabledciphersuites = { "SSL DH anon WITH RC4 128 MD5" }; 19. serversocket.setenabledciphersuites(enabledciphersuites); 20. System.out.println( "Server started, waiting for client..." ); // accept a client connection 23. clientsocket = (SSLSocket)serverSocket.accept(); 24. BufferedReader is = new BufferedReader( 25. new InputStreamReader(clientSocket.getInputStream())); 26. PrintWriter os = new PrintWriter(new OutputStreamWriter( 27. clientsocket.getoutputstream()), true); // read an integer from the client and write its square back 30. String line = is.readline(); 31. System.out.println( "Received " + line + " from client." ); 32. int n = Integer.parseInt(line); 33. os.println( n*n ); // clean up 36. is.close(); 37. os.close(); 38. clientsocket.close(); 39. serversocket.close(); 40. } catch (IOException e) { 41. e.printstacktrace(); 42. } 43. } 44. } Figure 3: Naive socket server. 3
4 server and a chat client should be multi-threaded. The next section presents a command-line solution to multi-threaded chat. 2 Command-Line Chat The code for this section appears in package jwg.ch2.chatcl. To test-run: first run the server (ServerChatCL), then run one or multiple copies of the client (ClientChatCL). Similar to the naive case, you should run these programs in separate command-line consoles outside MyEclipse. This section presents command-line chat programs. They use the basic operators learned from Section 1 (e.g. create SSL sockets, read from the socket and write to the socket), while addressing the limitations. Let s first examine the protocol in Figure 4 and see how it addresses the limitations of the naive socket protocol. CLIENT Connect to server. Create a ClientSideListener. Get a nickname from keyboard. Write the nickname. Get a message from keyboard. Write the message. SERVER Create a server socket. Create a ServerSidePlayerGroup. Accept a client connection. Create a ServerSideListener thread. SERVERSIDELISTENER Read a nickname. Create a ServerSidePlayer object, and add to ServerSidePlayerGroup. Broadcast a welcome message. Read a chat message. Broadcast the chat message. CLIENTSIDELISTENER Read a message. Display the message. Figure 4: The protocol for the command-line chat example. In a chat program, a client should have two separate threads to get user input (from keyboard in this section) and to read server messages from the socket. This is because both of these two operations are blocking calls. For instance, in the naive example (Figure 1), while the singlethreaded client is trying to read an integer (=49) from the socket, it sits there waiting for the server to send a message, and therefore cannot get user input. In the new protocol (Figure 4), the client creates a ClientSideListener thread which takes care of reading from the socket, thereby freeing up the client thread to get user input. In a chat program, the server should be able to establish socket connections with multiple clients. This was not done in the naive example (Figure 1). The new protocol (Figure 4) achieves this goal because the server creates a ServerSideListener thread for every client. A follow-up requirement is that in a chat program, the server should maintain information for all the connected clients (e.g. their client sockets and their nicknames). In the programs developed in this section, two more classes are created for this purpose: ServerSidePlayer and ServerSide- PlayerGroup. A ServerSidePlayer object keeps the information for one connected client. The ServerSidePlayerGroup object simply keeps an array of ServerSidePlayer objects. One might 4
5 wonder: why can t the server simply keep an array of ServerSidePlayer objects without introducing a new ServerSidePlayerGroup class? The answer is that with a wrapper class, operations to access the shared array of ServerSidePlayer objects can be easily synchronized. There are six programs in this section (package jwg.ch2.chatcl): ClientChatCL.java: the client program. ServerChatCL.java: the server program. ClientSideListenerChatCL.java: the client-side listener. ServerSideListenerChatCL.java: the server-side listener. ServerSidePlayerChatCL.java: the information of one client maintained at the server. ServerSidePlayerGroupChatCL.java: the group of client information. All the programs should be easy to understand. ClientChatCL.java, ServerChatCL.java, clientsidelistenerchatcl.java, and ServerSideListener.java closely follow the procedures as described in the corresponding boxes in Figure 4. ServerSidePlayerChatCL.java maintains some information for a client. And ServerSidePlayerGroupChatCL.java simply maintains an array of such client information. Let s examine the implementation of server broadcast to better understand the mechanism. As Figure 4 shows, ServerSideListener has a loop that keeps reading a chat message (from one client) and broadcasting it (to the other clients). Figure 5 shows excerpts of three functions (one in each java source file) that work together to implement server broadcast. ServerSideListenerChatCL.run() Here ServerSideListenerChatCL is a class extending Thread. When a thread executes, its run() function will automatically be called. Here the run() function includes an infinite while loop (Lines 7-10) which reads a chat message (String message=is.readline()) and broadcasts to the other clients (players.broadcast(nickname, message)). ServerSidePlayerGroupChatCL.Broadcast() The ServerSidePlayerGroupChatCL class maintains an ArrayList of ServerSidePlayerChatCL objects, one for each player (client). To broadcast a message from a client with a given nickname, the synchronized Broadcast function examines all these players and call player.send(message) for each player who is not the message source. Please note that if a Java object is visible to multiple threads, all read and write operations to the object should be done through synchronized methods. Java Virtual Machine ensures that (i) invocations to synchronized methods on the same object do not interleave; and (ii) the changes made by a synchronized method are visible to all the other threads. Also note the special syntax of for statement in Line 17. Such a for statement, which was introduced in Java 5, allows easier scanning for a collection. In general the syntax is: for (type item : collection) statement; ServerSidePlayerChatCL.Send() ServerSidePlayerChatCL.java maintains three variables for a client: a PrintWriter for writing to the server through socket, the nickname of the player, and the client socket. To send a message to the client, as Line 33 shows, it calls os.println(message), where os is the PrintWriter object. 5
6 1. // Figure 5. Excerpts of code implementing server broadcast in command-line chat public class ServerSideListenerChatCL extends Thread { 4. private BufferedReader is; 5. private ServerSidePlayerGroupChatCL players; 6. public void run() { 7. while (true) { 8. String message = is.readline(); 9. players.broadcast(nickname, message); 10. } 11. } 12. } public class ServerSidePlayerGroupChatCL { 15. private ArrayList<ServerSidePlayerChatCL> players; 16. synchronized public void Broadcast( String nickname, String message ) { 17. for ( ServerSidePlayerChatCL player: players ) { 18. if (!player.match(nickname)) player.send(message); 19. } 20. } 21. } public class ServerSidePlayerChatCL { 24. private PrintWriter os; 25. public String nickname; 26. private Socket clientsocket; public boolean Match(String nickname) { 29. return nickname.equals(this.nickname); 30. } public void Send(String message) { 33. os.println(message); 34. } 35. } Figure 5: Excerpts of code implementing server broadcast in command-line chat. 6
7 3 GUI Chat The code for this section appears in package jwg.ch2.chatgui. To test-run: first run the server (ServerChatGUI), then run one or multiple copies of the client (ClientChatGUI). Unlike Sections 1 and 2, all these executions can be invoked from within MyEclipse. Since each instance creates a new window (JFrame for the server and JApplet for the client), their output do not interfere with each other. Also, because ClientChatGUI is a JApplet which does not have a main function, you cannot invoke it at command line as you did for the non GUI version. To invoke it at command line you need to run appletviewer. This section transforms the programs in the command-line chat section to include GUI interface. In particular, this section again has six java files, which have one-to-one mapping to the six java files in Section 2. But, there are two additional files: ClientChatGUI.form and ServerChatGUI.form. These are created using Matisse4MyEclipse. This software is included in the MyEclipse professional version. It provides drag-and-drop abilities when designing user interfaces (an ability that used to attract many users to NetBeans). Before studying this section, you should go through the Matisse4MyEclipse tutorial. In the MyEclipse IDE, 1. Select the menu item Help Welcome. 2. Click Tutorials. 3. Select Creating a Swing Application with Matisse4MyEclipse. After you go through the tutorial, you should know these concepts: Any GUI component created by Matisse4MyEclipse has two files: a.java file and a.form file. Open the.form file, and you can see, at the bottom of the window, a source tab and a design tab. Click the design tab to modify the GUI design. Click the source tab to view or modify the java source. You never need to open the.java file directly. In the generated source file, there are two parts you are not supposed to directly change whatsoever. These parts are enclosed within //GEN-BEGIN and //GEN-END. To indirectly change things in these parts, you modify in the GUI design mode. The GUI client is illustrated in Figure 6. The client JApplet has, at the bottom, a line of four components used for sending a message. Besides those, it has four JTabbedPane windows, with labels World, Char+Who+Option, Chat, and System Messages. Let s first explain the four controls at the bottom. The JComboBox with label All. This component tells to whom the message should be sent. The default value means to send to all players currently online. The other choices in the JComboBox are: Local meaning to send only to players in the current scene; Group meaning to send to those players who are in the same group; and Individual meaning to send to an individual recipient. In this sample code, this component is not actually used. 7
8 Figure 6: The GUI client. Figure 7: The GUI server. 8
9 The disabled JComboBox with label Select Recipient. This component should be enabled if Individual is selected in the first component. The intention is that the nickname of one individual recipient can then be selected here. In this sample code, this component is not used, either. The long and empty JTextField. The player can input a message here. The JButton with label Send. Clicking on this button will send the message. Let s now explain the four JTabbedPane windows. The JTabbedPane with label World. This is the main game canvas. The isometric graphics, play sprites, and so on, all should appear here. In this sample code, this panel is empty. The JTabbedPane with several panels labeled Char, Who, and Option. These panels are meant to show supplementary information. For instance, the Char panel should display the player s character data, such as level, life, experience, and so on. The Who panel should display the list of players that are online. The Option panel should allow the users to specify certain preferences, e.g. to block messages that are sent to All. In this sample code, this panel is empty, too. The JTabbedPane with label Chat. messages. This panel contains a JTextArea that displays the chat The JTabbedPane with label System Messages. This panel contains a JTextArea that displays the system messages. The GUI server is illustrated in Figure 7. This simple server GUI only has two pieces: A JTextArea on the top, which is an output console. A JTable at the bottom, which displays the list of clients that are currently connected. Out of the six java source files, only two are significantly different from their counterparts in the command-line chat. These are the client GUI (ClientChatGUI.java) and the server GUI (ServerChat- GUI.java). The rest of this section focuses on explaining the key points in ClientChatGUI.java, after which the server GUI source code should be easy to understand. Figure 8 shows excerpts from ClientChatGUI.java. Line 3 suppresses the warning message that the serializable class does not declare a static final serialversionuid field. Line 4 tells that class ClientChatGUI inherits from JApplet. Lines 5-17 are the init() function, which is automatically called when initializing this JApplet. The initialization code, however, is not directly put inside the init() function. Rather, it is wrapped by the EventQueue.invokeLater() function as follows: java.awt.eventqueue.invokelater(new Runnable() { public void run() { // initialization codes } }); 9
10 1. // Figure 8. ClientChatGUI.java: GUI Chat Client public class ClientChatGUI extends javax.swing.japplet { 5. public void init() { 6. java.awt.eventqueue.invokelater(new Runnable() { 7. public void run() { 8. initcomponents(); (new java.util.timer()).schedule(new java.util.timertask() { 11. public void run() { 12. // omitted: connect to the server and create a client-side listener 13. } 14. }, 50); 15. } 16. }); 17. } private void sendandclear() { 20. String message = jtextfieldinput.gettext(); 21. jtextfieldinput.settext(""); 22. os.println(message); 23. } synchronized public void showchatmessage(string nickname, String message) { 26. jtextareaoutput.append("[[" + nickname + "]] " + message + "\n"); 27. if (jtextareaoutput.getdocument().getlength() > maxtextareaoutputlength) { 28. jtextareaoutput.replacerange("", 0, 200); 29. } 30. jtextareaoutput.setcaretposition(jtextareaoutput.getdocument().getlength()); 31. } private void jtextfieldinputkeytyped(java.awt.event.keyevent evt) { 34. if (evt.getkeychar() == \n ) sendandclear(); 35. } private void jbuttonsendactionperformed(java.awt.event.actionevent evt) { 38. sendandclear(); 39. } //GEN-BEGIN:initComponents 42. private void initcomponents() { // omitted } 43. //GEN-END:initComponents //GEN-BEGIN:variables 46. // omitted 47. //GEN-END: variables 48. } Figure 8: GUI Chat Client. 10
11 Here the invokelater() function takes as input a Runnable object, whose run() function will automatically be called. This method should be used whenever an application thread needs to update the GUI. Because all GUI updates go to the same AWT event queue, this wrapper ensures that the initialization code is executed asynchronously on the AWT event dispatching thread after all pending AWT events have been processed. Without this wrapper, programs with complex GUI might halt during execution. Let s now take a closer look at the initialization code. Line 8 calls the one function whose name is not determined by you or me but by Matisse4MyEclipse. The function is called initcomponents(). It initializes the GUI components. As illustrated later in Lines 41-43, the definition of the function is enclosed between //GEN-BEGIN and //GEN-END. When you use Matisse4MyEclipse to update the GUI interface and save, this function definition will automatically be updated. So be cautious not to directly modify the content of it, but to modify in the design mode. Lines wrap the code to connect to server into Timer.schedule() function. This function has several forms. In the form that our code adopts, the first parameter is a TimerTask object whose run() function contains the connection code, and the second parameter is the delay in terms of milliseconds. For instance, the sample code says to try connect to the server in a separate thread after 50 milliseconds. This wrapping, by connecting to the server asynchronously, allows the GUI to immediately display even if it takes a long time to establish server connection. Lines show how to get a message from the input JTextField, clear the JTextField, and then send the message to the server. Lines are a synchronized function to display a message in the chat window. Here synchronization is needed because both the GUI part (when the user tries to send a message to the server, the message is echoed) and the ClientSideListener (when receiving a server message) will call this function to update the chat window. Lines make sure the chat window does not display too much data (to prevent memory overflow). Whenever the JTextArea for displaying chat messages contains more than a certain number of characters, the oldest 200 characters will be erased. Line 30 sets the caret position to the end of the JTextArea. Even though no caret is shown because the JTextArea is not editable, this statement is still useful because it scrolls the window upwards upon displaying a new message. Lines show two callback functions whose names can be set by you and me, but through Matisse4MyEclipse in the design mode. The name of the callback function jtextfieldinputkeytyped is set as follows. 1. In MyEclipse, open ClientSideGUI.form. This automatically goes to the design mode. 2. Click the input JTextField in the client GUI. 3. If the properties window is not visible, open it by selecting Windows Show View Properties. 4. In the properties window, there are four tabs: Properties, Binding, Events, and Code. Select the Events tab. 5. Scroll down to the keytyped event and provide the desired function name. This function ensures that when the user presses the ENTER key in the input JTextField, the function sendandclear is called. The name of the callback function jbuttonsendactionperformed is set similarly. This function ensures that when the user clicks on the Send button, again the function sendandclear is called. Therefore the user can send a message in two ways. Such flexibility provides slightly more friendly user interface. Finally, Lines show the two parts that are generated by Matisse4MyEclipse: the initcomponents() function and the set of variables of the GUI components. 11
CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary
CPSC 441 Tutorial TCP Server Department of Computer Science University of Calgary TCP Socket Client Server Connection Request Server Listening on welcoming socket Client Socket Server Socket Data Simple
More informationCONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13
CONTENTS Chapter 1 Getting Started with Java SE 6 1 Introduction of Java SE 6... 3 Desktop Improvements... 3 Core Improvements... 4 Getting and Installing Java... 5 A Simple Java Program... 10 Compiling
More informationLab 1 : Java Sockets
Lab 1 : Java Sockets 1. Goals In this lab you will work with a low-level mechanism for distributed communication. You will discover that Java sockets do not provide: - location transparency - naming transparency
More informationVirtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.
1 - What if the main method is declared as private? 1. The program does not compile 2. The program compiles but does not run 3. The program compiles and runs properly ( From Lectuer # 2) 4. The program
More informationMultimedia Programming
Multimedia Programming Medialogy, 8 th Semester, Aalborg University Wednesday 6 June 2012, 09.00 12.00 Instructions and notes You have 3 hours to complete this examination. Neither written material nor
More informationWeek 13 Lab - Exploring Connections & Remote Execution
Week 13 Lab - Exploring Connections & Remote Execution COSC244 & TELE202 1 Assessment This lab is worth 0.5%. The marks are awarded for completing the programming exercise and answering the questions.
More informationJava: Graphical User Interfaces (GUI)
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, and Bertrand Meyer Java: Graphical User Interfaces (GUI) With material from Christoph Angerer The essence of the Java Graphics API Application
More informationCS 351 Design of Large Programs Sockets Example
CS 351 Design of Large Programs Sockets Example Brooke Chenoweth University of New Mexico Spring 2019 Socket Socket(String host, int port) InputStream getinputstream() OutputStream getoutputstream() void
More informationJava Intro 3. Java Intro 3. Class Libraries and the Java API. Outline
Java Intro 3 9/7/2007 1 Java Intro 3 Outline Java API Packages Access Rules, Class Visibility Strings as Objects Wrapper classes Static Attributes & Methods Hello World details 9/7/2007 2 Class Libraries
More informationCome & Join Us at VUSTUDENTS.net
Come & Join Us at VUSTUDENTS.net For Assignment Solution, GDB, Online Quizzes, Helping Study material, Past Solved Papers, Solved MCQs, Current Papers, E-Books & more. Go to http://www.vustudents.net and
More informationObject-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming)
Object-Oriented Software Engineering (Re-exam for Object-Oriented Analysis, Design and Programming) Medialogy, 4 th Semester, Aalborg Monday 11 June 2012, 09.00 12.00 Instructions You have 3 hours to complete
More informationGive one example where you might wish to use a three dimensional array
CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming
More informationHanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS
Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Glossary of Terms 2-4 Step by Step Instructions 4-7 HWApp 8 HWFrame 9 Never trust a computer
More informationG51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions
G51PGP Programming Paradigms Lecture 009 Concurrency, exceptions 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals = new Animal[6]; animals[0]
More informationDHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)
DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR 2018-19 (ODD SEM) DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING SUB: OBJECT ORIENTED PROGRAMMING SEM/YEAR: III SEM/ II YEAR
More informationEvent Driven Programming
Event Driven Programming Part 1 Introduction Chapter 12 CS 2334 University of Oklahoma Brian F. Veale 1 Graphical User Interfaces So far, we have only dealt with console-based programs Run from the console
More informationTHIS EXAMINATION PAPER MUST NOT BE REMOVED FROM THE EXAMINATION ROOM
UNIVERSITY OF LONDON GOLDSMITHS COLLEGE B. Sc. Examination 2012 COMPUTER SCIENCE IS52025A Internet and Distributed Programming Duration: 2 hours 15 minutes Date and time: There are five questions in this
More informationCS 51 Laboratory # 12
CS 51 Laboratory # 12 Pictionary Objective: To gain experience with Streams. Networked Pictionary This week s assignment will give you the opportunity to practice working with Streams in the context of
More informationSELF-STUDY. Glossary
SELF-STUDY 231 Glossary HTML (Hyper Text Markup Language - the language used to code web pages) tags used to embed an applet. abstract A class or method that is incompletely defined,
More informationICOM 4015-Advanced Programming. Spring Instructor: Dr. Amir H. Chinaei. TAs: Hector Franqui, Jose Garcia, and Antonio Tapia. Reference: Big Java
ICOM 4015-Advanced Programming Spring 2014 Instructor: Dr. Amir H. Chinaei TAs: Hector Franqui, Jose Garcia, and Antonio Tapia Reference: Big Java By Hortsmann, Ed 4 Lab 7 Continuation of HTTP and Introduction
More informationReading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.
Simple Networking Loading applets from the network. Applets are referenced in a HTML file. Java programs can use URLs to connect to and retrieve information over the network. Uniform Resource Locator (URL)
More information1 OBJECT-ORIENTED PROGRAMMING 1
PREFACE xvii 1 OBJECT-ORIENTED PROGRAMMING 1 1.1 Object-Oriented and Procedural Programming 2 Top-Down Design and Procedural Programming, 3 Problems with Top-Down Design, 3 Classes and Objects, 4 Fields
More informationJava Socket Application. Distributed Systems IT332
Java Socket Application Distributed Systems IT332 Outline Socket Communication Socket packages in Java Multithreaded Server Socket Communication A distributed system based on the client server model consists
More informationCS 251 Intermediate Programming GUIs: Event Listeners
CS 251 Intermediate Programming GUIs: Event Listeners Brooke Chenoweth University of New Mexico Fall 2017 What is an Event Listener? A small class that implements a particular listener interface. Listener
More informationGraphical User Interfaces (GUIs)
CMSC 132: Object-Oriented Programming II Graphical User Interfaces (GUIs) Department of Computer Science University of Maryland, College Park Model-View-Controller (MVC) Model for GUI programming (Xerox
More informationJava for Interfaces and Networks
Java for Interfaces and Networks Threads and Networking Federico Pecora School of Science and Technology Örebro University federico.pecora@oru.se Federico Pecora Java for Interfaces and Networks Lecture
More informationIntroduction to concurrency and GUIs
Principles of Software Construction: Objects, Design, and Concurrency Part 2: Designing (Sub)systems Introduction to concurrency and GUIs Charlie Garrod Bogdan Vasilescu School of Computer Science 1 Administrivia
More informationGraphical User Interface (GUI)
Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware
More informationPieter van den Hombergh Richard van den Ham. March 17, 2018
: Network : Network, Object Pieter van den Hombergh Richard van den Ham Fontys Hogeschool voor Techniek en Logistiek March 17, 2018 /FHTenL : Network March 17, 2018 1/21 Topics, Object Some everyday life
More informationMultiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)
M257 MTA Spring2010 Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks) 1. If we need various objects that are similar in structure, but
More informationChapter 3: A Larger Example: SocketChat
page 1 Chapter 3: A Larger Example: SocketChat In this chapter we are going to look at three versions of a larger socket-based example: a simple `chat' application. The application does not have many capabilities,
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More informationJava Input/Output. 11 April 2013 OSU CSE 1
Java Input/Output 11 April 2013 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter
More informationInput from Files. Buffered Reader
Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads
More informationControl Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User
CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by
More informationCS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1
P a g e 1 CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1 Q1 Describe some Characteristics/Advantages of Java Language? (P#12, 13, 14) 1. Java
More informationInteractive Programming In Java
IPIJ: Chapter Outlines Page 1 Front Matter I. Table of Contents 2. Preface Introduction to Interactive Programming by Lynn Andrea Stein A Rethinking CS101 Project Interactive Programming In Java Chapter
More informationNetworking Code CSCI 201 Principles of Software Development
Networking Code CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Server Networking Client Networking Program Outline USC CSCI 201L Server Software A server application
More informationVALLIAMMAI ENGINEERING COLLEGE
VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 60 0 DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK III SEMESTER CS89- Object Oriented Programming Regulation 07 Academic Year 08 9 Prepared
More informationCOMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming
COMP 213 Advanced Object-oriented Programming Lecture 20 Network Programming Network Programming A network consists of several computers connected so that data can be sent from one to another. Network
More informationJonathan Aldrich Charlie Garrod
Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Introduction to GUIs Jonathan Aldrich Charlie Garrod School of Computer Science 1 Administrivia Homework
More informationBuilding a Java First-Person Shooter
Building a Java First-Person Shooter Episode 2 [Last update: 4/30/2017] Objectives This episode adds the basic elements of our game loop to the program. In addition, it introduces the concept of threads.
More informationTable of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...
Table of Contents Chapter 1 Getting Started with Java SE 7 1 Introduction of Java SE 7... 2 Exploring the Features of Java... 3 Exploring Features of Java SE 7... 4 Introducing Java Environment... 5 Explaining
More informationChapter 4: Processes
Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems 4.1 Process Concept An operating
More informationCS506 Web Design & Development Final Term Solved MCQs with Reference
with Reference I am student in MCS (Virtual University of Pakistan). All the MCQs are solved by me. I followed the Moaaz pattern in Writing and Layout this document. Because many students are familiar
More informationSchool of Informatics, University of Edinburgh
CS1Bh Lecture Note 16 Case Study: Client/Server In this case study we look at providing a slightly more realistic client/server application and explore some of the issues that arise in this context. Specifically
More informationJava s Implementation of Concurrency, and how to use it in our applications.
Java s Implementation of Concurrency, and how to use it in our applications. 1 An application running on a single CPU often appears to perform many tasks at the same time. For example, a streaming audio/video
More informationGraphical User Interface (GUI)
Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware
More informationJava Support for developing TCP Network Based Programs
Java Support for developing TCP Network Based Programs 1 How to Write a Network Based Program (In Java) As mentioned, we will use the TCP Transport Protocol. To communicate over TCP, a client program and
More informationThe JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class
CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical
More informationCourse Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski
CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule
More informationNetwork Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff
Network Programming by Vlad Costel Ungureanu for Learn Stuff Java Network Protocols 2 Java Network Protocols 3 Addresses Innet4Address (32-bit) 85.122.23.145 - numeric pentalog.com symbolic Innet6Address
More informationSupporting Materials
Preface p. xxiii Introduction p. xxiii Key Features p. xxiii Chapter Outlines p. xxiv Supporting Materials p. xxvi Acknowledgments p. xxvii Java Fundamentals p. 1 Bits, Bytes, and Java p. 2 The Challenge
More informationPart I: Learn Common Graphics Components
OOP GUI Components and Event Handling Page 1 Objectives 1. Practice creating and using graphical components. 2. Practice adding Event Listeners to handle the events and do something. 3. Learn how to connect
More informationQuiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java
CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4 Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce
More informationB2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA
B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE
More informationprotocols September 15,
Contents SCI 351 4 Protocols, WWW Internet applications WWW, document technology Lennart Herlaar Original slides by Piet van Oostrum September 15, 2003 SCI351-4 1 X SCI351-4 1 X Internet applications How
More informationLesson 3: Accepting User Input and Using Different Methods for Output
Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program
More informationB2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA
B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE
More informationNetworking Basics. network communication.
JAVA NETWORKING API Networking Basics When you write Java programs that communicate over the network, you are programming at the application layer. Typically, you don't need to concern yourself with the
More informationAssignment 1. Due date February 6, 2007 at 11pm. It must be submitted using submit command.
Assignment 1 Due date February 6, 2007 at 11pm. It must be submitted using submit command. Note: submit 4213 a1 . Read the manpages ("man submit") for more details on the submit command. It is
More informationSyllabus & Curriculum for Certificate Course in Java. CALL: , for Queries
1 CONTENTS 1. Introduction to Java 2. Holding Data 3. Controllin g the f l o w 4. Object Oriented Programming Concepts 5. Inheritance & Packaging 6. Handling Error/Exceptions 7. Handling Strings 8. Threads
More informationNetwork. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark
Network Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark jbb@ase.au.dk Outline Socket programming If we have the time: Remote method invocation (RMI) 2 Socket Programming Sockets
More informationLTBP INDUSTRIAL TRAINING INSTITUTE
Java SE Introduction to Java JDK JRE Discussion of Java features and OOPS Concepts Installation of Netbeans IDE Datatypes primitive data types non-primitive data types Variable declaration Operators Control
More informationPrinciples, Models, and Applications for Distributed Systems M
Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Lab assignment 4 (worked-out) Connection-oriented Java Sockets Luca Foschini Winter
More informationDEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1
DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 Objectives 1 6.1 Methods 1 void or return 1 Parameters 1 Invocation 1 Pass by value 1 6.2 GUI 2 JButton 2 6.3 Patterns
More informationGraphical interfaces & event-driven programming
Graphical interfaces & event-driven programming Lecture 12 of TDA 540 (Objektorienterad Programmering) Carlo A. Furia Alex Gerdes Chalmers University of Technology Gothenburg University Fall 2017 Pop quiz!
More informationTommy Färnqvist, IDA, Linköping University
Lecture 4 Threads and Networking in Java TDDC32 Lecture notes in Design and Implementation of a Software Module in Java 23 January 2013 Tommy Färnqvist, IDA, Linköping University 4.1 Lecture Topics Contents
More informationCS 351 Design of Large Programs Threads and Concurrency
CS 351 Design of Large Programs Threads and Concurrency Brooke Chenoweth University of New Mexico Spring 2018 Concurrency in Java Java has basic concurrency support built into the language. Also has high-level
More informationCS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets
CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets (like simple DNS) 3. Programs using Raw sockets (like packet
More informationInput, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003
Input, Output and Exceptions COMS W1007 Introduction to Computer Science Christopher Conway 24 June 2003 Input vs. Output We define input and output from the perspective of the programmer. Input is data
More informationWeb Server Project. Tom Kelliher, CS points, due May 4, 2011
Web Server Project Tom Kelliher, CS 325 100 points, due May 4, 2011 Introduction (From Kurose & Ross, 4th ed.) In this project you will develop a Web server in two steps. In the end, you will have built
More informationJava for Interfaces and Networks (DT3029)
Java for Interfaces and Networks (DT3029) Lecture 3 Threads and Networking Federico Pecora federico.pecora@oru.se Center for Applied Autonomous Sensor Systems (AASS) Örebro University, Sweden Capiscum
More informationIntroduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014
Introduction to GUIs Principles of Software Construction: Objects, Design, and Concurrency Jonathan Aldrich and Charlie Garrod Fall 2014 Slides copyright 2014 by Jonathan Aldrich, Charlie Garrod, Christian
More informationIT101. Graphical User Interface
IT101 Graphical User Interface Foundation Swing is a platform-independent set of Java classes used for user Graphical User Interface (GUI) programming. Abstract Window Toolkit (AWT) is an older Java GUI
More information13 th Windsor Regional Secondary School Computer Programming Competition
SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse
More informationM257 Past Paper Oct 2007 Attempted Solution
M257 Past Paper Oct 2007 Attempted Solution Part 1 Question 1 The compilation process translates the source code of a Java program into bytecode, which is an intermediate language. The Java interpreter
More informationAdvanced Java Programming. Networking
Advanced Java Programming Networking Eran Werner and Ohad Barzilay Tel-Aviv University Advanced Java Programming, Spring 2006 1 Overview of networking Advanced Java Programming, Spring 2006 2 TCP/IP protocol
More informationAsynchronous Communication using Messaging
FACULTY OF AUTOMATION AND COMPUTER SCIENCE COMPUTER SCIENCE DEPARTMENT DISTRIBUTED SYSTEMS Assignment 3 Communication using Messaging A 3.1: The Basics Ioan Salomie Tudor Cioara Ionut Anghel Marcel Antal
More informationIntroduction This assignment will ask that you write a simple graphical user interface (GUI).
Computing and Information Systems/Creative Computing University of London International Programmes 2910220: Graphical Object-Oriented and Internet programming in Java Coursework one 2011-12 Introduction
More informationSwing: Building GUIs in Java
Swing: Building GUIs in Java ENGI 5895: Software Design Andrew Vardy Faculty of Engineering & Applied Science Memorial University of Newfoundland February 13, 2017 Outline 1 Introduction 2 Aside: Inner
More informationCSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013
CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 Name: This exam consists of 5 problems on the following 6 pages. You may use your double-sided hand-written 8 ½ x 11 note sheet
More informationCSCD 330 Network Programming Spring 2018
CSCD 330 Network Programming Spring 2018 Lecture 7 Application Layer Socket Programming in Java Reading: Chapter 2, Java links Relevant Links page Some Material in these slides from J.F Kurose and K.W.
More informationJava for Programmers Course (equivalent to SL 275) 36 Contact Hours
Java for Programmers Course (equivalent to SL 275) 36 Contact Hours Course Overview This course teaches programmers the skills necessary to create Java programming system applications and satisfies the
More informationDOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS
Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully
More informationA Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.
A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p. 9 Classes and Objects p. 11 Creating Objects p. 12 Static or
More informationCSCD 330 Network Programming Spring 2018
CSCD 330 Network Programming Spring 2018 Lecture 6 Application Layer Socket Programming in Java Reading for Java Client/Server see Relevant Links Some Material in these slides from J.F Kurose and K.W.
More informationCore Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)
Overview: Java programming language is developed by Sun Microsystems. Java is object oriented, platform independent, simple, secure, architectural neutral, portable, robust, multi-threaded, high performance,
More informationJava Networking (sockets)
Java Networking (sockets) Rui Moreira Links: http://java.sun.com/docs/books/tutorial/networking/toc.html#sockets http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets_p.html Networking Computers
More informationJava Programming. Price $ (inc GST)
1800 ULEARN (853 276) www.ddls.com.au Java Programming Length 5 days Price $4235.00 (inc GST) Overview Intensive and hands-on, the course emphasizes becoming productive quickly as a Java application developer.
More informationMidterm assessment - MAKEUP Fall 2010
M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:
More informationInternet Technology 2/7/2013
Sample Client-Server Program Internet Technology 02r. Programming with Sockets Paul Krzyzanowski Rutgers University Spring 2013 To illustrate programming with TCP/IP sockets, we ll write a small client-server
More informationCSCD 330 Network Programming Winter 2019
CSCD 330 Network Programming Winter 2019 Lecture 7 Application Layer Socket Programming in Java Reading: Chapter 2, Java links Relevant Links page Some Material in these slides from J.F Kurose and K.W.
More informationSwing: Building GUIs in Java
Swing: Building GUIs in Java ENGI 5895: Software Design Andrew Vardy Faculty of Engineering & Applied Science Memorial University of Newfoundland February 13, 2017 Outline 1 Introduction 2 Aside: Inner
More informationCMSC 331 Second Midterm Exam
1 20/ 2 80/ 331 First Midterm Exam 11 November 2003 3 20/ 4 40/ 5 10/ CMSC 331 Second Midterm Exam 6 15/ 7 15/ Name: Student ID#: 200/ You will have seventy-five (75) minutes to complete this closed book
More informationSimple Java Reference
Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...
More informationIntroduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1
Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1 Introducing Object Oriented Programming... 2 Explaining OOP concepts... 2 Objects...3
More informationMerge Sort Quicksort 9 Abstract Windowing Toolkit & Swing Abstract Windowing Toolkit (AWT) vs. Swing AWT GUI Components Layout Managers Swing GUI
COURSE TITLE :Introduction to Programming 2 COURSE PREREQUISITE :Introduction to Programming 1 COURSE DURATION :16 weeks (3 hours/week) COURSE METHODOLOGY:Combination of lecture and laboratory exercises
More informationString temp [] = {"a", "b", "c"}; where temp[] is String array.
SCJP 1.6 (CX-310-065, CX-310-066) Subject: String, I/O, Formatting, Regex, Serializable, Console Total Questions : 57 Prepared by : http://www.javacertifications.net SCJP 6.0: String,Files,IO,Date and
More informationWhich of the following syntax used to attach an input stream to console?
Which of the following syntax used to attach an input stream to console? FileReader fr = new FileReader( input.txt ); FileReader fr = new FileReader(FileDescriptor.in); FileReader fr = new FileReader(FileDescriptor);
More information