Explore Westonci.ca, the top Q&A platform where your questions are answered by professionals and enthusiasts alike. Get quick and reliable solutions to your questions from a community of seasoned experts on our user-friendly platform. Join our Q&A platform to connect with experts dedicated to providing accurate answers to your questions in various fields.
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());
Thanks for using our platform. We aim to provide accurate and up-to-date answers to all your queries. Come back soon. We hope you found what you were looking for. Feel free to revisit us for more answers and updated information. Thank you for visiting Westonci.ca, your go-to source for reliable answers. Come back soon for more expert insights.