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

Mental Health

Blog banner

Texting is actually better than talking in person

Blog banner

Demystifying Cryptography: A Beginner's Guide

Blog banner

In the world of Technology...

Blog banner

RAID

Blog banner

Threat management

Blog banner

10 Signs That Prove YOU are his FIRST priority.

Blog banner

Introduction to Virtual Memory - 080

Blog banner

Note on Process, Process creation and Process termination

Blog banner

Tools to support CSI activities

Blog banner

Memory Management

Blog banner

IT service level agreement

Blog banner

Basic Security For SOAP Services

Blog banner

Virtual machine.

Blog banner

Fitness

Blog banner

Disk Scheduling

Blog banner

MODERN OPERATING SYSTEM

Blog banner

How to Encrypt and Decrypt Using GNU PGP

Blog banner

Functions of Operating System

Blog banner

I/O Buffering

Blog banner

TRAIN TRAVELING

Blog banner

What is Vishing?

Blog banner

ProofHub

Blog banner

Vulnerabilities in OnePlus Devices

Blog banner

The Evolution of the Microprocessor ~ Aditya Pai

Blog banner

Importance Of Time

Blog banner

Direct Memory Access

Blog banner

The Five Steps of Data Science

Blog banner

Service transition principles

Blog banner

Studying Denial of service attack using DOSHTTP tool

Blog banner

Importance Of Yoga.

Blog banner

Install Ubuntu Easily

Blog banner

Open Source Project By Google

Blog banner

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

Blog banner

Continual service improvement vs maintenance phase in IT

Blog banner

Blockchain Transactions

Blog banner

Animal’s have my heart

Blog banner

CONCURRENCY

Blog banner

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

Blog banner

Direct Memory Access

Blog banner

15 Interesting Facts about India

Blog banner

Having passion in life

Blog banner