ALL >> Computer-Programming >> View Article
Socket Programming In Java
Java Socket programming is used to communicate between 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 pieces of information:1) the server's IP Address and 2) the Port number.
1) First open the two notepads.
A] In the first notepad, type the code for server side and save it as the same class name which you have provided in the code and with the extension .java
For Example, MyServer1.java
import java.io.*; import java.net.*; public class MyServer1 { public static void main(String[] args) { try { ServerSocket ss=new ServerSocket(6666); Socket s=ss.accept();//establishes connection DataInputStream dis=new DataInputStream(s.getInputStream()); String str=(String)dis.readUTF(); System.out.println("message= "+str); System.out.println("message= "+str); System.out.println("message= "+str); ss.close(); } catch(Exception ...
... e) { System.out.println(e); } } }
B] In second notepad, type the code for the client side and save it as the same class name which you have provided in the code and with the extension .java
For Example, MyClient1.java
import java.io.*; import java.net.*; public class MyClient1 { public static void main(String[] args) { try { Socket s=new Socket("localhost",6666); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); dout.writeUTF("Hello Server"); dout.flush(); dout.close(); s.close(); } catch(Exception e) { System.out.println(e); } } }
2) Output for the code:-
A] Server-Side :
Socket Programming
B] Client-Side :
Socket Programming
Launched to the world in 2017, Wisemonkeys(https://wisemonkeys.info/) is now a robust Learning management system.
Just follow a 3-step registration process and get connected. Since we appreciate genuine users and do not encourage spammers we follow small registration process:
1. Sign up(https://me.wisemonkeys.info/login)
2. Confirm your email. (for the first time the email might fall into your spam/junk/promotion folder. Please mark it as not spam and confirm the link).
3. Login and get started.
4. Or log in via Google/Microsoft.
Our hardworking team is thriving hard to make this platform better and better. If you have any suggestions and feedback, then do write to us at: genius@wisemonkeys.info
Add Comment
Computer Programming Articles
1. Which Are The Best Java Coding Classes In Bhopal?Author: Shankar Singh
2. Warehouse Management In Zambia: Essential Features To Look For
Author: Doris Rose
3. Ecommerce Web Design And Development In Melbourne With The Merchant Buddy
Author: themerchantbuddy
4. Why Website Maintenance Is Crucial For Business Success
Author: Yogendra Shinde
5. Boost Your Business With Smart Invoice Pos Software In Zambia
Author: Cecilia Robert
6. How Stablecoin Development Ensures Stability And Security?
Author: Michael noah
7. Công Cụ Tính Chiều Cao Chuẩn Từ Minbin Tool: Đo Lường Và Cải Thiện Chiều Cao Hiệu Quả
Author: KenJi123
8. How To Make A Courier App For Courier Delivery And Tracking Service
Author: Deorwine Infotech
9. Reputation Management In The Digital Age: Protecting And Enhancing Your Law Firm’s Image
Author: jamewilliams
10. What Features Should I Look For In Wordpress Ecommerce Plugins?
Author: Rocket Press
11. Staying Updated With The Latest Gaming News
Author: Next Tech Plus
12. Game Development: Evolving Technologies And New Horizons
Author: Rick John
13. Why Svelte Is The Most In-demand Framework For Web Development?
Author: Why Svelte Is The Most In-Demand Framework For Web
14. Maximizing Ebay Sales With Maropost/neto And Pursuit Info Solutions
Author: rachelvander
15. The Importance Of Software Testing: Ensuring Quality In Development
Author: John Mathew