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

What is Spyware?

Blog banner

Zero Trust Security Model: Revolutionizing Cybersecurity in the Digital Age

Blog banner

CONCURRENCY

Blog banner

Yoga in INDIA and ABROAD

Blog banner

1.1 basic elements

Blog banner

Evolution of Operating system

Blog banner

Pandas Matrix Applications

Blog banner

Number Guessing game --lisp

Blog banner

Affiliate Marketing V/S Influencer Marketing

Blog banner

Starvation

Blog banner

Paging

Blog banner

Types of Threads

Blog banner

Current Trends in GIS and Remote Sensing(Ocean Applications)

Blog banner

social media issue

Blog banner

The New Dr. Frankenstein who will perform first full head transplant

Blog banner

DEVELOPMENTS LEADING TO MODERN OPERATING SYSTEMS

Blog banner

How can denial-of-service(Dos) attacks be prevented?

Blog banner

RSA (Rivest-Shamir-Adelman) Algorithm

Blog banner

Android Flashlight Application

Blog banner

To travel is to live

Blog banner

The IT Service Lifecycle

Blog banner

security controls

Blog banner

File System

Blog banner

Types of Big Data

Blog banner

Security requirements for Safe E-Payments

Blog banner

MEMORY HIERARCHY

Blog banner

What is a Dumpster Diving Attack?

Blog banner

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

Blog banner

Student Grade Calculator in LISP

Blog banner

MODERN OPERATING SYSTEM

Blog banner

Internet of Things

Blog banner

Crypto tax evasion

Blog banner

Data Security must be your Priority!

Blog banner

Cloud Forensic Tools And Storage :A Review Paper

Blog banner

Europe Through My Lens

Blog banner

Cache memory

Blog banner

Article on Fresh Book

Blog banner

The House ??of Patola Designs: Traditional Weaves with a Modern Twist

Blog banner

File Organization and Access

Blog banner

" THE ANSWERS THAT LED INDIAN BEAUTIES TO THE MISS UNIVERSE HISTORIC WIN. "

Blog banner

GIS REMOTE SENSING

Blog banner

SAVE TREES

Blog banner