NETWORKING

 
 
 Server Stream Sockets 
 Client Stream Sockets 
 Data Gram Sockets 
 Tic Tac Toe 
  Create Board  

 java.net through which Java offers socket-based communications tat enable applications to view networking as streams of data and pocket-based communications that enable individual packets of information in audio and video to be transmitted over the Internet.

Client-server relationship:    The client requests that some action be performed, and the server performs the action and response to the client, for example World Wide Web browsers (client) and World Wide Web servers.

Java provides stream sockets and datagram sockets.  Stream sockets establish a process to connect to another process,  like the telephone system in which you dial and receive a connection to the telephone of the person you wish to communicate with, and is maintained for the duration of the call, even when you are not talking.  As long as the connection is in place, data flows between the processes in continuous streams.  They provide a connection-oriented service using TCP (Transmission Control Protocol).   This protocol is desirable for the vast majority of Java programmers.  In datagram sockets, individual packets of information are transmitted using UDP (User Datagram Protocol), which is a connectionless service without any guarantee that packets arrive in any particular order.  Packets may be lost, duplicated, or arrive out of sequence.  Datagram is not the right protocol for everyday users, but used in network applications that do nor require the error checking and reliability of TCP.

HyperText Transfer Protocol (HTTP) that forms the basis of the World Wide Web uses URLs (Uniform Resource Locators) which is a type of URI (Uniform Resource Identifier) to locate data on the internet.  Knowing the URI of publicly available HTML files anywhere on the World Wide Web, leads to access data through HTTP.

Establishing a Simple Server Using Stream Sockets:    This requires five steps.

1.    Create a Server-Socket Object. with a constructor ServerSocket server = new ServerSocket( port, queueLength); registers an available port number and specifies a maximum number of clients that can wait to connect to the server.  If the queue is full, the server refuses client connections.  This binds the server to a port.  Port numbers can be between 0 and 65,535.  Port numbers below 1024 are reserved for E-Mail and World Wide Web servers and require special access privileges.

2.    The server listens indefinitely for an attempt by the client to connect.  The program calls Server-Socket method accept.  Socket connection = server.accept(); which returns a Socket object when a connection with a client is established.

3.    OutputStream and InputStream Objects enable the server to communicate with the client by sending and receiving bytes.  The server sends information to the client via the OutputStream and receives information via the InputStream.

4.    Processing Phase when the server and client communicate via OutputStream and InputStream Objects.

5.    When the transmission is complete, the server closes the connection by invoking the close method on the Socket and on the corresponding streams.

Establishing a Simple Client Using Stream Sockets:    This requires four steps.

1.    Create a Socket to connect to the server with a constructor Socket connection = new ServerSocket(ServerAddress, port);   The two arguments are the Server's Internet Address and the port number.  If the connection attempt is successful, this statement returns a Socket.  A failure leads to IOException.  An UnknownHostException occurs when a server address indicated by a client cannot be resolved.

2.    The client uses Socket methods getInputStream and getOutputStreams to obtain references to the Socket's InputStream and OutputStream.

3.   In the processing phase, the client and server communicate via the OutputStream and InputStream Objects.

4.    The client closes the connection by invoking the close method on the Socket and on the corresponding streams.  When processing information sent by a server, the client must determine when the server is finished sending information so that the client can call close to close the Socket connection.

For more information on Networking:     Networking 

For more information of Stream Sockets      Stream Sockets

For more information on Datagram Sockets:     Datagram Sockets

Connectionless Client/Server Interaction with Datagrams is like the way mail is carried via the postal service.  If a large message will not fit in one envelope, break it into separate message pieces placed in separate, sequentially numbered envelopes.  Although mailed at the same time, they could arrive in order, out of order, or not at all (or you may receive some and not others).  The message pieces must be reassembled into sequential order to make sense.  In datagrrams, in addition to all the problems, duplicates may arrive on the receiving computer.

Client/Server Tic-Tac-Toe Using a Multithreaded server:    The server assigns Xs to the first client (who makes the first move) and Os to the second client.  The clients can place a mark only in an empty square on the board.

    board = new byte[ 9 ];
    players = new Player[ 2 ];
    currentPlayer = 0
    server = new ServerSocket( 5000, 2 );
     // wait for two connections so that the game can be played
    // wait for each client to connect
    for ( int i = 0; i < players.length; i++ ) {
        players[ i ] = new Player( server.accept(), i );
        players[ i ].start();
    }
    // Player X waits until Player O connects and then resumes play
    players[ O ]. setSuspended ( false );
    players.[ 0 ].notify();
     // if location not occupied, make move
    if ( !isOccupied( location) ) {
     // set move in board array
     board[ location ] = ( byte ) (  currentPlayer == 0 ? 'X' : 'O' );
     // change current player
    currentPlayer = ( currentPlayer + 1 ) % 2;
     // let new current player know that move has occurred
    players[ currentPlayer ].otherlayerMoved( location );
     // tell waiting player to continue
    notify();
    // determine whether location is occupied
    public boolean isOccupied( int location )
    {
        if ( board[ location ] == 'X' || board[ location ] == 'O' )
            return true;
        else
            return false;
    // specify player's mark
    mark = ( PlayerNumber == 0 ? 'X' : 'O' );
     // send message that other player moved
    public void otherPlayerMoved( int location )
    {
        output.write( "opponent moved" );
    // send client message indicating its mark (X or O)
    display( "Player " + ( playerNumber == 0 ? 'X' : 'O' + " connected" );
    // send message indicating connection
    output.write( "Player " + ( player == 0 ? "X connected\n" : "O connected, please wait\n" ) );
    // if player X,  wait for another player to arrive
    if ( mark == 'X' )
        output.write( "Waiting for another player" );

    // create board
    board = new Square[ 3 ] [ 3 ];
    for ( int row = 0; row < board.length; row++ ) {
        for ( int column = 0; column < board[ row ].length; column++ ) {
            // create square
            board[ row ] [ column ] = new square( '  ' , row * 3 + column );
    // connect to server
    connection = new Socket( InetAddress.getByName( "127.0.0.1" ), 5000 )
    get player's mark (X or O)
    myMark = input.readChar();
    idfield.setText( "You are Player \" " + myMark + "\" " );
    myTurn = (myMark == 'X' ? true : false );
    // set mark in square
    int row = location / 3
    int column = location % 3
    board[ row ] [ column ].setMark( myMark == 'X' ? 'O' : 'X' );
    displayArea.append( "Opponent moved. Your turn. \n" );

    // draw square
    g.drawRect( 0, 0, 29, 29 );
    g.drawString( String.valueOf( mark ), 11, 20 );