You are on page 1of 10

Topic Presentation:

Client and Server Sockets


By: Zainab Hamoud AL-Rashdi
ID: 2016493008

1
Main points:
1- meaning of Socket .
2- Client and server socket.
3- Methods and class of client and server socket.
4- Programming example.

2
Meaning of socket:
Socket programming is a way of connecting two nodes on a
network to communicate with each other. One socket (node)
listens on a particular port at an IP, while other socket reaches
out to the other in order to form a connection.

3
Socket in Java:
A socket in Java is one endpoint of a two-way communication
link between two programs running on the network.

The socket have four operations performed:


1- connect to remote machine.
2- send data.
3- receive data.
4- close the connection.
4
Client socket:
Client socket: will first wait for the server to start. Once the
server is up and running, the client send a request to the
server. After that, the client will wait for the response from the
server. 
To initiate client request we need to follow this steps:
1- Establish connection or create a socket.
Example: Socket socket = new Socket(“127.0.0.1”, 5000);
2- Communication.
3- Closing the connection.

The java.net.socket class allow us to create socket objects.

The important methods that client socket is


used:
1- public InputStream getInputStream()
2- public OutputStream getOutputStream()
3- public synchronized void close()
Server socket:
Server socket: Basically, the server will instantiate its object
and wait for the client request. Once the client sends the
request, the server will communicate back with the response.
The steps to code the server-side application:
1- We need two socket:
- AServerSocket: ServerSocket ss=new ServerSocket(6666);  
- A plain old socket: Socket s=new Socket("localhost",6666);  
2- communicate with the client with the response.
3- getOutputStream()
4- Close the Connection

The java.net.serversocket class represent a server socket.

The important methods that server socket is used:


1- public Socket accept()
2- bind()
3- listen()
2- public synchronized void close() 6
Program example
Client side:
package clientpre; catch(UnknownHostException u)
import java.net.*; {
import java.io.*; System.out.println(u);
/** }
* catch(IOException i)
{
* @author zaina
System.out.println(i);
*/ }
public class clientpre String line = "";
{
// initialize socket and input while (!line.equals("Over"))
output streams {
private Socket socket = null; try
private DataInputStream input = {
null; line = input.readLine();
out.writeUTF(line);
private DataOutputStream out =
}
null; catch(IOException i)
// constructor to put ip address {
and port System.out.println(i);
public clientpre(String address, }
int port) }
{
try
try {
input.close();
{
out.close();
socket = new Socket(address, socket.close();
port); }
System.out.println("Connected") catch(IOException i)
; {
System.out.println(i);
input = new }
DataInputStream(System.in); }
public static void main(String args[]) {
clientpre client = new clientpre("127.0.0.1", 5000);
out = new
}
DataOutputStream(socket.getOu
tputStream());
} 7
Program example
Server side:

package serverpre;
import java.net.*;
import java.io.*; while (!line.equals("Over"))
/** {
* try
{
* @author zaina
line = in.readUTF();
*/ System.out.println(line);
public class serverpre
{ }
catch(IOException i)
private Socket socket = null; {
private ServerSocket server = null; System.out.println(i);
private DataInputStream in = null; }
// constructor with port }
public serverpre(int port) System.out.println("Closing connection");
{
socket.close();
in.close();
try{ }
server = new ServerSocket(port); catch(IOException i){
System.out.println("Server started"); System.out.println(i);
System.out.println("Waiting for a client ..."); }
socket = server.accept(); }
System.out.println("Client accepted"); public static void main(String args[]){
// takes input from the client socket serverpre server = new serverpre(5000);
in = new DataInputStream( }
new }
BufferedInputStream(socket.getInputStream()
)); 8
String line = "";
- First we run the server side, and this
the output:

- The output of the client side:

9
References:

Java Socket Programming (Java Networking Tutorial) - javatpoint.


(n.d.). Retrieved January 09, 2021, from
https://www.javatpoint.com/socket-programming

Ashutoshpawar07. (2015, February 19). Client Server Program In Java


Using Sockets. Retrieved January 09, 2021, from
https://www.youtube.com/watch?v=vCDrGJWqR8w&t=124s

Java Socket Programming Examples. (n.d.). Retrieved January 09,


2021, from https://cs.lmu.edu/~ray/notes/javanetexamples/

10

You might also like