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

Trends that began during the pandemic

Blog banner

Incident management in ITSM

Blog banner

Virtual Machine

Blog banner

File and File System Structure

Blog banner

Assignment 2

Blog banner

What is Minting & Mining

Blog banner

Sweet Mango Murabba

Blog banner

Data Security and Data Privacy in Data Science

Blog banner

What is metaverse?

Blog banner

A BLOG ON MYSQL

Blog banner

SMARTSHEET

Blog banner

Virtual memory

Blog banner

Dancing Classes In Mumbai

Blog banner

Types of Big Data

Blog banner

Memory Management in Operating System

Blog banner

Digital Marketing

Blog banner

Process State

Blog banner

Blog name

Blog banner

Policies for Service Transition

Blog banner

Importance of Website

Blog banner

Deadlock

Blog banner

Self managing devices

Blog banner

Why Festivals Are the Best Classrooms for Young Minds?

Blog banner

Binary Search Tree (BST) in Data Structure

Blog banner

Automating OSINT tasks for efficient Cyber Forensics Investigations

Blog banner

Practical Implementation of Client Server model using TCP/IP.

Blog banner

The Peephole

Blog banner

Digital marketing spotlight “Dove’s Real Beauty Campaign”

Blog banner

The Role of Teachers in Building a Child’s Confidence

Blog banner

Social Media Sentiment Analysis

Blog banner

BLOCKCHAIN MACHANISM

Blog banner

How to lose belly fat

Blog banner

INTERRUPTS

Blog banner

Objectives and functions of Operating System...

Blog banner

Disk cache

Blog banner

Regression Analysis

Blog banner

Cloud Computing: Threats and Vulnerabilities

Blog banner

COMPUTER FORENSICS AND GRAPHICS

Blog banner

The Rise of Evil Twin Attacks: A New Kind Of Spoofing Cyberattack

Blog banner

operating system

Blog banner

City of Mumbai (Bombay):

Blog banner

Theads

Blog banner