Download Socket - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Dependency injection wikipedia , lookup

C Sharp syntax wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
 “A computer network is a set of two or more
computers connected together in order to share
information and other resources.”


The computer in a network is connected with
one another through cables, satellite or
telephone lines. Essential components of every
network system are *Hardware *Software
*People
It is also called information network.
Network topologies:
“Physical layout or arrangement of connected devices in a network.”
To send and receive any kind of data or message it is necessary to have IP address of the
sender and receiver.
 Socket Programming:
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.
After the connections are established, communication can occur using I/O streams. Each socket
has both an OutputStream and an InputStream. The client's OutputStream is connected to the
server's InputStream, and the client's InputStream is connected to the server's OutputStream.
Two types of TCP sockets
1. The java.net.Socket class represents a socket, It is used by a servers so that
they can accept connection.
and
2. java.net.ServerSocket class provides a mechanism for the server program
to listen for clients and establish connections with them.
Socket Class Methods For Client
ServerSocket Class Methods For
Server
 Public Socket(InetAddress host, int port)
throws IOException
 public ServerSocket(int port)
This method except that the host is denoted by an
InetAddress object.
throws IOException
 public Socket(InetAddress host, int port,
InetAddress localAddress, int localPort)
Attempts to create a server socket bound to the
specified port. An exception occurs if the port is already
bound by another application.
throws IOException
 public ServerSocket()
Except that the host is denoted by an InetAddress object
instead of a String
throws IOException
public Socket(String host, int port)
throws UnknownHostException, IOException.
This method attempts to connect to the specified server at
the specified port
Creates an unbound server socket.
 public ServerSocket(int port, int backlog)
throws IOException
The backlog parameter specifies how many incoming
clients to store in a wait queue.
import java.io.*;
import java.net.*;
Request from Client to Server
class ConnectDemo
{
Public static void main (String ars[])
{
try
{
Socket s = new Socket (“10.123.3.34” , 225);
DataInputStream inp = new DataInputStream(s.getInputStream () );
Boolean more_data = true;
System.out.println(“Established Connection”);
while (more_data)
{
String line = inp.readLine();
if (line==null)
more_data = false;
else
System.out.println(line);
}
}
catch(IOException e)
{
System.out.print(“IO error” + e)
}
}
}
import java.io.*;
import java.net.*;
Server Response to Client
class SimpleServer
{
public static void main (String args [ ] )
{
try {
ServerSocket sock = new ServerSocket(7500);
Socket newsock = sock.accept ( );
DataInputStream inp = new DataInputStream(newsock . getInputStream ( ) );
PrintStream outp = new PrintStream( ) ;
outp.Println(“hello : : enter QUIT to exit” );
Boolean more_data = true;
while(more_data)
{
String line = inp.readLine( )
if (line = = null);
more_data = false;
else
{
outp.println ( “ From Server : ” + line + “ln” ) ;
if (line. trim() . Equals ( “ QUIT ” ) )
more_data = false ;
}
}
newsock.Close( ) ;
}
Catch ( Exception e )
{
System.out.print ( “IO error “ + e )
}
}
}