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

Client/Server Computing

The client-server model is a distributed communication framework of network


processes among service requestors, clients and service providers. The clientserver connection is established through a network or the Internet.
The client-server model is a core network computing concept also building
functionality for email exchange and Web/database access.
Applications are usually designed so that one computer (or end system) acts as a
server, providing a service to other computers on a network. To access a server, a
program is run on a users computer, this is called a client program. The program
establishes a connection through the network allowing communication with the
server.

Socket:
A socket is one endpoint of a two-way communication link between two programs
running on the network. A socket is bound to a port number so that the TCP layer
can identify the application that data is destined to be sent to. An endpoint is a
combination of an IP address and a port number. Every TCP connection can be
uniquely identified by its two endpoints. That way you can have multiple
connections between your host and the server.

The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8


Communication consists between a pair of sockets.

Socket Programming: This is most widely used concept in Networking and it


has been explained in very detail. Sockets provide the communication
mechanism between two computers using TCP. A client program creates a
socket on its end of the communication and attempts to connect that socket to
a server.

When the connection is made, the server creates a socket object on its end of
the communication. The client and server can now communicate by writing to
and reading from the socket.

The java.net package provides support for the two common network
protocols:

TCP: TCP stands for Transmission Control Protocol, which allows for reliable
communication between two applications. TCP is typically used over the Internet
Protocol, which is referred to as TCP/IP.

UDP: UDP stands for User Datagram Protocol, a connection-less protocol that
allows for packets of data to be transmitted between applications.

Server:
ServerSocket is a class for creating a server side connection for remote clients to connect
to, On client connection success, returns an ordinary socket which can later be used to
communicate with the client.
A socket is bound to a port number so that the TCP layer can identify the application that
data is destined to be sent to. An endpoint is a combination of an IP address and a port
number.

The java.net.ServerSocket class is used by server applications to obtain a port and listen for
client requests.
Class java.io.DataInputStream. A data input stream lets an application read
primitive Java data types from an underlying input stream in a machine-independent way. An
application uses a data output stream to write data that can later be read by a data input stream.
public Socket accept() throws IOException- Waits for an incoming client.

Server
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class server
{
public final static int port=13;
public static void main(String args[])
{
ServerSocket serversocket;
Socket socket;
try
{
serversocket=new ServerSocket(port);
socket=serversocket.accept();
while(true)
{
DataInputStream in=new DataInputStream(socket.getInputStream());
String str=in.readLine();
System.out.println("Data from Client is "+str);
PrintStream out=new PrintStream(socket.getOutputStream());
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
out.println(st);
}
}
catch(IOException ioe)
{
// serversocket.close();
System.out.println(ioe);
}
}
}

Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class cc
{
public static void main(String args[])
{
Socket socket;
String hostname;
try
{
socket=new Socket("localhost",13);
while(true)
{
PrintStream out=new PrintStream(socket.getOutputStream());
Scanner sc=new Scanner(System.in);
String st=sc.nextLine();
out.println(st);
DataInputStream in=new DataInputStream(socket.getInputStream());
String str=in.readLine();
System.out.println("data from server"+str);
}
}
catch(UnknownHostException u)
{
System.err.println(u);
}
catch(IOException ie)
{
System.err.println(ie);
}
}
}

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