GregJerneganBrandonS..

advertisement
Greg Jernegan
Brandon Simmons
The Beginning…

The problem
 Huge demand for internet enabled
applications and programs that
communicate over a network.

Basic Level
 Server-client-media for communication
○ Client makes request
○ Server offers services for request
What is Socket Programming

Sockets provide an interface for
programming networks at the transport
layer.
 Similar to file I/O
 Communication is independent of
programming language
○ This allows communication to exist among
any languages even if they’re different
 Example java talks to C++
How it works…
Server has one socket is bound to a
specific port
 Listens for connection request

 Upon request if all goes well connection is
made

After connection made the server gets a
new socket bound to a different port
 New port # needed so original socket can
listen for connection request as well as
serve the connected client
What that means…

Socket programming introduces a
multitasking environment
 More clients can be served at once with out
a bottle neck or delay in service.
Types of Socket Programming

SP can connected using both Connection Oriented
server an client program and Connectionless
 TCP beginning or UDP beginning

Programming may happen among many languages
 in Python
○ Example: tcpserver.py and tcpclient.py
 in Perl
○ Example: tcpserver.pl and tcpclient.pl
 In C
○ Example: tcpserver.c and tcpclient.c
 in Java
○ TCPServer.java and TCPClient.java
Simple Server in JAVA
// 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();
}
Simple Client in JAVA
// 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();
}
}
Commands












socket() creates a new socket of a certain socket type, identified by an integer
number, and allocates system resources to it.
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.
listen() is used on the server side, and causes a bound TCP socket to enter listening
state.
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.
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(), recv(), write(), read(), sendto() and recvfrom(), are used for
sending and receiving data to/from a remote socket.
close() causes the system to release resources allocated to a socket. In case of TCP,
the connection is terminated.
gethostbyname(), 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.
getsockopt() is used to retrieve the current value of a particular socket option for the
specified socket.
setsockopt() is used to set a particular socket option for the specified socket.
Download