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

Concurrency:Deadlock and Starvation

Blog banner

Simple Ways of Avoiding Basic Mistakes in Smart Phone Security

Blog banner

Linux Memory Management

Blog banner

An Approach To Spyware Detection And Removal

Blog banner

Operating System Objectives and Functions

Blog banner

Yahoo! mail

Blog banner

WHAT IS SNAPCHAT AND HOW DOES IT WORK?

Blog banner

SESSION HIJACKING

Blog banner

Virtual Machine

Blog banner

Modern operating system

Blog banner

Breaking the Cycle of Loneliness : Protecting your Mental Health Through meaningful Connection ( Part — 2)

Blog banner

STUDY OF SYSTEM HACKING TOOLS

Blog banner

Digital Marketing Ethics Transparency Trust And Brand Reputation digital

Blog banner

Discover The Top 3 Places To Stay in London

Blog banner

Trello ( management software)

Blog banner

Street foods

Blog banner

E-Cash (Electronic Cash)

Blog banner

Fossil Hunting 101 at the Peace River, Arcadia, Florida, USA

Blog banner

Instagram Features in 2023 That Will Leave You Stunned!

Blog banner

Busted : Common Web Security Myths

Blog banner

Threads Concurrency: Mutual Exclusion and Synchronization

Blog banner

Memory Management

Blog banner

Can ChatGPT Answer All My Questions About Life?

Blog banner

Sweet Mango Murabba

Blog banner

Power of words

Blog banner

Workplace mental health: A Psychological Perspective on Employee Well-being and Organizational Growth

Blog banner

5 People who claimed to have Time Traveled

Blog banner

Malicious softwares

Blog banner

How can parents support a child’s mental health?

Blog banner

Use case of K-means clustering

Blog banner

Introduction to Data Science: Life Cycle & Applications

Blog banner

Evolution of operating system

Blog banner

Teamwork

Blog banner

Different memory allocation strategies

Blog banner

Cyber-crime Investigation and Future Directions

Blog banner

Deadlock

Blog banner

Place to visit in pune

Blog banner

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

Blog banner

Consumer to consumer business mode

Blog banner

Optimization of operating system design

Blog banner

Starvation

Blog banner

SQL Injection

Blog banner