wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Socket Programming in Java

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

Process, process creation and process termination

Blog banner

How the mobile IP is used in GSM architecture.

Blog banner

Network Forensics Tools and Techniques

Blog banner

Operating System

Blog banner

Music is life

Blog banner

Multiprocessor and Multicore Organization

Blog banner

ART AND CULTURE OF VRINDAVAN

Blog banner

Man In The Middle Attack

Blog banner

Deadlock

Blog banner

AI & Data Science in Healthcare – Predicting diseases, medical imaging analysis

Blog banner

Memory management

Blog banner

The Everyday Parenting Dilemma: Safety Vs Independence

Blog banner

The Power of Forensic Watermarking in the Fight Against Content Piracy

Blog banner

About myself

Blog banner

Multithreading in Operating System

Blog banner

Social Media.

Blog banner

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

Blog banner

Festive Ethnic Wear Guide: Patola-Inspired Looks for Every Celebration

Blog banner

Artificial Intelligence and I

Blog banner

DIGITAL ECONOMY

Blog banner

Meal Maharaj — 3 CP, 5 CP, 8 CP. Same Love, Different Portions

Blog banner

The Role of Cyber Forensics in Addressing Cyber security Challenges in Smart Cities

Blog banner

5 Common Faults In Construction Tenders

Blog banner

Modern operating system

Blog banner

An Overview of Virtual Machines

Blog banner

R Programming

Blog banner

(Input/Output) in os

Blog banner

MySQL

Blog banner

Satellite Based Positioning

Blog banner

1 Dentist in Maroubra, Sydney and her 10 obsessions

Blog banner

Twisted world

Blog banner

Harsh Rathod

Blog banner

Processes: Process Description and Control.

Blog banner

Things You Should Leave Behind In 2025, Whilst In 2026

Blog banner

Sage business cloud accounting

Blog banner

What is M-commerce and how it's work

Blog banner

Windows Operating System

Blog banner

The khan mehtab transforming the modular switches company

Blog banner

SMARTSHEET MANAGEMENT SOFTWARE

Blog banner

Survey of Which Movies Types people are Prefer to Watch in India.

Blog banner

FREE VERSION OF G-MAIL

Blog banner

Hacking Mobile Platforms

Blog banner