Socket Programming
Using Java
ICS-432
Muhammad Wasiq
Introduction to Sockets
A socket is one end-point of a two-way communication
link between two programs running on the network.
Client Server
Socket Socket
Port: 1432 Port: 25
IP: 192.168.230.1 IP: 192.168.230.2
SYN Server
Client
SYN/ACK
ACK
PUSH
ACK
Introduction to Sockets
Creating a Client Socket
import java.io.*;
import java.net.*;
public class CreateClientSocket {
public static void main(String[] args) throws IOException {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//create a client socket
socket = new Socket("localhost", 4444);
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
}
}
Introduction to Sockets
Creating a Server Socket
import java.io.*;
import java.net.*;
public class CreateServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=null;
Socket clientSocket = null;
String inputLine, outputLine;
try {
//creating a server socket
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
System.exit(-1);
}
try {
//accepting a clients request
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
Introduction to Sockets
Exercise # 1
Compile the CreateServerSocket and
CreateClientSocket classes and run them.
Analyze the traffic using ethereal when you run
them.
Introduction to Sockets
Exchanging Data Between Client & Server
Sending message to server from client and
receiving replies from the server
// Create a print writer to writing to the socket. In other words, to send data to
// server
out = new PrintWriter(socket.getOutputStream(), true);
// Create an input stream to read data from the socket. In other words, to
// receive data from server
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//sending message to the server
out.println(userInput);
//receiving message from the server and printing it
serverMessage = in.readLine();
System.out.println("echo:” + serverMessage);
Introduction to Sockets
Exchanging Data Between Client & Server
Receiving messages from client on server and
sending replies to the client
// Create a print writer to writing to the socket. In other words, to send data to
// client
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
// Create an input stream to read data from the socket. In other words, to
// receive data from client
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//reading from the client
inputLine = in.readLine();
//sending message to a client
outputLine = "Reply From Server :HelloClient ";
out.println(outputLine);
Introduction to Sockets
Closing Sockets
On Client Side:
// Closing the input stream
in.close();
// Closing the print writer
out.close();
// Closing the Socket
socket.close();
On Client Side:
// Closing the input stream
in.close();
// Closing the print writer
out.close();
// Closing the Server Socket
serverSocket.close();
//Closing the client socket
clientSocket.close();
Introduction to Sockets
A Complete Client and Server Pair
Download HelloServer.java and HelloClient.java from
http://www.ccse.kfupm.edu.sa/wasiq/ics432.
Investigate their code, compile them and run them.
Analyze the traffic being exchanged between them using
Ethereal.
Introduction to Sockets
A Persistent Server (A multi-threaded Server)
Usually servers are multithreaded
The main thread keeps listening for the incoming requests.
When a news request comes, it spawns a separate thread to
attend that request. This way, the main thread is always free
to receive new requests.
boolean listening=true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
System.exit(-1);
}
while (listening) {
new EchoServerThread(serverSocket.accept()).start();
}
Introduction to Sockets
A Persistent Server (A multi-threaded Server)
Download EchoClient.java, EchoServer.java and
EchoServerThread.java from
http://www.ccse.kfupm.edu.sa/wasiq/ics432.
Investigate their code, compile them and run them.
Analyze the traffic being exchanged between them using
Ethereal.
Introduction to Sockets
A Simple SMTP Client
You can use the following four basic SMTP commands to
write a simple SMTP client:
HELO smtp
MAIL FROM: <emailid@domainname>
RCPT TO: <emailid@domainname>
DATA
Following the data command, you type your e-mail text
End your e-mail content by issuing <CRLF>.<CRLF> command
In programs, you can replace CRLF by \n
QUIT
Try the above commands using telnet:
telnet soldier.ccse.kfupm.edu.sa 25
HELO SMTP
MAIL FROM: wasiq@ccse.kfupm.edu.sa
….
…
Introduction to Sockets
A Simple SMTP Client
Download SMTPClient.java from
http://www.ccse.kfupm.edu.sa/wasiq/ics432.
Investigate its code, compile it and run it. Analyze the
traffic using Ethereal.
Introduction to Sockets
A Simple POP Client
Write a simple pop client to connect to the ITC POP Server
“pop.kfupm.edu.sa” on port 110.
Following are the POP commands you will send to the server:
USER <username>
PASS <password>
LIST
RETR list-number
QUIT
First try connecting to pop.kfupm.edu.sa using telnet:
telnet pop.kfupm.edu.sa 110
USER wasiq
PASS *****
LIST
……
…..