Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
How can I Combining two concepts, Threads and Networking, in one java application
Server(
mport java.io.*;
import java.net. *;
import java.util.Scanner;
public class Server{
public static void main (String arg[]) throws Exception {
/*1.connection*/
ServerSocket serverSocket = new ServerSocket(6789);
Socket connectionSocket = serverSocket.acceptO;
/*2. A scanner or a BufferedReader for reading from the socket */
Scanner scanner = new Scanner(connectionSocket.getInputStreamO);
/*3. A DataOutputStream object to write in the socket*/
DataOutputStream outToClient = new DataOutputStream (connectionSocket.getOutputStream)
/*4. reading from the socket*/
String clientSentence = scanner.nextLine0;
System.out.printin("Received: " + clientSentence);
/*5. modification of the sentence*/
String modifiedSentence=clientSentence.toUpperCaseO;
/*6. writing in the socket*/
outToClient.writeBytes(modifiedSentence+'(n');
/*7.closing connections*/
scanner.close©;
connectionSocket.closeO;
}
}
Client (
import java.io.
import java.net.*
import java.util.Scanner;
public class Clientf
public static void main(String argv[]) throws Exception {
/*1. Connection*/
Socket connectionSocket = new Socket ("localhost", 6789);
Scanner scanner, scannerFromSocket;
DataOutputStream outToServer;
String sentenceFromServer;
String clientSentence;
*2. Scanner to read from keyboard */
scanner = new Scanner(System.in);
/*3. Scanner to read from the socket */
scanner FromSocket = new Scanner (connectionSocket.getInputStream));
/*4. DataOutputStream to write in the socket */
outToServer = new DataOutputStream (connectionSocket.getOutputStream0);
/*5. receiving and sending messages*/
clientSentence=scanner.nextLine0;
outToServer.writeBytes(clientSentence+\n');
sentenceFromServer-scannerFromSocket.nextLine0;
System.out.printin(sentenceFromServer);
scanner FromSocket.closeO;
scanner.close0;
connectionSocket.close0;
}
}
Expert Solution
arrow_forward
Step 1 Introduction to threads and networking
It is defined as the main thread. In java all the programs will be having atleast one thread that is known as main thread.
This will be created by the Java Virtual Machine (JVM) at the program’s start, when the main() method is invoked with the main thread.
Creating a thread is accomplished by implementing an interface and extending a class. Every Java thread is created and controlled by the java.lang.Thread class.
Java is a premier language for network programming. java.net package encapsulate large number of classes and interface that provides an easy-to use means to access network resources.
Step by stepSolved in 2 steps
Knowledge Booster
Similar questions
- import org.firmata4j.IODevice;import org.firmata4j.Pin;import org.firmata4j.firmata.FirmataDevice;import org.firmata4j.ssd1306.SSD1306;import java.io.IOException;import java.util.HashMap;import java.util.TimerTask;public class minorproj extends TimerTask {static String recLog = "\"/dev/cu.usbserial-0001\"";static IODevice myGroveBoard;private final SSD1306 theOledObject;private Pin MoistureSensor;private Pin WaterPump;private Pin Button;private int sampleCount;public minorproj(SSD1306 theOledObject, Pin Button, Pin MoistureSensor, Pin WaterPump) {this.theOledObject = theOledObject;this.Button = Button;this.MoistureSensor = MoistureSensor;this.WaterPump = WaterPump;this.sampleCount = 0;}double startTime = System.currentTimeMillis();@Overridepublic void run() {while (true) {{// check if button is pressedif (this.Button.getValue() == 1) {break; // exit loop and stop program}String VolValue = String.valueOf(MoistureSensor.getValue());System.out.println("Moisture Sensor Value:" +…arrow_forwardWrite a multithreaded Hello World program using OpenMP. Have each thread say “Hello world” along with its thread ID and the number of threads the program is using.Run the program with as many threads as your computer will allow. Then try setting the thread count to a different number. please post:- The source code.- A screenshot of the program running.- A screenshot of the program running with a different thread count.arrow_forwardSolution question number 2arrow_forward
- import org.firmata4j.IODevice;import org.firmata4j.Pin;import org.firmata4j.firmata.FirmataDevice;import org.firmata4j.ssd1306.SSD1306;import java.io.IOException;import java.util.HashMap;import java.util.TimerTask;public class minorproj extends TimerTask {static String recLog = "\"/dev/cu.usbserial-0001\"";static IODevice myGroveBoard;private final SSD1306 theOledObject;private Pin MoistureSensor;private Pin WaterPump;private Pin Button;private int sampleCount;public minorproj(SSD1306 theOledObject, Pin Button, Pin MoistureSensor, Pin WaterPump) {this.theOledObject = theOledObject;this.Button = Button;this.MoistureSensor = MoistureSensor;this.WaterPump = WaterPump;this.sampleCount = 0;}double startTime = System.currentTimeMillis();@Overridepublic void run() {while (true) {{// check if button is pressedif (this.Button.getValue() == 1) {break; // exit loop and stop program}String VolValue = String.valueOf(MoistureSensor.getValue());System.out.println("Moisture Sensor Value:" +…arrow_forward2. LAMP stands for "Linux, Apache, MySQL, and PHP," and it is a web hosting stack that fits well together. The letters denote and.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education