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
96f699a0
Commit
96f699a0
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Gracefully handles case when port is already in use.
Closes
#8
parent
13f94cf1
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
+13
-4
13 additions, 4 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
with
13 additions
and
4 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+
13
−
4
View file @
96f699a0
...
...
@@ -48,9 +48,18 @@ public class Chat {
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
]);
int
port
;
ServerSocket
serverSocket
;
do
{
String
prompt
=
"Select port number"
;
int
port
=
getPort
(
prompt
,
userInput
);
ServerSocket
serverSocket
=
new
ServerSocket
(
port
);
port
=
getPort
(
prompt
,
userInput
);
try
{
serverSocket
=
new
ServerSocket
(
port
);
}
catch
(
BindException
ignored
)
{
System
.
out
.
println
(
"Port "
+
port
+
" is already in use."
);
serverSocket
=
null
;
}
}
while
(
serverSocket
==
null
);
System
.
out
.
println
(
"Waiting for client."
);
return
serverSocket
.
accept
();
}
...
...
@@ -128,7 +137,7 @@ public class Chat {
haveGoodNumber
=
true
;
try
{
port
=
userInput
.
nextInt
();
if
(
port
<
0
)
throw
new
InputMismatchException
(
"Expected
non-nega
tive value, got "
+
port
);
if
(
port
<
=
0
)
throw
new
InputMismatchException
(
"Expected
posi
tive value, got "
+
port
);
if
(
port
>=
2
*
(
Short
.
MAX_VALUE
+
1
))
{
throw
new
InputMismatchException
(
"Expected value less than 65536, got "
+
port
);
}
...
...
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