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

Every body is beautiful

Blog banner

Remote Work in 2026

Blog banner

Maharashtrian culture: Tradition, Art, Food

Blog banner

INTERNET SECURITY

Blog banner

Computer Forensics and its Impact in Business Environment

Blog banner

INTERNET

Blog banner

Digital Footprints An Emerging Dimension of Digital Inequality

Blog banner

Deadlock

Blog banner

Mail merge

Blog banner

Multiprocessor

Blog banner

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

Blog banner

The Rich Heritage Of Patola Sarees: Gujarat’s Timeless Weaving Art

Blog banner

Booting Process In Operating System

Blog banner

15 Interesting Facts about India

Blog banner

Confidence Building Activities for the New School Year

Blog banner

Amazon

Blog banner

Fitness regime by Deepesh

Blog banner

Review on Cyber Forensics and its Analysis Tools

Blog banner

What is a Malware ?

Blog banner

THE LEGAL ISSUES OF COMPUTER FORENSICS IN INDIA

Blog banner

What is a Dumpster Diving Attack?

Blog banner

MEMORY MANAGEMENT REQUIREMENT

Blog banner

Virus

Blog banner

OPERATING SYSTEM OBJECTIVES AND FUNCTIONS

Blog banner

Virtualisation

Blog banner

Zero-Day Attack

Blog banner

Uber

Blog banner

Social Engineering

Blog banner

Art and Culture of Rajasthan

Blog banner

New Ransomware Encrypts Your Android And Then Changes PIN Lock

Blog banner

Memory Management

Blog banner

Virtual Memory

Blog banner

What is Spyware? and examples of them.

Blog banner

Threads and concurrency

Blog banner

Protect yourself from System Hacking with these Simple Steps

Blog banner

Have You Explored India Yet?

Blog banner

Service design process in ITSM

Blog banner

IT security management

Blog banner

Blockchain in IoT Applications

Blog banner

This School Year, Let Their Belongings Come Back Home Too with Our Ultimate Labelling Kit

Blog banner

Review on Recovering Deleted Files

Blog banner

Southern Turkey Earthquake: Causes and Consequences of a Tragic Natural Disaster

Blog banner