Task - Packages - Creating and Using Packages #97
akash-coded
started this conversation in
Tasks
Replies: 4 comments
-
Serverpackage mygame.server;
import mygame.utilities.*;
import mygame.client.*;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) throws IOException {
ServerSocket serverSocket = null;
Utilities.printMsg("creating server socket");
try {
serverSocket = new ServerSocket(4445);
} catch (IOException e) {
System.err.println("Unable to create server socket, " + e);
System.exit(1);
}
Utilities.printMsg("accepting client connections");
while (true) {
try {
Socket clientSocket = serverSocket.accept();
new Client(clientSocket);
} catch (IOException e) {
System.err.println("Unable to accept socket connection, " + e);
System.exit(1);
}
}
}
}Clientpackage mygame.client;
import mygame.utilities.*;
import mygame.server.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
Socket clientSocket = null;
public Client(Socket s) {
clientSocket = s;
}
public void run() {
if (clientSocket == null) {
return;
}
Utilities.printMsg("creating output stream");
try (Socket localSocket = clientSocket;
PrintStream out = new PrintStream(localSocket.getOutputStream())) {
Utilities.printMsg("writing current date");
Date d = new Date();
out.println(d);
} catch (IOException e) {
System.err.println("IOException occurred, " + e);
} finally {
clientSocket = null;
}
}
}
Utilitiespackage mygame.utilities;
public class Utilities {
// set DEBUG = false and compile to stop debug messages
final static boolean DEBUG = true;
public static void printMsg(String msg) {
if (DEBUG) {
System.out.println(msg);
}
}
}Directory and Output |
Beta Was this translation helpful? Give feedback.
0 replies
-
ClientServerUtilitiespackage myname.client;
import java.io.*;
import java.net.*;
import java.util.*;
import myname.utilities.Utilities;
public class Client extends Thread {
Socket clientSocket = null;
public Client(Socket s) {
clientSocket = s;
}
public void run() {
if (clientSocket == null) {
return;
}
Utilities.printMsg("creating output stream");
try (Socket localSocket = clientSocket;
PrintStream out = new PrintStream(localSocket.getOutputStream())) {
Utilities.printMsg("writing current date");
Date d = new Date();
out.println(d);
} catch (IOException e) {
System.err.println("IOException occurred, " + e);
} finally {
clientSocket = null;
}
}
}
# Server
package myname.server;
import java.io.*;
import java.net.*;
import myname.client.Client;
import myname.utilities.Utilities;
public class Server {
public static void main(String args[]) {
ServerSocket serverSocket = null;
Utilities.printMsg("creating server socket");
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Unable to create server socket, " + e);
System.exit(1);
}
Utilities.printMsg("accepting client connections");
while (true) {
try {
Socket clientSocket = serverSocket.accept();
new Client(clientSocket).start();
} catch (IOException e) {
System.err.println("Unable to accept socket connection, " + e);
System.exit(1);
}
}
}
}
# Utilities
package myname.utilities;
public class Utilities {
// set DEBUG = false and compile to stop debug messages
final static boolean DEBUG = true;
public static void printMsg(String msg) {
if (DEBUG) {
System.out.println(msg);
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
-
serverpackage mygame.server;
import mygame.client.*;
import mygame.utilities.*;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
ServerSocket serverSocket = null;
Utilities.printMsg("creating server socket");
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Unable to create server socket, " + e);
System.exit(1);
}
Utilities.printMsg("accepting client connections");
while (true) {
try {
Socket clientSocket = serverSocket.accept();
new Client(clientSocket).start();
} catch (IOException e) {
System.err.println("Unable to accept socket connection, " + e);
System.exit(1);
}
}
}
}clientpackage mygame.client;
import mygame.server.*;
import mygame.utilities.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Client extends Thread {
Socket clientSocket = null;
public Client(Socket s) {
clientSocket = s;
}
public void run() {
if (clientSocket == null) {
return;
}
Utilities.printMsg("creating output stream");
try (Socket localSocket = clientSocket;
PrintStream out = new PrintStream(localSocket.getOutputStream())) {
Utilities.printMsg("writing current date");
Date d = new Date();
out.println(d);
} catch (IOException e) {
System.err.println("IOException occurred, " + e);
} finally {
clientSocket = null;
}
}
}utilitiespackage mygame.utilities;
import mygame.client.*;
import mygame.server.*;
public class Utilities {
// set DEBUG = false and compile to stop debug messages
final static boolean DEBUG = true;
public static void printMsg(String msg) {
if (DEBUG) {
System.out.println(msg);
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.
-
Assume you have written some classes. Belatedly, you decide they should be split into three packages, as listed in the following table. Furthermore, assume the classes are currently in the default package (they have no
packagestatements).Questions
Exercises
Download the source files as listed here.
mygamedirectory you just created.)Solution
Check your answers.
Beta Was this translation helpful? Give feedback.
All reactions