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

Types of E-Commerce

Blog banner

What does the Australian summer have in store for your oral health?

Blog banner

GraphQL

Blog banner

Virtual Machine's

Blog banner

The Power of Cyber Forensic in Solving Crimes

Blog banner

Deadlock in operating system

Blog banner

Concept and definition of m-commerce

Blog banner

Why Summer Break Is Important for Emotional and Cognitive Growth?

Blog banner

Network Forensics

Blog banner

INSTAGRAM

Blog banner

What are Virtual Machines

Blog banner

Importance Of Yoga.

Blog banner

Multithreading in Operating System

Blog banner

Technological Advancement

Blog banner

The Role of Teachers in Building a Child’s Confidence

Blog banner

Worms, viruses and Bots

Blog banner

Meshoo

Blog banner

File management

Blog banner

Top 5 Post-Wedding Skin Care Tips

Blog banner

RAID and It's Levels

Blog banner

Operating System Objectives and Functions

Blog banner

Deadlock and starvation in operating system

Blog banner

Evolution of Operating system.

Blog banner

Multiprocessor scheduling

Blog banner

How Reading Books Shape a Child’s Imagination and Thinking?

Blog banner

File management

Blog banner

Why Seasonal Summer Foods Are Best for Your Health?

Blog banner

Royal enfield

Blog banner

evolution of operating system - Disha Parekh

Blog banner

A book review

Blog banner

SMARTSHEET MANAGEMENT SYSTEM

Blog banner

A little bit of salt is all the hash needs!

Blog banner

Understanding Gen Z: A Generation Facing Crisis and Potential

Blog banner

Topic: Sessions in Operating system

Blog banner

Social Engineering Deceptions and Defenses

Blog banner

Tracking Emails & Email Crimes

Blog banner

MEMORY HIERARCHY

Blog banner

10 Types of Friends in every friend group

Blog banner

IOT Hacking Techniques

Blog banner

Computer Security

Blog banner

How Puppet Shows and Role Play Teach Empathy to Preschoolers

Blog banner

Geographic Information Systems(By aditi Unnikrishnan)

Blog banner