123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Socket Programming In Java

Profile Picture
By Author: Wisemonkeys
Total Articles: 277
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

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

Total Views: 299Word Count: 315See All articles From Author

Add Comment

Computer Programming Articles

1. How Mobile Applications Improve Business Productivity: A Complete Guide For Modern Businesses
Author: Rudram Technology Solutions

2. Hour Of Code: Unlock The Power Of Programming
Author: LuxCore LLC

3. Ethical Hacking Tutorial: A Complete Guide For Beginners
Author: Tech Point

4. How Charging Station Management Software Helps Boost Your Ev Charging Station Business
Author: TelioEV

5. Construction Inventory Management: A Practical Guide To Tracking Materials, Tools & Costs
Author: mary

6. Digital Transformation And Software Development Trends
Author: brainbell10

7. How To Manage Offshore Software Development?
Author: brainbell10

8. 3d Slot Game Development Solutions For Immersive Casino Experiences: The Complete Guide
Author: Lalit Kumar Gupta

9. Maximize Your Business Growth With Microsoft Dynamics 365
Author: brainbell10

10. How To Optimize Your Web App Development Costs?
Author: brainbell10

11. Google Cloud Data Engineer Course | Gcp Training & Certification
Author: ravinder

12. Less Tutorial: A Complete Guide For Beginners To Advanced Learning
Author: Tech Point

13. Complete Sass Tutorial For Beginners: Master Modern Css Preprocessing
Author: Tech Point

14. Factors For Choosing Restaurant App Development For Your Business
Author: brainbell10

15. How Custom Web Application Development Improves Business Efficiency
Author: Rudram Technology Solutions

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: