wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Socket Programming in Java

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

Why Businesses Are Investing More in Automation than Advertising?

Blog banner

DISK SCHEDULING

Blog banner

Toothache at night? What does this mean, and how to cope with it?

Blog banner

A Happier Workplace Starts with Healthy Lunches by Meal Maharaj

Blog banner

LINUX

Blog banner

Cherish the Craft — Essential Tips to Maintain Your Patola Collection

Blog banner

Swiggi

Blog banner

Deadlock and starvation

Blog banner

Safeguarding Your Data: The Importance of Wireless Encryption

Blog banner

How to make Pancakes

Blog banner

IT service level agreement

Blog banner

Carrot Pickle With Raisins (lagan Nu Achar)

Blog banner

Why Soft Skills Matter as Much as Grades?

Blog banner

10 Things To Do On Valentine's Day If You're Single

Blog banner

Memory Management

Blog banner

Getting to Kashmir: Alternative to the Jammu-Srinagar highway

Blog banner

The 60-Minute Window: What to Do (And What NOT to Do) When You Knock Out a Tooth

Blog banner

MEMORY HIERARCHY

Blog banner

How To Secure Operating Systems ?

Blog banner

Multicore and Multithreading

Blog banner

Busted : Common Web Security Myths

Blog banner

Android Flashlight Application

Blog banner

Processing Crime and Incident Scenes

Blog banner

Virtual Memory

Blog banner

Unlocking the Secrets: Basic Operations of Computer Forensic Laboratories

Blog banner

Culture Of Ladakh

Blog banner

Riddhi Miyani 53003220140

Blog banner

undefined

Blog banner

INTERRUPTS

Blog banner

How to Choose Your Daily Signature Scent Based on Your Personal Vibe?

Blog banner

Semaphores

Blog banner

Processes : Process description and control

Blog banner

MORDERN UNIX SYSTEM

Blog banner

What is time ? The term which has astonished Scientists

Blog banner

Yahoo! mail

Blog banner

Severe landslides continue to cause concern in Joshimath, Uttarakhand

Blog banner

Exploring Human Factors in Cyber Forensics Investigations.

Blog banner

DATA VAULT

Blog banner

Who decides your overthinking, anyway?

Blog banner

On-Page SEO

Blog banner

Palm Vein Biometric Technology; Contactless vein authentication

Blog banner

Bots and Cyber Security

Blog banner