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

Are Social Media Paid Campaigns Worth It?

Blog banner

Types of Threads

Blog banner

What is Virtual Memory

Blog banner

MODERN OPERATING SYSTEM

Blog banner

Short note on Microsoft office

Blog banner

Deadlocks in Operating Systems

Blog banner

Outlook.com

Blog banner

security requirements for safe e-payment

Blog banner

Deadlock and Starvation

Blog banner

GraphX:- A graph processing tool

Blog banner

BIRYANI ! The history you never knew about

Blog banner

A-B-C of Networking: Part-2 (Components)

Blog banner

What is semaphore in operating system?

Blog banner

Expressing and Measuring Risk (Risk Management)

Blog banner

Characteristics of Etherum

Blog banner

Retiring ISS

Blog banner

Os Virtual Memory

Blog banner

OS Assignment 1

Blog banner

Service transistion under difficult conditions

Blog banner

EID UL FITR

Blog banner

Exploring the Power of Encase Forensic Tools: Unraveling Digital Mysteries

Blog banner

Cyber Crime Investigation In The Era Of Big Data

Blog banner

Security Issues

Blog banner

Elements and Principles of Photography

Blog banner

VIRTUAL MACHINES

Blog banner

Raising Emotionally Intelligent Students: The Classroom Beyond Academics

Blog banner

Steganography and Steganalysis

Blog banner

Stop Racism

Blog banner

QUANTUM COMPUTING IN SECURITY:A GAME CHANGER IN DIGITAL WORLD

Blog banner

Dekkers Algorithm

Blog banner

Next time you click on "Remind me later", think twice!

Blog banner

Starvation

Blog banner

Rapido

Blog banner

Interesting fact about kangaroo.

Blog banner

Microsoft Windows Overview

Blog banner

Benefits of Yoga

Blog banner

Ubiquitous Computing

Blog banner

Different memory allocation strategies

Blog banner

Social Media Sentiment Analysis

Blog banner

Classification Algorithms (Decision trees, SVM, Logistic regreession)

Blog banner

Os assignment

Blog banner

From Websites To Super Apps For Digital User Experience

Blog banner