wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Socket Programming in Java

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

File and File System Structure

Blog banner

Predictive Analysis - Ek Overview

Blog banner

Functions of Operating System

Blog banner

Music helps reduce stress

Blog banner

Risk factors in service transistion

Blog banner

The New Classic: Indo Western Patola Outfits for Today’s Woman

Blog banner

Explain Kernel in OS

Blog banner

Security Breaches in Stock market trading

Blog banner

Odoo

Blog banner

What is 'Multi-core and Multi-threading' ?

Blog banner

Can ChatGPT Answer All My Questions About Life?

Blog banner

Kernel Memory Allocation In Linux.

Blog banner

My Favorite Sportsperson

Blog banner

How to tie a Tie

Blog banner

New Horizon Europe project ‘EvoLand’ sets off to develop new prototype services.

Blog banner

The application udemy

Blog banner

Real time scheduling

Blog banner

AI and Data Science: Revolutionizing Industries

Blog banner

What Makes Patola the Queen of Silk?

Blog banner

Worms, viruses and Bots

Blog banner

Firewall / IDS Evasion Techniques

Blog banner

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

Blog banner

Reclaim Your Bite and Beauty: All About Dental Restorative Treatments

Blog banner

How Social Media Algorithms Will Work in 2026?

Blog banner

Impacts of Data Breach On Companies Challenges and Prevention

Blog banner

Precision-Recall in Data Science

Blog banner

A Deep Dive

Blog banner

Dancing Classes In Mumbai

Blog banner

Esri India launches Policy Maps.

Blog banner

What is Spyware? and examples of them.

Blog banner

Uniprocessor Scheduling

Blog banner

RAID

Blog banner

Deadlock in Operating System

Blog banner

Solving Problems with AI: The Power of Search Algorithms

Blog banner

MYNTRA

Blog banner

Network Forensics Tools and Techniques

Blog banner

The New Classic: Indo Western Patola Outfits for Today’s Woman

Blog banner

Population

Blog banner

OS ASSIGNMENT

Blog banner

Operating system

Blog banner

Memory Management - operating system

Blog banner

Segmentation and paging concept

Blog banner