Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
190cdc73
Commit
190cdc73
authored
6 years ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
reformat, improve exception to silence warning
parent
ee34f295
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
projects/TcpExample3/Client/TcpClient/src/tcpclient/TcpClient.java
+39
-46
39 additions, 46 deletions
...TcpExample3/Client/TcpClient/src/tcpclient/TcpClient.java
with
39 additions
and
46 deletions
projects/TcpExample3/Client/TcpClient/src/tcpclient/TcpClient.java
+
39
−
46
View file @
190cdc73
package
tcpclient
;
package
tcpclient
;
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
/**
/**
* Before, we always used telnet to connect to the server.
* Before, we always used telnet to connect to the server. Here we are now
* Here we are now writing our own program to do the connection.
* writing our own program to do the connection.
*
*
* As you will see, when we run this after we start the server
* As you will see, when we run this after we start the server we will see the
* we will see the same string telnet printed, sent by the server.
* same string telnet printed, sent by the server. The output at the server will
* The output at the server will show different socket pairs for
* show different socket pairs for each time we ran it.
* each time we ran it.
*
*
* @author mcgredo
* @author mcgredo
*/
*/
public
class
TcpClient
{
public
class
TcpClient
{
public
final
static
String
LOCALHOST
=
"0:0:0:0:0:0:0:1"
;
// String constant, i.e. 127.0.0.1
public
final
static
String
LOCALHOST
=
"0:0:0:0:0:0:0:1"
;
// String constant, i.e. 127.0.0.1
public
static
void
main
(
String
[]
args
)
public
static
void
main
(
String
[]
args
)
{
{
try
{
try
while
(
true
)
{
{
System
.
out
.
println
(
"creating socket"
);
while
(
true
)
{
// We request an IP to connect to ("localhost") and
System
.
out
.
println
(
"creating socket"
);
// port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket
// We request an IP to connect to ("localhost") and
// object; the server uses a ServerSocket to wait for
// port number at that IP (2317). This establishes
// connections.
// a connection to that IP in the form of the Socket
Socket
socket
=
new
Socket
(
LOCALHOST
,
2317
);
// locohost?
// object; the server uses a ServerSocket to wait for
// connections.
// Read the single line written by the server. We'd
Socket
socket
=
new
Socket
(
LOCALHOST
,
2317
);
// locohost?
// do things a bit differently if many lines to be read
// from the server, instead of one only.
// Read the single line written by the server. We'd
InputStream
is
=
socket
.
getInputStream
();
// do things a bit differently if many lines to be read
InputStreamReader
isr
=
new
InputStreamReader
(
is
);
// from the server, instead of one only.
BufferedReader
br
=
new
BufferedReader
(
isr
);
InputStream
is
=
socket
.
getInputStream
();
InputStreamReader
isr
=
new
InputStreamReader
(
is
);
String
serverMessage
=
br
.
readLine
();
BufferedReader
br
=
new
BufferedReader
(
isr
);
System
.
out
.
println
(
"=================================================="
);
System
.
out
.
println
(
"Now we're talking!"
);
String
serverMessage
=
br
.
readLine
();
System
.
out
.
println
(
"The message the server sent was "
+
serverMessage
);
System
.
out
.
println
(
"=================================================="
);
// socket gets closed, either automatically/silently this code (or possibly by server)
System
.
out
.
println
(
"Now we're talking!"
);
}
// end while(true)
System
.
out
.
println
(
"The message the server sent was "
+
serverMessage
);
}
// socket gets closed, either automatically/silently this code (or possibly by server)
catch
(
IOException
e
)
{
}
// end while(true)
System
.
out
.
println
(
"Problem with client: "
);
// describe what is happening
}
System
.
out
.
println
(
e
);
catch
(
IOException
e
)
}
{
System
.
out
.
println
(
"Problem with client: "
);
// describe what is happening
System
.
out
.
println
(
e
);
}
// program exit: tell somebody about that
// program exit: tell somebody about that
System
.
out
.
println
(
"client exit"
);
System
.
out
.
println
(
"client exit"
);
}
}
}
}
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