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

Elegant fashion style

Blog banner

CONCURRENCY

Blog banner

Understanding Toddler Tantrums: What They Really Mean

Blog banner

Drawing tips for a beginner

Blog banner

Memory Management

Blog banner

OS Assignment 1

Blog banner

I/O buffer and its techniques

Blog banner

Scheduling

Blog banner

Os assignment

Blog banner

Key to success in Sports

Blog banner

The Sunny Side of Instagram

Blog banner

Virtual memory in Operating System

Blog banner

15 Interesting Facts about India

Blog banner

Health and fitness

Blog banner

**THE MUJAWARR: Transforming the Logistics Industry**

Blog banner

Evolution of Operating system

Blog banner

Fault Tolerance in an Operating System

Blog banner

How to use open SSL for web server - browser communication

Blog banner

The Evolution of Operating Systems

Blog banner

File Management system

Blog banner

Why Skipping Afternoon Meals Affects More Than Just Your Hunger?

Blog banner

You Get Everyone, But No One Gets You: The Lonely Side of High Emotional Intelligence

Blog banner

Things You Should Leave Behind In 2025, Whilst In 2026

Blog banner

NodeJs

Blog banner

All you need to know about “Off-page SEO”

Blog banner

Memory management

Blog banner

10 Types of Friends in every friend group

Blog banner

Memory Management

Blog banner

How College Events Build Real-world Skills You Can’t Learn From Textbooks

Blog banner

E-BUSINESS RISK MANAGEMENT

Blog banner

Blog name

Blog banner

URL vs Domain

Blog banner

Memory Management

Blog banner

Why Extreme Opinions Are Rising: Psychological Insights into Society’s Divides

Blog banner

10 Reasons why Monica and Chandler are the best couple ever.!!!

Blog banner

Importance of internet

Blog banner

Concept and definition of m-commerce

Blog banner

What is service level Agreement?

Blog banner

Pipedrive

Blog banner

Indian Culture and Tradition

Blog banner

Process states

Blog banner

E-Governance

Blog banner