wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Socket Programming in Java

profile
Kiddoneeta
Oct 26, 2017
0 Likes
0 Discussions
696 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

Throttle engine ’Sneak peek into the future’

Blog banner

A Traveller’s Guide to Offbeat Places in Arcadia, Florida

Blog banner

Photography

Blog banner

Processes: Process Description and Control.

Blog banner

Multiprocessor and scheduling

Blog banner

Memory Management

Blog banner

Starvation

Blog banner

How International Schools Build Global-Minded Students through Curriculum & Activities

Blog banner

In the world of Technology...

Blog banner

Buffers in Operating Systems

Blog banner

Os Virtual Memory

Blog banner

MySQL

Blog banner

Distributed Denial of Service (DDoS) attack

Blog banner

Mumbai famous street food

Blog banner

Starvation and Deadlock.

Blog banner

Emotional Intelligence in Children: Why It Is as Important as Academics

Blog banner

Why Your Child Cries at Drop-Off and How Preschools Handle It Gently

Blog banner

Introduction to Virtual Memory - 080

Blog banner

Memory Management in Operating System

Blog banner

SECURITY TOOLS

Blog banner

Web browser forensics:Tools,Evidence collection and analysis

Blog banner

Note Taker App

Blog banner

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

Blog banner

Blockchain uses and use cases

Blog banner

Have You Explored India Yet?

Blog banner

Computer security techniques

Blog banner

Introduction to Data Science: Life Cycle & Applications

Blog banner

?What Children Learn Between Activities: The Hidden Learning Moments in a Preschool Day

Blog banner

Why Are So Many Adults Getting Diagnosed with ADHD?

Blog banner

Theads

Blog banner

Be you

Blog banner

Evolution of operating system

Blog banner

A Survey of Anti-Forensic Techniques: Methods, Challenges, and Countermeasures

Blog banner

The Dark Web: A Breeding Ground for Cybercriminals – How to Guard Against Threats

Blog banner

Why Friendship at Work is Important

Blog banner

Virtual Machine

Blog banner

Animal’s have my heart

Blog banner

E-security systems | CCTV

Blog banner

Clustering Techniques

Blog banner

Access management

Blog banner

The Rise of Polo Tourism in the USA: How Travellers Are Blending Luxury Stays with Elite Sports

Blog banner

Virtual Memory

Blog banner