w 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
w 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
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
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
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 step
Solved in 2 steps
Recommended textbooks for you
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
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