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

A Survey of Anti-Forensic Techniques: Methods, Challenges, and Countermeasures

Blog banner

Ethical Issues in Data Science and Role of Data Science in Smart Cities

Blog banner

Multiprocessor and Multicore Organization

Blog banner

gis substation

Blog banner

Number Guessing game --lisp

Blog banner

Uniprocessor scheduling

Blog banner

Virtualization

Blog banner

Brain wash of social media

Blog banner

PODIO

Blog banner

What is metaverse?

Blog banner

Festive Ethnic Wear Guide: Patola-Inspired Looks for Every Celebration

Blog banner

Memory Management in Operating System

Blog banner

WHAT IS SNAPCHAT AND HOW DOES IT WORK?

Blog banner

Deadlock

Blog banner

Uniprocessor Scheduling

Blog banner

Direct Memory Access

Blog banner

objectives and function of operating system

Blog banner

Health and fitness in technology

Blog banner

Be you

Blog banner

LIFEHACKER

Blog banner

Operating System

Blog banner

Facebook Shut Down an AI Program!!! Facebook AI bots became Terminators???

Blog banner

Beautiful and stunning natural phenomena worth to see

Blog banner

Apache Kafka

Blog banner

Threads and concurrency

Blog banner

Supervised and Unsupervised Learning

Blog banner

MEMORY MANAGEMENT REQUIREMENT

Blog banner

"Geographic Information Systems (GIS) and its Applications in Urban Planning"

Blog banner

WAKE UP ITS FOOD o'CLOCK...!!!!!

Blog banner

Service transition principles

Blog banner

POVERTY:Causes and solutions to problems

Blog banner

Twisted world

Blog banner

Skills An Ethical Hacker Must Have

Blog banner

Operating Systems

Blog banner

Threads Concurrency: Mutual Exclusion and Synchronization

Blog banner

Kernel Memory Allocation In Linux.

Blog banner

Uniprocessor Scheduling

Blog banner

Kafka - A Framework

Blog banner

Mumbai famous street food

Blog banner

AN EVENT-BASED DIGITAL FORENSIC INVESTIGATION

Blog banner

Emerging threats in cyber Forensics

Blog banner

Deadlock and Starvation

Blog banner