wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Socket Programming in Java

profile
Kiddoneeta
Oct 26, 2017
0 Likes
0 Discussions
697 Reads
 
  • 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:1) IP Address of Server, and 2)Port number.

1) First open the two notepad.

A] In 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

Comments ()


Sign in

Read Next

Answer

Blog banner

How Does Preschool Help Children Become Better Decision Makers?

Blog banner

DMZ: Your Secret Weapon for Data Security

Blog banner

SQL Injection practice on DVWA

Blog banner

Your Guide to Great Flossing, by the best affordable dentist in Maroubra

Blog banner

Apache Spark :- Powerful Data Processing Tool

Blog banner

Why Progressive Web Apps (PWAs) Are Replacing Traditional Websites

Blog banner

Different types of scam frauds

Blog banner

Marvel Cinematic Universe

Blog banner

Fault Tolerance in an Operating System

Blog banner

10 Unsolved Mysteries all over the world

Blog banner

How to Grow Your Brand on YouTube Without a Big Budget

Blog banner

Kayak Eco Tours Near Arcadia: A Complete Guide for Nature Lovers

Blog banner

Virtual Memory

Blog banner

Patola Outfits for the Modern Wardrobe: Reviving Indian Handloom in Style

Blog banner

Traditional UNIX Scheduling

Blog banner

Classification Vs Clustring? What's the diffrence?

Blog banner

1.1 basic elements

Blog banner

social media issue

Blog banner

MEMORY MANAGEMENT (techniques)

Blog banner

The Lunchbox That Came Back Untouched — How to Handle a Fussy Eater at Preschool Age

Blog banner

Modern Operating system

Blog banner

Strengthening Active Directory Security

Blog banner

virtual machine

Blog banner

Meal Maharaj — 3 CP, 5 CP, 8 CP. Same Love, Different Portions

Blog banner

What is Network Security?

Blog banner

GIS Bharat Maps

Blog banner

note taker app

Blog banner

Vulnerability Assessment (Vulnerability Analysis)

Blog banner

Security in Cloud Computing Environment using cryptography - Rushabh Modi

Blog banner

Google classroom

Blog banner

On-Page SEO

Blog banner

Uniprocessor and Types

Blog banner

Why You Should Not Use Free VPNs

Blog banner

Music is life

Blog banner

Deadlock and Starvation

Blog banner

gis substation

Blog banner

Stay Close To Adventure In Arcadia, Florida At Oak Tree Hotel

Blog banner

Fault tolerance

Blog banner

Pandas Matrix Applications

Blog banner

Data Security and Data Privacy in Data Science

Blog banner

Threads in OS

Blog banner