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
tubessister
IF3230-Tugas-Besar-Sister-2015
Commits
6265fa9f
Commit
6265fa9f
authored
Apr 17, 2015
by
William Stefan Hartono
Browse files
Server dan client sudah bisa berhubungan, client masih dummy menggunakan phyton
parent
fce71887
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/client/client.py
0 → 100644
View file @
6265fa9f
#Socket client example in python
import
socket
#for sockets
import
sys
#for exit
#create an INET, STREAMing socket
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
except
socket
.
error
:
print
(
'Failed to create socket'
)
sys
.
exit
()
print
(
'Socket Created'
)
host
=
'127.0.0.1'
;
port
=
5005
;
try
:
remote_ip
=
socket
.
gethostbyname
(
host
)
except
socket
.
gaierror
:
#could not resolve
print
(
'Hostname could not be resolved. Exiting'
)
sys
.
exit
()
#Connect to remote server
s
.
connect
((
remote_ip
,
port
))
print
(
'Socket Connected to '
+
host
+
' on ip '
+
remote_ip
)
#Send some data to remote server
user_input
=
input
(
'Masukkan angka 1-15 (0 untuk keluar):'
)
if
user_input
==
'1'
:
message
=
'{
\"
method
\"
:
\"
serverStatus
\"
,
\"
server
\"
: [{
\"
ip
\"
:
\"
167.205.32.46
\"
,
\"
port
\"
: 8000}]}'
elif
user_input
==
'2'
:
message
=
'{
\"
method
\"
:
\"
signup
\"
,
\"
username
\"
:
\"
williamstefan
\"
,
\"
password
\"
:
\"
1234
\"
}'
elif
user_input
==
'3'
:
message
=
'{
\"
method
\"
:
\"
login
\"
,
\"
username
\"
:
\"
williamstefan
\"
,
\"
password
\"
:
\"
1234
\"
}'
elif
user_input
==
'4'
:
message
=
'{
\"
method
\"
:
\"
inventory
\"
,
\"
token
\"
:
\"
1
\"
}'
elif
user_input
==
'5'
:
message
=
'{
\"\"
:
\"
mixitem
\"
,
\"
token
\"
:
\"
1
\"
,
\"
item1
\"
: 0,
\"
item2
\"
: 1}'
elif
user_input
==
'6'
:
message
=
'{
\"
method
\"
:
\"
map
\"
,
\"
token
\"
:
\"
1
\"
}'
elif
user_input
==
'7'
:
message
=
'{
\"
method
\"
:
\"
field
\"
,
\"
token
\"
:
\"
1
\"
}'
elif
user_input
==
'8'
:
message
=
'{
\"
method
\"
:
\"
offer
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offered_item
\"
: 0,
\"
n1
\"
: 1,
\"
demanded_item
\"
: 1,
\"
n2
\"
: 1}'
elif
user_input
==
'9'
:
message
=
'{
\"
met:hod
\"
:
\"
tradebox
\"
,
\"
token
\"
:
\"
1
\"
}'
elif
user_input
==
'10'
:
message
=
'{
\"
method
\"
:
\"
sendfind
\"
,
\"
token
\"
:
\"
1
\"
,
\"
item
\"
: 2}'
elif
user_input
==
'11'
:
message
=
'{
\"
method
\"
:
\"
findoffer
\"
,
\"
item
\"
: 2}'
elif
user_input
==
'12'
:
message
=
'{
\"
method
\"
:
\"
sendaccept
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
elif
user_input
==
'13'
:
message
=
'{
\"
method
\"
:
\"
accept
\"
,
\"
offer_token
\"
: 1}'
elif
user_input
==
'14'
:
message
=
'{
\"
method
\"
:
\"
fetch
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
elif
user_input
==
'15'
:
message
=
'{
\"
method
\"
:
\"
canceloffer
\"
,
\"
token
\"
:
\"
1
\"
,
\"
offer_token
\"
: 1}'
try
:
#Set the whole string
s
.
sendall
(
message
.
encode
(
'utf-8'
))
except
socket
.
error
:
#Send failed
print
(
'Send failed'
)
sys
.
exit
()
print
(
'Message send successfully'
)
print
(
message
)
#Now receive data
reply
=
s
.
recv
(
4096
)
print
(
reply
.
decode
(
'UTF-8'
))
\ No newline at end of file
src/server/server.py
0 → 100644
View file @
6265fa9f
## Handling Connections
import
json
import
socket
import
sys
import
_thread
from
_thread
import
*
HOST
=
'127.0.0.1'
# Symbolic name meaning all available interfaces
PORT
=
5005
# Arbitrary non-privileged port
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
print
(
'Socket created'
)
#Bind socket to local host and port
try
:
s
.
bind
((
HOST
,
PORT
))
except
(
socket
.
error
,
msg
):
print
(
'Bind failed. Error Code : '
+
str
(
msg
[
0
])
+
' Message '
+
msg
[
1
])
sys
.
exit
()
print
(
'Socket bind complete'
)
#Start listening on socket
s
.
listen
(
10
)
print
(
'Socket now listening'
)
#Function for handling connections. This will be used to create threads
def
clientthread
(
conn
):
#Sending message to connected client
conn
.
send
(
'Welcome to the server. Type something and hit enter
\n
'
.
encode
(
'utf-8'
))
#send only takes string
#infinite loop so that function do not terminate and thread do not end.
while
True
:
try
:
#Receiving from client
json_input
=
conn
.
recv
(
1024
)
reply
=
'OK...'
+
json_input
.
decode
(
'UTF-8'
)
if
not
json_input
:
break
else
:
print
(
'json_input'
,
json_input
.
decode
(
'UTF-8'
))
decoded
=
json
.
loads
(
json_input
.
decode
(
'UTF-8'
))
print
(
"JSON parsing example: "
,
decoded
[
'method'
])
conn
.
sendall
(
reply
.
encode
(
'UTF-8'
))
except
Exception
as
e
:
break
#came out of loop
conn
.
close
()
#now keep talking with the client
while
1
:
#wait to accept a connection - blocking call
conn
,
addr
=
s
.
accept
()
print
(
'Connected with '
+
addr
[
0
]
+
':'
+
str
(
addr
[
1
]))
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread
(
clientthread
,(
conn
,))
s
.
close
()
\ No newline at end of file
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