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
e638d8d6
Commit
e638d8d6
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Application recovers if user enters invalid port number.
parent
a243a835
Branches
Branches containing commit
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
+22
-8
22 additions, 8 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
with
22 additions
and
8 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+
22
−
8
View file @
e638d8d6
...
...
@@ -4,6 +4,7 @@ import java.io.*;
import
java.net.InetAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.util.InputMismatchException
;
import
java.util.Scanner
;
public
class
Chat
{
...
...
@@ -64,8 +65,8 @@ public class Chat {
private
Socket
connectAsServer
()
throws
IOException
{
byte
[]
address
=
InetAddress
.
getLocalHost
().
getAddress
();
System
.
out
.
println
(
"Host address: "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
]);
S
ystem
.
out
.
print
(
"Select port number
: "
)
;
in
t
port
=
getPort
();
S
tring
prompt
=
"Select port number
"
;
shor
t
port
=
getPort
(
prompt
);
ServerSocket
serverSocket
=
new
ServerSocket
(
port
);
return
serverSocket
.
accept
();
}
...
...
@@ -78,15 +79,28 @@ public class Chat {
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
address
[
i
]
=
Byte
.
parseByte
(
tokens
[
i
]);
}
S
ystem
.
out
.
print
(
"Enter port host is opening at "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
]
+
": "
)
;
in
t
port
=
getPort
();
S
tring
prompt
=
"Enter port host is opening at "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
];
shor
t
port
=
getPort
(
prompt
);
return
new
Socket
(
InetAddress
.
getByAddress
(
address
),
port
);
}
private
int
getPort
()
{
int
port
=
scanner
.
nextInt
();
private
short
getPort
(
String
prompt
)
{
boolean
haveGoodNumber
=
false
;
short
port
=
0
;
while
(!
haveGoodNumber
)
{
System
.
out
.
print
(
prompt
+
": "
);
haveGoodNumber
=
true
;
try
{
port
=
scanner
.
nextShort
();
if
(
port
<
0
)
throw
new
InputMismatchException
(
"Expected non-negative value, got "
+
port
);
}
catch
(
InputMismatchException
ignored
)
{
System
.
out
.
println
(
"The port number must be a positive integer strictly less than 65536."
);
haveGoodNumber
=
false
;
}
finally
{
scanner
.
nextLine
();
}
}
return
port
;
}
...
...
This diff is collapsed.
Click to expand it.
Christopher Bohn
@bohn
mentioned in issue
#7 (closed)
·
5 years ago
mentioned in issue
#7 (closed)
mentioned in issue #7
Toggle commit list
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