Академический Документы
Профессиональный Документы
Культура Документы
• 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;
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();
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;
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));