Вы находитесь на странице: 1из 25

Socket Programming

Guided By: Prepared By:


Prof. Kanchan Bhale Shreyas Patel
Aniruddh Fataniya
Vishal Polara
Outline of the Talk
• Basic Concepts
• Introduction to Socket Programming
• Advantages
• Disadvantages
• Socket Programming through TCP
Network Programming
• A network allows arbitrary applications to
communicate.
• Most network applications can be divided
into two pieces: a client and a server.
• A Web browser (a client) communicate
with a Web server.
• A Telnet client that we use to log in to a
remote host.
• A user who needs access to data located at
remote server.
Client-Server Paradigm
• Server waits for client to request a
connection.
• Client contacts server to establish a
connection.
• Client sends request.
• Server sends reply.
• Client and/or server terminate connection.
Two types of Communication
• Connection-oriented
– Setup the link before communication.
– Similar to the phone call. We need the phone
number and receiver.
• Connectionless
– No link needed to be set up before
communication.
– Similar to send a letter. We need the address
and receiver.
Sockets

• A socket is defined as an endpoint for


communication.

• Concatenation of IP address and port


– Connection-oriented: Phone number and receiver
– Connectionless: Address and receiver

• A socket pair (local IP address, local port, foreign


IP address, foreign port) uniquely identifies a
communication.
Socket Description
The socket 161.25.19.8:80 refers to port 80 on web
server 161.25.19.8
Sockets for Server and Client
Server
• Welcoming socket
Welcomes some initial contact from a client.
• Connection socket
Is created at initial contact of client.
New socket that is dedicated to the particular
client.
Client
• Client socket
 Initiate a TCP connection to the server by
creating a socket object. (Three-way handshake)
 Specify the address of the server process,
namely, the IP address of the server and the port
number of the process.
Socket
Types of Internet Sockets
• Stream Sockets • Datagram Sockets
(SOCK_STREAM) (SOCK_DGRAM)
 Connection oriented  Connectionless
 one-to-one  One-to-one or one-to many
 In-order delivery  Unordered delivery
 Transmission after  Transmission with
Connect destination address
 Rely on TCP to provide  Rely on UDP & Connection
reliable two- way is unreliable
connected communication
 Ex. Web, Email, Telnet  Ex. Multimedia, VOIP
Socket functional calls
• socket (): Create a socket
• bind(): bind a socket to a local IP address and port #
• listen(): passively waiting for connections
• connect(): initiating connection to another socket
• accept(): accept a new connection
• Write(): write data to a socket
• Read(): read data from a socket
• close(): close a socket (tear down the connection)
Socket Programming with TCP
Example - Programming Client

• Initialization:
– gethostbyname - look up server
– socket - create socket
– connect - connect to server port
• Transmission:
– send – send message to server
– recv - receive message from server
• Termination:
– close - close socket
Example - Programming Server

• Initialization:
– socket - create socket
– bind – bind socket to the local address
– listen - associate socket with incoming requests
• Loop:
– accept - accept incoming connection
– recv - receive message from client
– send - send message to client
• Termination:
– close - close connection socket
Advantages
• Sockets are flexible and sufficient.
• Efficient socket based programming can
be easily implemented for general
communications.
• Sockets cause low network traffic.
Disadvantages
• Socket based communications allows only
to send packets of raw data between
applications.
• Both the client-side and server-side have
to provide mechanisms to make the data
useful in any way.
• Since the data formats and protocols
remain application specific, the re-use of
socket based implementations is limited.
Steps for creating Server Program
1. Open the Server Socket:
ServerSocket server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept();
3. Create I/O streams for communicating to the client
DataInputStream is = new DataInputStream(client.getInputStream());
DataOutputStream os = new
DataOutputStream(client.getOutputStream());
4. Perform communication with client
Receive from client: String line = is.readLine();
Send to client: os.writeBytes(“Hello\n”);
5. Close socket:
client.close();
Steps for creating Client Program
1. Create a Socket Object:
Socket client = new Socket(server, port_id);
2. Create I/O streams for communicating with the server.
is = new DataInputStream(client.getInputStream());
os = new DataOutputStream(client.getOutputStream());
3. Perform I/O or communication with the server:
Receive data from the server: String line = is.readLine();
Send data to the server: os.writeBytes(“Hello\n”);
4. Close the socket when done:
client.close();
TCPServer
import java.io.*;
import java.net.*;

class TCPServer
{
public static void main(String argv[]) throws Exception
{
String fromclient;
String toclient;

ServerSocket Server = new ServerSocket (5000);

System.out.println ("TCPServer Waiting for client on port 5000");

while(true)
{
Socket connected = Server.accept();
System.out.println( " THE CLIENT"+" "+
connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));

BufferedReader inFromClient =
new BufferedReader(new InputStreamReader (connected.getInputStream()));

PrintWriter outToClient =
new PrintWriter(
connected.getOutputStream(),true);
while ( true )
{
System.out.println("SEND(Type Q or q to Quit):");
toclient = inFromUser.readLine();

if ( toclient.equals ("q") || toclient.equals("Q") )


{
outToClient.println(toclient);
connected.close();
break;
}
else
{
outToClient.println(toclient);
}

fromclient = inFromClient.readLine();

if ( fromclient.equals("q") || fromclient.equals("Q") )
{
connected.close();
break;
}
else
{
System.out.println( "RECIEVED:" + fromclient );
}

} } }}
TCPClient
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String FromServer;
String ToServer;

Socket clientSocket = new Socket("localhost", 5000);

BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));

PrintWriter outToServer = new PrintWriter(


clientSocket.getOutputStream(),true);

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(


clientSocket.getInputStream()));
while (true)
{
FromServer = inFromServer.readLine();
if ( FromServer.equals("q") || FromServer.equals("Q"))
{
clientSocket.close();
break;
}
else
{
System.out.println("RECIEVED:" + FromServer);
System.out.println("SEND(Type Q or q to Quit):");
ToServer = inFromUser.readLine();
if (ToServer.equals("Q") || ToServer.equals("q"))
{
outToServer.println (ToServer) ;
clientSocket.close();
break;
}
else
{
outToServer.println(ToServer);
}}} }}
Thank you

Вам также может понравиться