The Artima Developer Community
Sponsored Link

Java Answers Forum
Help Me Please!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Richard McClenaghan

Posts: 1
Nickname: rik
Registered: Oct, 2002

Help Me Please! Posted: Oct 15, 2002 4:34 AM
Reply to this message Reply
Advertisement
I have the following code and have to fill in the blanks can u help at all? i'm trying to connect a client to a server using TCP layout, any help at all would be GREATLY appreciated! Thanks!


import java.io.*;
import java.net.*;

public class P2_3_Client
{
static void process()
{ try
{ BufferedReader user_input = new BufferedReader(new InputStreamReader(System.in));

// create a socket
....

// get input & output streams
....
....

// read and print to screen a message from server
System.out.println(....);

boolean hit = false;
int row = 0, column = 0;
String from_keyboard;

/* when an array of type String is allocated
it's elements are automatically set to null
*/
String[][] grid = new String[4][4];
drawGrid(grid);

do
{ System.out.print("Enter row: ");
from_keyboard = user_input.readLine();
row = Integer.parseInt(from_keyboard);
System.out.print("Enter column: ");
from_keyboard = user_input.readLine();
column = Integer.parseInt(from_keyboard);

// send co-ordinates to server
....
....

// get indication of hit or miss from server
hit = ....
if (hit)
{ System.out.println("HIT!");
grid[row][column] = "h";
}
else
{ System.out.println("MISS");
grid[row][column] = "m";
}
drawGrid(grid);
} while(!hit);

// close input stream
....

// close output stream
....

// close socket
....
}
catch(SocketException e)
{ e.printStackTrace();
System.exit(1);
}
catch (UnknownHostException e)
{ System.out.println(e);
System.exit(1);
}

catch (IOException e)
{ System.out.println(e);
System.exit(1);
}
}

static void drawGrid(String[][] grid)
{ for (int r = 0; r < 4; r++)
{ System.out.println(" ----- ----- ----- ----- ");
System.out.println("| | | | |");
System.out.print("|");
for (int c = 0; c < 4; c++)
{ if (grid[r][c] == null)
System.out.print(" |");
else
if (grid[r][c].equals("m"))
System.out.print(" O |");
else
if (grid[r][c].equals("h"))
System.out.print(" X |");
}
System.out.println();
System.out.println("| | | | |");
}

System.out.println(" ----- ----- ----- -----");
System.out.println(); System.out.println();
}

public static void main(String args[])
{ P2_3_Client client = new P2_3_Client();
process();
}
}

------------------------------------------------------

import java.io.*;
import java.net.*;

public class P2_3_Server
{
public P2_3_Server()
{ try
{ ServerSocket ....
System.out.println("P2_3_Server is up and waiting for a connection...");
getClients(....);
}
catch (IOException e)
{ System.out.println(e);
System.exit(1);
}
}


static void getClients(ServerSocket server_socket)
{ try
{ // accept a connection request
....

// connection request received and processed, get input & output streams
....
....


// generate random choice for battleship position
int battleship_row = (int) (Math.random() * 4);
int battleship_column = (int) (Math.random() * 4);
boolean hit = false;
int user_row, user_column;

// send acknowledgement of request service to client
....("Connection to " + (InetAddress.getLocalHost()).getHostName() + " is successful");

while (!hit)
{ ....
....
if (.... == battleship_row && .... == battleship_column)
hit = true;
....(hit);
}

// close input stream
....

// close output stream
....

// close client socket
....
}
catch (IOException e)
{ System.out.println(e);
System.exit(1);
}

}

public static void main(String args[])
{ P2_3_Server server = new P2_3_Server();
}
}

Topic: how to import a jar file Previous Topic   Next Topic Topic: i want to compare a character of a string

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use