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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CSCE 361
starter code
socket_chat
Commits
33f767b8
Commit
33f767b8
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Continuing to replace strings with i18n properties.
Partially addresses
#6
parent
3ec24460
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+28
-17
28 additions, 17 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
src/main/resources/socketchat_en.properties
+16
-4
16 additions, 4 deletions
src/main/resources/socketchat_en.properties
with
44 additions
and
21 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+
28
−
17
View file @
33f767b8
...
...
@@ -2,6 +2,7 @@ package edu.unl.cse.csce361.socket_chat;
import
java.io.*
;
import
java.net.*
;
import
java.text.MessageFormat
;
import
java.util.InputMismatchException
;
import
java.util.Locale
;
import
java.util.ResourceBundle
;
...
...
@@ -25,10 +26,10 @@ public class Chat {
@SuppressWarnings
(
"WeakerAccess"
)
public
Socket
connect
(
Scanner
userInput
)
{
String
yes
=
bundle
.
getString
(
"connection.response
s
.yes"
);
String
no
=
bundle
.
getString
(
"connection.response
s
.no"
);
String
yes
=
bundle
.
getString
(
"connection.response.yes"
);
String
no
=
bundle
.
getString
(
"connection.response.no"
);
// "Are you the chat host? [Y] "
System
.
out
.
print
(
bundle
.
getString
(
"connection.prompt
s
.isHost"
)
+
" ["
+
yes
+
"] "
);
System
.
out
.
print
(
bundle
.
getString
(
"connection.prompt.isHost"
)
+
" ["
+
yes
+
"] "
);
String
answerString
=
userInput
.
nextLine
().
toUpperCase
();
String
answer
=
answerString
.
length
()
>
0
?
answerString
.
substring
(
0
,
no
.
length
())
:
yes
;
isHost
=
(!
answer
.
equals
(
no
));
...
...
@@ -36,8 +37,8 @@ public class Chat {
try
{
socket
=
isHost
?
connectAsServer
(
userInput
)
:
connectAsClient
(
userInput
);
}
catch
(
IOException
ioException
)
{
// "Connection failed"
System
.
err
.
println
(
bundle
.
getString
(
"connection.error
s
.generalConnectionFailure"
)
+
": "
+
ioException
);
// "Connection failed
: ...
"
System
.
err
.
println
(
bundle
.
getString
(
"connection.error.generalConnectionFailure"
)
+
": "
+
ioException
);
System
.
exit
(
1
);
}
return
socket
;
...
...
@@ -48,34 +49,39 @@ public class Chat {
try
{
socket
.
close
();
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Error while closing socket: "
+
ioException
);
// "Error while closing socket: ..."
System
.
err
.
println
(
bundle
.
getString
(
"connection.error.closingError"
)
+
": "
+
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
]);
// "Host address: ..."
System
.
out
.
println
(
MessageFormat
.
format
(
bundle
.
getString
(
"connection.info.hostAddress.0.1.2.3"
),
address
[
0
],
address
[
1
],
address
[
2
],
address
[
3
]));
int
port
;
ServerSocket
serverSocket
;
do
{
String
prompt
=
"Select port number"
;
port
=
getPort
(
prompt
,
userInput
);
//
"Select port number"
port
=
getPort
(
bundle
.
getString
(
"connection.prompt.selectPortNumber"
)
,
userInput
);
try
{
serverSocket
=
new
ServerSocket
(
port
);
}
catch
(
BindException
ignored
)
{
System
.
out
.
println
(
"Port "
+
port
+
" is already in use."
);
// "Port ... is already in use."
System
.
out
.
println
(
MessageFormat
.
format
(
bundle
.
getString
(
"connection.error.portInUse.0"
),
port
));
serverSocket
=
null
;
}
}
while
(
serverSocket
==
null
);
System
.
out
.
println
(
"Waiting for client."
);
System
.
out
.
println
(
bundle
.
getString
(
"connection.info.waiting"
)
);
return
serverSocket
.
accept
();
}
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
];
// "Enter port host is opening at ..."
String
prompt
=
MessageFormat
.
format
(
bundle
.
getString
(
"connection.prompt.getHostPort.0.1.2.3"
),
address
[
0
],
address
[
1
],
address
[
2
],
address
[
3
]);
int
port
=
getPort
(
prompt
,
userInput
);
Socket
socket
=
null
;
int
attemptCount
=
0
;
...
...
@@ -87,13 +93,18 @@ public class Chat {
try
{
socket
=
new
Socket
(
InetAddress
.
getByAddress
(
address
),
port
);
}
catch
(
ConnectException
ignored
)
{
System
.
out
.
println
(
"Attempt "
+
attemptCount
+
": Chat server is not yet ready at "
+
address
[
0
]
+
"."
+
address
[
1
]
+
"."
+
address
[
2
]
+
"."
+
address
[
3
]
+
":"
+
port
);
// "Attempt ...: Chat server is not yet ready at ..."
System
.
out
.
println
(
MessageFormat
.
format
(
bundle
.
getString
(
"connection.error.hostNotReady.attempt.1.2.3.4.port"
),
attemptCount
,
address
[
0
],
address
[
1
],
address
[
2
],
address
[
3
],
port
));
if
(
attemptCount
<
MAXIMUM_CONNECTION_ATTEMPTS
)
{
System
.
out
.
println
(
"Will attempt to connect again in "
+
attemptCount
+
" seconds"
);
// "Will attempt to connect again in ... seconds."
System
.
out
.
println
(
MessageFormat
.
format
(
bundle
.
getString
(
"connection.info.reAttempt"
),
attemptCount
));
socket
=
null
;
}
else
{
System
.
err
.
println
(
"Exceeded maximum number of connection attempts. Terminating."
);
// "Exceeded maximum number of connection attempts. Terminating."
System
.
err
.
println
(
bundle
.
getString
(
"connection.error.tooManyAttempts"
));
System
.
exit
(
1
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main/resources/socketchat_en.properties
+
16
−
4
View file @
33f767b8
connection.errors.generalConnectionFailure
=
Connection failed
connection.prompts.isHost
=
Are you the chat host?
connection.responses.yes
=
Y
connection.responses.no
=
N
\ No newline at end of file
connection.error.closingError
=
"Error while closing socket"
connection.error.generalConnectionFailure
=
Connection failed
connection.error.hostNotReady.attempt.1.2.3.4.port
=
Attempt {0}: Chat server is not yet ready at {1}.{2}.{3}.{4}:{5}
connection.error.portInUse.0
=
Port {0} is already in use.
connection.error.tooManyAttempts
=
Exceeded maximum number of connection attempts. Terminating.
connection.info.hostAddress.0.1.2.3
=
Host address: {0}.{1}.{2}.{3}
connection.info.reAttempt
=
Will attempt to connect again in {0} seconds.
connection.info.waiting
=
Waiting for client.
connection.prompt.getHostPort.0.1.2.3
=
Enter port host is opening at {0}.{1}.{2}.{3}
connection.prompt.isHost
=
Are you the chat host?
connection.prompt.selectPortNumber
=
Select port number
connection.response.yes
=
Y
connection.response.no
=
N
\ No newline at end of file
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