Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jonathan Sudibya
IF3230-Tugas-Besar-Sister-2015
Commits
a48460aa
Commit
a48460aa
authored
Apr 23, 2015
by
Jonathan Sudibya
Browse files
testing tacker
parent
cf89d8f6
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/server/TCPServer/src/Controller/ConnectionHandler.java
View file @
a48460aa
...
...
@@ -2,11 +2,18 @@ package Controller;
import
Model.Client
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.net.InetAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.charset.Charset
;
import
java.util.Vector
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
org.json.simple.JSONObject
;
/*
* To change this license header, choose License Headers in Project Properties.
...
...
@@ -43,6 +50,9 @@ public class ConnectionHandler implements Runnable {
@Override
public
void
run
()
{
System
.
out
.
println
(
"Running thread "
+
threadName
);
System
.
out
.
println
(
"Connecting to tracker server..."
);
connectTracker
();
System
.
out
.
println
(
"Connection to tracker server successfull... Ready to recieve connection!"
);
while
(
active
){
try
{
clients
.
add
(
new
Client
(
globalID
,
welcomeSocket
.
accept
()));
...
...
@@ -66,4 +76,27 @@ public class ConnectionHandler implements Runnable {
{
active
=
_active
;
}
private
void
connectTracker
()
{
try
{
String
sentence
;
String
modifiedSentence
;
BufferedReader
inFromUser
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
Socket
newServer
=
new
Socket
(
"167.205.32.46"
,
8000
);
DataOutputStream
outToTracker
=
new
DataOutputStream
(
newServer
.
getOutputStream
());
BufferedReader
inFromTracker
=
new
BufferedReader
(
new
InputStreamReader
(
newServer
.
getInputStream
()));
JSONObject
join
=
new
JSONObject
();
join
.
put
(
"method"
,
"join"
);
join
.
put
(
"ip"
,
InetAddress
.
getLocalHost
().
getHostAddress
());
join
.
put
(
"port"
,
6789
);
sentence
=
join
.
toJSONString
();
outToTracker
.
write
(
sentence
.
getBytes
(
Charset
.
forName
(
"UTF-8"
)));
modifiedSentence
=
inFromTracker
.
readLine
();
System
.
out
.
println
(
"FROM TRACKER: "
+
modifiedSentence
);
newServer
.
close
();
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
ConnectionHandler
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
}
src/server/TCPServer/src/Model/Client.java
View file @
a48460aa
...
...
@@ -59,8 +59,12 @@ public class Client implements Runnable {
if
(
obj
!=
null
){
JSONObject
data
=
(
JSONObject
)
obj
;
}
capitalizedSentence
=
clientSentence
.
toUpperCase
()
+
'\n'
;
JSONObject
correct
=
new
JSONObject
();
correct
.
put
(
"status"
,
"ok"
);
capitalizedSentence
=
correct
.
toJSONString
();
outToClient
.
writeBytes
(
capitalizedSentence
);
connectionSocket
.
close
();
active
=
false
;
}
else
{
active
=
false
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment