0% found this document useful (0 votes)
61 views20 pages

Internet Networking: Socket Programming

The document discusses Internet networking and socket programming. It covers key concepts like sockets, TCP/IP, client-server programming, and implementing basic networking clients and servers in Java using sockets. Socket programming allows applications to communicate over a computer network via TCP or UDP. The client program creates a socket and connects to the server, which uses a ServerSocket to listen for connections and accept sockets from clients. Data can then be sent and received between the client and server using input/output streams attached to the sockets.

Uploaded by

Yusuf Mubarak
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)
61 views20 pages

Internet Networking: Socket Programming

The document discusses Internet networking and socket programming. It covers key concepts like sockets, TCP/IP, client-server programming, and implementing basic networking clients and servers in Java using sockets. Socket programming allows applications to communicate over a computer network via TCP or UDP. The client program creates a socket and connects to the server, which uses a ServerSocket to listen for connections and accept sockets from clients. Data can then be sent and received between the client and server using input/output streams attached to the sockets.

Uploaded by

Yusuf Mubarak
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/ 20

INTERNET NETWORKING

SOCKET PROGRAMMING
• Understand the concept of socket
• Learn how to send & receive data through sockets
• Implement network clients & servers
Recap of the last lecture(18/01/2023)
• INTERNET: the global network that links
millions of computers.
• Internet is not same as web
• A web is one of many internet services
– Email, FTP, Telnet, VoIP, Newsgroup and more
• Networking models: TCP/IP, OSI and more
Transmission Channel
• Physical characteristics of transmissions on
network differs widely
• Connection using cables – coaxial, twisted
pair, fibre optics
• Connection using DSL modem – send signals
across a telephone wire encoded as tones
• Connection using wireless – transmit a
modulated radio frequency
Internet Protocol (IP)
• Protocol is a set of rules that governs the
transmission of network information.
– application data, network data
• Example of protocols: Microsoft Networking,
Novell Netware, AppleTalk, and Ethernet
• IP was developed for LAN to communicate with
each other
• It has become the basis for connecting computers
around the world
• IP addresses are denoted by sequence of four
numbers e.g. 130.65.86.66
TCP/IP
• The IP has just one function—to attempt to
deliver data from one computer to another
across the Internet
• TCP/IP: a pair of communication protocols
used to establish reliable transmission of data
between two computers on the Internet
• Exceptions are “streaming media” services,
However, the most popular Internet services—
the World Wide Web and e-mail—use TCP.)
TCP/IP
• A TCP connection requires the Internet
addresses and port numbers of both end
points.
• TCP uses port numbers for this purpose. A port
number is an integer between 0 and 65,535
• Some applications use “well-known” port
numbers
• web servers use port 80, mail servers running
the Post Office Protocol (POP) use port 110.
TCP/IP
• TCP packets, therefore, must contain
– The Internet address and port number of the
recipient (destination)
– The Internet address and port number of the
sender (source)
Application level protocol
• Each Internet application has a different
application protocol, which describes how the
data for that particular application are
transmitted
• HTTP: the Hypertext Transfer Protocol which
is used for the World Wide Web
• Suppose you type a web address, called a
Uniform Resource Locator such as
http://horstmann.com/index.html
Application level protocol
• Post Office Protocol (POP), is used to
download received messages from e-mail
servers
• To send messages, use another protocol called
the Simple Mail Transfer Protocol (SMTP)
• Mention other application level protocol?
Socket and ServerSocket
• Socket and ServerSocket classes are used for
connection-oriented socket programming (TCP
connection)
• DatagramSocket and DatagramPacket classes
are used for connection-less socket
programming (UDP connection)
A Client program
• A socket is an object that encapsulates a TCP
connection
• To communicate with the other end point of
the connection, use the input and output
streams attached to the socket
• The client in socket programming must know
two information:
– IP Address of Server, and
– Port number
Socket class
• A socket is simply an endpoint for
communications between the machines. The
Socket class can be used to create a socket.
• Important methods
Method Description

1) public InputStream getInputStream() returns the InputStream attached with this


socket.

2) public OutputStream getOutputStream() returns the OutputStream attached with


this socket.

3) public synchronized void close() closes this socket


Creating a Client
• To create the client application, we need to
create the instance of Socket class
• Pass the IP address or hostname of the Server
and a port number.
• Here, we are using "localhost" because our server
is running on same system
– Socket s =
new Socket("localhost",6666);
• Let's see a simple of Java socket programming
where client sends a text and server receives and
prints it
Java code for Client
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
final int APP_PORT = 6666;
try {
Socket s = new Socket("localhost",APP_PORT);
DataOutputStream dout =
new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
} catch(Exception e){System.out.println(e);} //end of try
} // end of main
} //end of class
A Server program
• The ServerSocket class is used by server
applications to listen for client connections
ServerSocket class

• The ServerSocket class can be used to create a


server socket
• The object is used to set communication with
the clients
• Important methods
Method Description

1) public Socket accept() returns the socket and establish a connection


between server and client.

2) public synchronized void close() closes the server socket.


Creating a Server
• To create the server application, create the
instance of ServerSocket class
• we are using 6666 port number for the
communication between the client and server
• The accept() method waits for the client. If clients
connects with the given port number, it returns
an instance of Socket.
– ServerSocket ss = new ServerSocket(6666);
– //establishes connection and waits for the client
– Socket s = ss.accept();
Java code Server
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
final int APP_PORT = 6666
try{
ServerSocket ss = new ServerSocket(APP_PORT);
Socket s = ss.accept();//establishes connection
DataInputStream dis =
new DataInputStream(s.getInputStream());
String str = (String)dis.readUTF();
System.out.println("message= " + str);
ss.close();
}catch(Exception e){System.out.println(e);} //end try
} //end main
} //end class
How to run (manual)
• To execute this program open two command
prompts and execute each program at each
command prompt as displayed in the below
figure.
• After running the client application, a message
will be displayed on the server console.
Class work
• After successful execution, modify the code to
practice more

You might also like