Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
socket_chat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CSCE 361
starter code
socket_chat
Commits
13f94cf1
Commit
13f94cf1
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Replaced Scanner field with injected dependencies.
parent
26614b2a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+27
-23
27 additions, 23 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
with
27 additions
and
23 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+
27
−
23
View file @
13f94cf1
...
...
@@ -11,24 +11,23 @@ public class Chat {
private
static
final
int
MAXIMUM_CONNECTION_ATTEMPTS
=
10
;
private
Socket
socket
;
private
Scanner
scanner
;
private
boolean
isHost
;
public
Chat
()
{
scanner
=
new
Scanner
(
System
.
in
);
socket
=
connect
();
socket
=
connect
(
new
Scanner
(
System
.
in
));
}
// THESE METHODS SET UP CONNECTION
// THESE METHODS SET UP
AND TEAR DOWN
CONNECTION
private
Socket
connect
()
{
@SuppressWarnings
(
"WeakerAccess"
)
public
Socket
connect
(
Scanner
userInput
)
{
System
.
out
.
print
(
"Are you the chat host? [Y] "
);
String
answerString
=
scanner
.
nextLine
().
toUpperCase
();
String
answerString
=
userInput
.
nextLine
().
toUpperCase
();
char
answer
=
answerString
.
length
()
>
0
?
answerString
.
charAt
(
0
)
:
'Y'
;
isHost
=
(
answer
!=
'N'
);
Socket
socket
=
null
;
try
{
socket
=
isHost
?
connectAsServer
()
:
connectAsClient
();
socket
=
isHost
?
connectAsServer
(
userInput
)
:
connectAsClient
(
userInput
);
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Connection failed: "
+
ioException
);
System
.
exit
(
1
);
...
...
@@ -36,21 +35,31 @@ public class Chat {
return
socket
;
}
private
Socket
connectAsServer
()
throws
IOException
{
@SuppressWarnings
(
"WeakerAccess"
)
public
void
disconnect
()
{
try
{
socket
.
close
();
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Error while closing socket: "
+
ioException
);
// We're terminating anyway, note the error and continue normal termination
}
}
private
Socket
connectAsServer
(
Scanner
userInput
)
throws
IOException
{
byte
[]
address
=
InetAddress
.
getLocalHost
().
getAddress
();
System
.
out
.
println
(
"Host address: "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
]);
String
prompt
=
"Select port number"
;
int
port
=
getPort
(
prompt
);
int
port
=
getPort
(
prompt
,
userInput
);
ServerSocket
serverSocket
=
new
ServerSocket
(
port
);
System
.
out
.
println
(
"Waiting for client."
);
return
serverSocket
.
accept
();
}
private
Socket
connectAsClient
()
throws
IOException
{
byte
[]
address
=
getRemoteHostAddress
();
private
Socket
connectAsClient
(
Scanner
userInput
)
throws
IOException
{
byte
[]
address
=
getRemoteHostAddress
(
userInput
);
String
prompt
=
"Enter port host is opening at "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
];
int
port
=
getPort
(
prompt
);
int
port
=
getPort
(
prompt
,
userInput
);
Socket
socket
=
null
;
int
attemptCount
=
0
;
do
{
...
...
@@ -75,7 +84,7 @@ public class Chat {
return
socket
;
}
private
byte
[]
getRemoteHostAddress
()
{
private
byte
[]
getRemoteHostAddress
(
Scanner
userInput
)
{
// This assumes IPv4. Probably a good assumption.
boolean
haveGoodAddress
=
false
;
byte
[]
address
=
new
byte
[
4
];
...
...
@@ -83,7 +92,7 @@ public class Chat {
System
.
out
.
print
(
"Enter IP address of host <##.##.##.##>: "
);
haveGoodAddress
=
true
;
try
{
String
addressString
=
scanner
.
nextLine
();
String
addressString
=
userInput
.
nextLine
();
String
[]
tokens
=
addressString
.
split
(
"\\."
);
if
(
tokens
.
length
==
4
)
{
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
...
...
@@ -111,14 +120,14 @@ public class Chat {
return
address
;
}
private
int
getPort
(
String
prompt
)
{
private
int
getPort
(
String
prompt
,
Scanner
userInput
)
{
boolean
haveGoodNumber
=
false
;
int
port
=
0
;
while
(!
haveGoodNumber
)
{
System
.
out
.
print
(
prompt
+
": "
);
haveGoodNumber
=
true
;
try
{
port
=
scanner
.
nextInt
();
port
=
userInput
.
nextInt
();
if
(
port
<
0
)
throw
new
InputMismatchException
(
"Expected non-negative value, got "
+
port
);
if
(
port
>=
2
*
(
Short
.
MAX_VALUE
+
1
))
{
throw
new
InputMismatchException
(
"Expected value less than 65536, got "
+
port
);
...
...
@@ -127,7 +136,7 @@ public class Chat {
System
.
out
.
println
(
"The port number must be a positive integer strictly less than 65536."
);
haveGoodNumber
=
false
;
}
finally
{
scanner
.
nextLine
();
userInput
.
nextLine
();
}
}
return
port
;
...
...
@@ -148,12 +157,6 @@ public class Chat {
System
.
err
.
println
(
"Terminating."
);
// I'm pretty sure this is recoverable if the client is waiting on the server
System
.
exit
(
1
);
}
try
{
socket
.
close
();
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Error while closing socket: "
+
ioException
);
// We're terminating anyway, note the error and continue normal termination
}
}
@SuppressWarnings
(
"SameParameterValue"
)
...
...
@@ -196,5 +199,6 @@ public class Chat {
public
static
void
main
(
String
[]
args
)
{
Chat
chat
=
new
Chat
();
chat
.
communicate
();
chat
.
disconnect
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment