Explore Westonci.ca, the premier Q&A site that helps you find precise answers to your questions, no matter the topic. Explore comprehensive solutions to your questions from knowledgeable professionals across various fields on our platform. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform.
Sagot :
Answer:
Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
The client in socket programming must know two information:
IP Address of Server, and
Port number.
Here, we are going to make one-way client and server communication. In this application, client sends a message to the server, server reads the message and prints it. Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the console until the client is connected. After the successful connection of client, it returns the instance of Socket at server-side.
Explanation:
Creating Client:
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection.
DataInputStream dis=new DataInputStream(s.getInputStream());
We hope this was helpful. Please come back whenever you need more information or answers to your queries. Your visit means a lot to us. Don't hesitate to return for more reliable answers to any questions you may have. Thank you for visiting Westonci.ca. Stay informed by coming back for more detailed answers.