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

Lecture powerpoints from the recommended textbook are by Lami Kaya, LKaya@ieee.org.

Lecture powerpoints are 2009 Pearson Education Inc.


Their content has sometimes been edited by Andy Brooks.

NET0183 Networks and Communications


Lecture 31
The Socket API
socket/tengill
8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

The recommended textbook is Computer Networks and Internets by Douglas E. Comer


http://www.coursesmart.com/0136066992/?a=1773944
www.pearson-books.com/student (for additional discounts and offers)

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

Socket @ Webopedia 31. mars 2010


http://www.webopedia.com/TERM/s/socket.html

(1) In UNIX and some other operating systems, a software object


that connects an application to a network protocol. In UNIX, for
example, a program can send and receive TCP/IP messages by
opening a socket and reading and writing data to and from the
socket. This simplifies program development because the
programmer need only worry about manipulating the socket and
can rely on the operating system to actually transport messages
across the network correctly. Note that a socket in this sense is
completely soft - it's a software object, not a physical component.

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

Socket: a door between an application process


and a transport protocol (e.g. TCP)

controlled by
application
developer
controlled by
operating
system

process

process

socket
TCP with
buffers,
variables

host or
server

from Kurose and Ross

internet

socket
TCP with
buffers,
variables

controlled by
application
developer
controlled by
operating
system

host or
server

3.13 Network Programming and the Socket API


The interface an application uses to specify communication
is known as an Application Program Interface (API).
One particular API has emerged as the de facto standard for
software that communicates over the Internet. This API is
known as the socket API, commonly abbreviated sockets.
The socket API is available for many OS such as Microsoft's
Windows systems as well as various UNIX systems,
including Linux.

sockets/tenglar
2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

3.15 Parameters and the Socket API


Socket programming differs from conventional I/O.
An application must specify details such as :
the address of a remote computer
the protocol port number
whether the application will act as a client or as a server

To avoid having a single socket function with many


parameters, designers of the socket API chose to
define many functions.
An application creates a socket, and then invokes
functions.
2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Figure 3.7 A summary of the major functions in the socket API.

2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

3.16 Socket Calls in a Client and Server


Figure 3.8 illustrates the sequence of socket calls made by
a typical client and server that use a stream connection.
The server waits for the client to send data.

In practice, some applications arrange for the server to send


first (i.e. send and recv are called in the reverse order).

2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Figure 3.8 Illustration of the sequence of socket functions called by a client and
server using the stream paradigm.

2009 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

How does this all work in Java?


A brief overview follows...

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

10

Simple, example client-server in Java.


AdditionClient.java RunAdditionClient.java
AdditionServer.java RunAdditionServer.java
The source code files are on NET0183 website.

The client sends two numbers to the server.


The server computes the addition and sends the result back to the client.
Window dialogues are used to report behaviour to let us see what is happening.

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

11

Socket and ServerSocket classes in Java


Socket knows about:
opening a connection
sending data
receiving data
closing a connection

ServerSocket knows about:


binding to a port
listening for incoming data
accepting connections

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

12

AdditionServer.java
import java.net.*;
import java.io.*;
import javax.swing.*;

public class AdditionServer extends JFrame


{
private JTextArea textWindow = new JTextArea();
private int port;
// the constructor
public AdditionServer(int portIn)
{
port = portIn;
setTitle("Addition Server");
add("Center",textWindow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
startServer();
}
8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

13

AdditionServer.java
private void startServer()
{
// declare a "general" socket and a server socket
Socket connection;
ServerSocket listenSocket;
// declare low level and high level objects for input
InputStream inStream;
DataInputStream inDataStream;
// declare low level and high level objects for output
OutputStream outStream;
DataOutputStream outDataStream;
// declare other variables
String client;
int first, second, sum;
boolean connected;

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

14

while(true)
AdditionServer.java
{
try {
// create a server socket
listenSocket = new ServerSocket(port);
textWindow.append("Listening on port " + port + "\n");
// listen for a connection from the client
connection = listenSocket.accept ();
connected = true;
// create an input stream from the client
inStream = connection.getInputStream();
inDataStream = new DataInputStream(inStream);
// create an output stream to the client
outStream = connection.getOutputStream ();
outDataStream = new DataOutputStream (outStream );
// wait for a string from the client
client = inDataStream.readUTF();
textWindow.append("Connection established with "
+ client + "\n" );
8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

15

AdditionServer.java
while(connected)
{
//read an integer from the client
first = inDataStream.readInt();
textWindow.append( "First number received: "
+ first + "\n");
//read an integer from the client
second = inDataStream.readInt();
textWindow.append( "Second number received: "
+ second + "\n");
sum = first + second;
textWindow.append( "Sum returned: "
+ sum + "\n");
//send the sum to the client
outDataStream.writeInt(sum);
}
}
catch (IOException e)
{
connected = false;
}}}}
8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

16

RunAdditionServer.java
public class RunAdditionServer {
/**
* @param args
*/
public static void main(String[] args) {
new AdditionServer(8901);
}
}

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

17

Simple, example client-server in Java.


Extracts from the Java API.

listenSocket = new ServerSocket(port);


Creates a server socket, bound to the specified port. A
port of 0 creates a socket on any free port. The maximum
queue length for incoming connection indications (a
request to connect) is set to 50. If a connection indication
arrives when the queue is full, the connection is refused.

connection = listenSocket.accept ();


Listens for a connection to be made to this socket and
accepts it. The method blocks until a connection is made.
A new Socket s is created and, if there is a security
manager, the security manager's checkAccept method is
called with s.getInetAddress().getHostAddress() and
s.getPort() as its arguments to ensure the operation is
allowed. This could result in a SecurityException.

8/25/2009

NET0183 Networks and Communications


by Dr Andy Brooks

18

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