Download GregJerneganBrandonS..

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
no text concepts found
Transcript
Brandon Simmons
Greg Jernegan
Socket Programming
References:
http://www.troubleshooters.com/codecorn/sockets/
http://en.wikipedia.org/wiki/Berkeley_sockets/
http://java.sun.com/developer/technicalArticles/ALT/sockets/
http://www.buyya.com/java/Chapter13.pdf
Notes:
http://www.troubleshooters.com/codecorn/sockets/


socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it.


listen() is used on the server side, and causes a bound TCP socket to enter listening state.

accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client,
and creates a new socket associated with the socket address pair of this connection.





send() and recv(), or write() and read(), or sendto() and recvfrom(), are used for sending and receiving data to/from a remote socket.


getsockopt() is used to retrieve the current value of a particular socket option for the specified socket.
bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number
and IP address.
connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to
establish a new TCP connection.
close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.
gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only.
select() is used to prune a provided list of sockets for those that are ready to read, ready to write, or that have errors.
poll() is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read
from or if an error occurred.
setsockopt() is used to set a particular socket option for the specified socket.
http://en.wikipedia.org/wiki/Berkeley_sockets
Distributed object-based applications can be easily developed using Java Remote Method Invocation (RMI). The simplicity of RMI, however,
comes at the expense of network communication overhead. Low-level sockets can be used to develop client/server systems, but since most
Java I/O classes are not object friendly, how can you transport full-blown objects over sockets? Object serialization is the mechanism that
allows you to read/write full-blown objects to byte streams.
Combining low-level sockets and object serialization gives you a powerful, efficient alternative to RMI that enables you to transport objects
over sockets and overcome the overhead incurred in using RMI.
This article:






Gives you a brief overview of object serialization
Shows you how to work with object serialization
Illustrates how to work with existing objects and custom objects
Shows you how to transport objects over sockets
Provides examples of multi-threaded servers
Provides an object-based sample implementation of the daytime protocol
Finally, a brief comparison between RMI and sockets with object serialization is presented.
Overview of Object Serialization
Object serialization is a mechanism that is useful in any program that wants to save the state of objects to a file and later read those objects to
reconstruct the state of the program, or to send an object over the network using sockets. Serializing a class can be easily done simply by having
the class implement the java.io.Serializable interface. This interface is a marker interface. In other words, it does not have any methods that
need to be implemented by the class implementing it. It is mainly used to inform the Java virtual machine (JVM)1 that you want the object to be
serialized.
There are two main classes that are used for reading and writing objects to streams: ObjectOutputStream and ObjectInputStream. The
ObjectOutputStream provides the writeObject method for writing an object to an output stream, and the ObjectInputStream provides the
readObject method for reading the object from an input stream. It is important to note that the objects used with these methods must be
serialized. That is, their classes must implement the Serializable interface.
1.Open the Server Socket:
ServerSocket server = new ServerSocket( PORT );
2. Wait for the Client Request:
Socket client = server.accept(); Socket Programming 1254
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();
An example program illustrating creation of a server socket, waiting for client request, and then
responding to a client that requested for connection.
// SimpleServer.java: A simple server program.
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) throws IOException {
// Register service on port 1254
ServerSocket s = new ServerSocket(1254);
Socket s1=s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF(“Hi there”);
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
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();
An example program illustrating establishment of connection to a server and then reading a message
sent by the server and displaying it on the console i
// SimpleClient.java: A simple client program.
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1254
Socket s1 = new Socket(“localhost”,1254);
// Get an input file handle from the socket and read the input
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
http://www.buyya.com/java/Chapter13.pdf