Professional Documents
Culture Documents
Socket Programming
Socket Programming
A socket is one endpoint of a two-way communication link between two programs running on
the network. A socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent to.
An endpoint is a combination of an IP address and a port number. Every TCP connection can be
uniquely identified by its two endpoints. That way you can have multiple connections between
your host and the server.
Server:
a server runs on a specific computer and has a socket that is bound to a specific port
number. The server just waits, listening to the socket for a client to make a connection
request.
3. Accept the connection -> Open a new socket for the client
1. Open an input stream and output stream to the socket for the client.
2. Read from and write to the stream according to the server's protocol.
Client:
The client knows the hostname of the machine on which the server is running and the port
number on which the server is listening.
1. Open a socket.
3. Read from and write to the stream according to the server's protocol.
*************************************************************************************
java.net package
ServerSocket class: implements a socket that servers can use to listen for and accept
connections to clients
Creates a server socket and binds it to the specified local port number, with the
specified backlog
The maximum queue length for incoming connection indications (a request to connect)
is set to the backlog parameter
If a connection indication arrives when the queue is full, the connection is refused
Socket class: implements one side of a two-way connection between your Java program and
another program on the network
Creates a stream socket and connects it to the specified port number on the named host
*************************************************************************************
Client Side , opening socket
try {
Socket Client_Socket;
catch (Exception e) {
System.out.println(e);
}
When implementing a server you also need to create a socket object from the ServerSocket in
order to listen for and accept connections from clients.
try {
serviceSocket = MyService.accept();
catch (Exception e) {
System.out.println(e);
}
Server side, opening socket
try {
ServerSocket Server_Socket;
catch (Exception e) {
System.out.println(e);
}
*************************************************************************************
creating input stream on client side
try {
DataInputStream input;
catch (Exception e) {
System.out.println(e);
}
The class DataInputStream allows you to read lines of text and Java primitive data types in a
portable way.
*************************************************************************************
On the server side, we can use DataInputStream to receive input from
the client
try {
DataInputStream input;
catch (Exception e) {
System.out.println(e);
*************************************************************************
************
try {
PrintStream output;
catch (Exception e) {
System.out.println(e);
}
The class PrintStream has methods for displaying textual representation of Java primitive data
types.
or we can do:
try {
DataOutputStream output;
catch (Exception e) {
System.out.println(e);
The class DataOutputStream allows you to write Java primitive data types; many of its methods
write a single Java primitive type to the output stream.
On the server side, you can use the class PrintStream to send
information to the client.
PrintStream output;
try {
catch (IOException e) {
System.out.println(e);
*************************************************************************************
******
closing socket
always close the output and input stream before closing the socket
output.close();
input.close();
MyClient.close();
catch (Exception e) {
System.out.println(e);
output.close();
input.close();
serviceSocket.close();
MyService.close();
catch (Exception e) {
System.out.println(e); }