0% found this document useful (0 votes)
8 views4 pages

Java Socket Programming (Client Server Program)

This document provides a tutorial on Java socket programming, specifically a simple client-server program using Socket and ServerSocket classes. It includes code examples for both the server and client, demonstrating how to establish a connection and send messages. Instructions for executing the programs are also provided, highlighting the use of localhost for local connections.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java Socket Programming (Client Server Program)

This document provides a tutorial on Java socket programming, specifically a simple client-server program using Socket and ServerSocket classes. It includes code examples for both the server and client, demonstrating how to establish a connection and send messages. Instructions for executing the programs are also provided, highlighting the use of localhost for local connections.

Uploaded by

Anima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

x

x
Java Socket Programming (Client Server
Program)
Categories

In this tutorial I have shared simple client server program example to explain java socket Algorithm
programming. Array
Articles
In this example I will use Socket and ServerSocket classes for connection oriented
Basic
socket programming. Since data is shared between server and client over network in the
Books
form of streams so DataInputStream and DataOutputStream classes are used. Collection
Data Structure
Subscribe To Get Free Java Programming Tutorials Directly In Your Inbox! Design Pattern ×
Java Socket Programming (Client Server Eclipse

Program) Enter your email address: Email


Error
Excel
Server Subscribe
Exception
Garbage Collection
import java.io.DataInputStream;
import java.net.ServerSocket; Interview Questions
import java.net.Socket; IO
class server { JDBC
public static void main(String args[]){ JSON
try{ JSP
//create socket, 5000 is port number
ServerSocket serverSocket = new ServerSocket(5000); Multithreading
MySQL
System.out.println("Waiting for Client...");
Networking
//establish connection OOP
Socket socket = serverSocket.accept();
Review
System.out.println("Client Connected..."); Servlet
//fetch incoming message Spring
DataInputStream dis = new DataInputStream(socket.getInputStream());
String message = (String)dis.readUTF(); String x
x
System.out.println("Client message: " + message);
//close connection Tools
serverSocket.close(); Uncategorized
}catch(Exception e){
e.printStackTrace(); Web Services
} XML
}
}

Above code is used to create server which is running on localhost on port number 5000.

Client
Subscribe To Get Free Java Programming Tutorials Directly In Your Inbox!
import java.io.DataOutputStream;
×
import java.net.Socket;
import java.util.Scanner;
Enter your email address:
class client {
public static void main(String args[]){
String message;
Scanner sc = new Scanner(System.in);
try{ Subscribe
//localhost because server is running on local machine, otherwise use ip o
Socket socket = new Socket("localhost", 5000);

System.out.println("Connected with Server...");


System.out.println("enter a message: ");
message = sc.nextLine();

//sending the message


DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF(message);
dout.flush();
dout.close();
//close connection
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
x
x

Since the server is running on local machine so I have used locahost while creating
connection. Otherwise use the IP address of server.

How to Execute?
1. First open a command prompt and run server program. The server will wait for client
to be connected.

Subscribe To Get Free Java Programming Tutorials Directly In Your Inbox! ×

Enter your email address:

Subscribe
2. Now open another command prompt and run client program. This will connect client x
x
with server. Enter a message at client side to send it to server.

See below screenshot as an example.

Subscribe To Get Free Java Programming Tutorials Directly In Your Inbox! ×

Enter
Comment below if you have any queries regarding above yourserver
client email program
address:in java.

You May Also Like:


Subscribe
Difference between JDK, JRE and JVM
4 Pillars of OOPs in Java
Java Global Variables
Java Random Number Generator
Java Program to Reverse a Number

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy