├── .gitignore ├── README.md ├── bab01 ├── branch.py ├── data-types.py ├── dictionary.py ├── function.py ├── list-for.py ├── list.py ├── loop.py └── tuple.py ├── bab02 ├── client.py ├── client1.py ├── read-with.py ├── read.py ├── server1.py ├── server2.py ├── write-with.py └── write.py ├── bab03 ├── client-select.py ├── client.py ├── server-select.py └── server.py ├── bab04 ├── client-queue-thread.py ├── client-thread.py ├── echoserver-thread.py ├── queue-1.py ├── queue-2.py ├── queue-thread.py ├── server-queue-thread.py ├── thread1.py ├── thread2.py ├── thread3.py ├── thread4.py └── thread5.py ├── bab05 ├── http-client-socket.py ├── http-htmlparser.py ├── http-simple-server.py ├── httplib-example.py ├── index.html ├── requests-bsoup.py └── socket-ssl.py ├── bab06 ├── ftp-delete.py ├── ftp-mkd.py ├── ftp-mlsd.py ├── ftp-pwd.py ├── ftp-rename.py ├── ftp-retr.py ├── ftp-retrbinary.py ├── ftp-rmd.py ├── ftp-sendcmd.py ├── ftp-stor.py ├── ftp-storbinary.py ├── ftp-welcome.py └── raw-list.py ├── bab07 ├── .DS_Store ├── client-udp1.py ├── client-udp2.py ├── client-udp3.py ├── server-udp.py └── server-udp2.py ├── bab08 ├── .DS_Store ├── serialization-client.py ├── serialization-server.py └── serialization-standalone.py ├── bab09 ├── .DS_Store ├── xmlrpc-boolean-client.py ├── xmlrpc-boolean-server.py ├── xmlrpc-datetime-client.py ├── xmlrpc-datetime-server.py ├── xmlrpc-multicall-client.py └── xmlrpc-multicall-server.py ├── bab10 └── smtp1.py ├── javaSocket ├── README.txt ├── echoClient │ ├── build.xml │ ├── build │ │ └── classes │ │ │ ├── .netbeans_automatic_build │ │ │ ├── .netbeans_update_resources │ │ │ └── echoclient │ │ │ └── EchoClient.class │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── private │ │ │ ├── private.properties │ │ │ └── private.xml │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── echoclient │ │ └── EchoClient.java ├── echoClientSerialization │ ├── build.xml │ ├── build │ │ └── classes │ │ │ ├── .netbeans_automatic_build │ │ │ ├── .netbeans_update_resources │ │ │ └── echoclientserialization │ │ │ └── EchoClientSerialization.class │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── private │ │ │ └── private.properties │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── echoclientserialization │ │ └── EchoClientSerialization.java ├── echoServer │ ├── build.xml │ ├── build │ │ └── classes │ │ │ ├── .netbeans_automatic_build │ │ │ ├── .netbeans_update_resources │ │ │ └── echoserver │ │ │ └── EchoServer.class │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── private │ │ │ ├── private.properties │ │ │ └── private.xml │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── echoserver │ │ └── EchoServer.java ├── echoServerMulticlient │ ├── build.xml │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── echoservermulticlient │ │ └── EchoServerMulticlient.java ├── echoServerSerialization │ ├── build.xml │ ├── build │ │ └── classes │ │ │ ├── .netbeans_automatic_build │ │ │ ├── .netbeans_update_resources │ │ │ └── echoserverserialization │ │ │ └── EchoServerSerialization.class │ ├── manifest.mf │ ├── nbproject │ │ ├── build-impl.xml │ │ ├── genfiles.properties │ │ ├── private │ │ │ └── private.properties │ │ ├── project.properties │ │ └── project.xml │ └── src │ │ └── echoserverserialization │ │ └── EchoServerSerialization.java └── messageSerialization │ ├── build.xml │ ├── build │ ├── built-jar.properties │ └── classes │ │ ├── .netbeans_automatic_build │ │ ├── .netbeans_update_resources │ │ └── messageserialization │ │ ├── Message.class │ │ └── MessageSerialization.class │ ├── dist │ ├── README.TXT │ └── messageSerialization.jar │ ├── manifest.mf │ ├── nbproject │ ├── build-impl.xml │ ├── genfiles.properties │ ├── private │ │ └── private.properties │ ├── project.properties │ └── project.xml │ └── src │ └── messageserialization │ ├── Message.java │ └── MessageSerialization.java └── pythonSocket ├── .project ├── .pydevproject ├── README.txt ├── echoClient.py ├── echoClientSerialization.py ├── echoServer.py ├── echoServerMulticlient.py └── echoServerSerialization.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *pyc 3 | venv 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # network-programming 2 | Source code collection of network programming using Python 3 | -------------------------------------------------------------------------------- /bab01/branch.py: -------------------------------------------------------------------------------- 1 | i = 5 2 | 3 | # check i value 4 | if i == 5: 5 | print("It is 5") 6 | else: 7 | print("It is not 5") 8 | 9 | # print 5 to 9 and say hello when it is 7 10 | for i in range(5, 10): 11 | if i == 7: 12 | print("Hello... it is 7") 13 | else: 14 | print(i) -------------------------------------------------------------------------------- /bab01/data-types.py: -------------------------------------------------------------------------------- 1 | # Integer 2 | x = 10 3 | print(type(x)) # Output: 4 | 5 | # Float 6 | y = 3.14 7 | print(type(y)) # Output: 8 | 9 | s = "Hello, Python!" 10 | print(type(s)) # Output: 11 | 12 | # String concatenation 13 | greeting = "Hello" + " " + "World" 14 | print(greeting) # Output: Hello World 15 | 16 | # Accessing characters 17 | print(s[0]) # Output: H 18 | print(s[-1]) # Output: ! 19 | 20 | # String slicing 21 | print(s[0:5]) # Output: Hello 22 | 23 | # String methods 24 | print(s.lower()) # Output: hello, python! 25 | print(s.upper()) # Output: HELLO, PYTHON! 26 | 27 | is_active = True 28 | is_logged_in = False 29 | 30 | print(type(is_active)) # Output: 31 | print(5 > 3) # Output: True 32 | print(5 < 3) # Output: False 33 | 34 | -------------------------------------------------------------------------------- /bab01/dictionary.py: -------------------------------------------------------------------------------- 1 | # declare dictionary 2 | banner = {} 3 | 4 | # fill dictionary 5 | banner['os'] = 'Ubuntu Server 13.04' 6 | banner['server'] = 'ProFTPd 1.3.4' 7 | banner['up'] = 315.5 8 | banner[200] = 'OK' 9 | 10 | print(banner) 11 | 12 | # iterate through dictionary 13 | for key, value in banner.items(): 14 | print(key, value) 15 | 16 | # delete item based on key 17 | del banner['up'] 18 | print(banner) -------------------------------------------------------------------------------- /bab01/function.py: -------------------------------------------------------------------------------- 1 | def greet(name): 2 | return f"Hello, {name}!" 3 | 4 | print(greet("Alice")) # Output: Hello, Alice! 5 | -------------------------------------------------------------------------------- /bab01/list-for.py: -------------------------------------------------------------------------------- 1 | # declare and fill list 2 | courses = ['j2ee', 'progjar', 'paal'] 3 | 4 | # iterate each element 5 | for course in courses: 6 | print(course) 7 | 8 | # get index and value with enumerate 9 | for index, value in enumerate(courses): 10 | print(index, value) 11 | 12 | # get element using index 13 | # python uses zero-based index 14 | print(courses[0]) -------------------------------------------------------------------------------- /bab01/list.py: -------------------------------------------------------------------------------- 1 | # declaration without elements 2 | courses = [] 3 | 4 | # declaration with elements 5 | languages = ['python', 'java', 'c'] 6 | print(languages) 7 | 8 | # fill the list 9 | courses.append('progjar') 10 | courses.append('j2ee') 11 | courses.append(2013) 12 | print(courses) 13 | 14 | # remove specific element by element 15 | courses.remove('j2ee') 16 | print(courses) 17 | 18 | # remove specific element by index 19 | del courses[0] 20 | print(courses) -------------------------------------------------------------------------------- /bab01/loop.py: -------------------------------------------------------------------------------- 1 | # pay attention to code indentation 2 | # python uses indentation, not curly brackets like in C 3 | 4 | # print 0 to 4 5 | for i in range(5): 6 | print(i) 7 | 8 | # print 0 to 4 too 9 | i = 0 10 | while i < 5: 11 | print(i) 12 | i = i + 1 -------------------------------------------------------------------------------- /bab01/tuple.py: -------------------------------------------------------------------------------- 1 | # declaration without brackets 2 | # elements are separated with comma 3 | address = 'localhost', 80, 21 4 | 5 | # declaration with brackets 6 | server_address = ('localhost', 5000) 7 | 8 | # access by index 9 | print(server_address[0]) 10 | 11 | # access by iteration 12 | for value in server_address: 13 | print(value) -------------------------------------------------------------------------------- /bab02/client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | class Client(object): 4 | def __init__(self) -> None: 5 | self.server_address = ('127.0.0.1', 5001) 6 | self.client_socket = None 7 | 8 | def connect(self): 9 | self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 10 | self.client_socket.connect(self.server_address) 11 | 12 | return self.client_socket 13 | 14 | def send(self) -> int: 15 | bytes_sent = self.client_socket.send(b'Hello network programming.') 16 | self.client_socket.close() 17 | 18 | return bytes_sent 19 | 20 | if __name__ == '__main__': 21 | sender = Client() 22 | sender.connect() 23 | sender.send() 24 | -------------------------------------------------------------------------------- /bab02/client1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | # create socket and connect to server 4 | server_address = ('localhost', 5001) 5 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | client_socket.connect(server_address) 7 | print(client_socket.getpeername()) 8 | print(client_socket.getsockname()) 9 | 10 | # send string to server and close socket 11 | client_socket.send(b'Hi ... ') 12 | client_socket.close() 13 | -------------------------------------------------------------------------------- /bab02/read-with.py: -------------------------------------------------------------------------------- 1 | # read file without close, with statement will close it for us :) 2 | with open('client1.py', 'r') as f: 3 | text = f.read() 4 | print(text) 5 | -------------------------------------------------------------------------------- /bab02/read.py: -------------------------------------------------------------------------------- 1 | # very simple way: open-read-close 2 | f = open('client1.py', 'r') 3 | data = f.read() 4 | print(data) 5 | f.close() 6 | -------------------------------------------------------------------------------- /bab02/server1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | # define server address, create socket, bind, and listen 4 | server_address = ('localhost', 5001) 5 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | server_socket.bind(server_address) 7 | server_socket.listen(1) 8 | 9 | # accept client, receive its message and print 10 | client_socket, client_address = server_socket.accept() 11 | data = client_socket.recv(1024) 12 | print(data) 13 | 14 | # close socket client and socket client 15 | client_socket.close() 16 | server_socket.close() 17 | -------------------------------------------------------------------------------- /bab02/server2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | 4 | # define server address, create socket, bind, and listen 5 | server_address = ('localhost', 5001) 6 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | server_socket.bind(server_address) 8 | server_socket.listen(5) 9 | 10 | # infinite loop accepting client 11 | try: 12 | while True: 13 | client_socket, client_address = server_socket.accept() 14 | print(client_socket) 15 | print(client_address) 16 | print(client_socket.getsockname()) 17 | print(client_socket.getpeername()) 18 | 19 | # receive data from client and print 20 | data = client_socket.recv(1024) 21 | print(data) 22 | 23 | # close socket client 24 | client_socket.close() 25 | 26 | # if user press ctrl + c, close socket client and exit 27 | except KeyboardInterrupt: 28 | server_socket.close() 29 | sys.exit(0) 30 | -------------------------------------------------------------------------------- /bab02/write-with.py: -------------------------------------------------------------------------------- 1 | # write to file using with, no need to close file explicitly 2 | data = 'A test to write to a file' 3 | with open('submission.html', 'w') as f: 4 | f.write(data) 5 | -------------------------------------------------------------------------------- /bab02/write.py: -------------------------------------------------------------------------------- 1 | data = 'Beautiful Python' 2 | 3 | f = open('file.txt', 'w') 4 | f.write(data) 5 | f.close() 6 | -------------------------------------------------------------------------------- /bab03/client-select.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | 4 | server_address = ('127.0.0.1', 5001) 5 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | client_socket.connect(server_address) 7 | 8 | sys.stdout.write('>> ') 9 | 10 | try: 11 | while True: 12 | message = sys.stdin.readline() 13 | client_socket.send(bytes(message, 'utf-8')) 14 | received_data = client_socket.recv(1024).decode('utf-8') 15 | sys.stdout.write(received_data) 16 | sys.stdout.write('>> ') 17 | 18 | except KeyboardInterrupt: 19 | client_socket.close() 20 | sys.exit(0) 21 | -------------------------------------------------------------------------------- /bab03/client.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | 4 | HOST, PORT = "localhost", 9999 5 | data = " ".join(sys.argv[1:]) 6 | 7 | # Create a socket (SOCK_STREAM means a TCP socket) 8 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: 9 | # Connect to server and send data 10 | sock.connect((HOST, PORT)) 11 | sock.sendall(bytes(data + "\n", "utf-8")) 12 | 13 | # Receive data from the server and shut down 14 | received = str(sock.recv(1024), "utf-8") 15 | 16 | print("Sent: {}".format(data)) 17 | print("Received: {}".format(received)) 18 | -------------------------------------------------------------------------------- /bab03/server-select.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import select 3 | import sys 4 | 5 | server_address = ('127.0.0.1', 5001) 6 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 8 | server_socket.bind(server_address) 9 | server_socket.listen(5) 10 | 11 | input_socket = [server_socket] 12 | 13 | try: 14 | while True: 15 | read_ready, write_ready, exception = select.select(input_socket, [], []) 16 | 17 | for sock in read_ready: 18 | if sock == server_socket: 19 | client_socket, client_address = server_socket.accept() 20 | input_socket.append(client_socket) 21 | 22 | else: 23 | data = sock.recv(1024) 24 | print(sock.getpeername(), data) 25 | 26 | if data: 27 | sock.send(data) 28 | else: 29 | sock.close() 30 | input_socket.remove(sock) 31 | 32 | except KeyboardInterrupt: 33 | server_socket.close() 34 | sys.exit(0) 35 | -------------------------------------------------------------------------------- /bab03/server.py: -------------------------------------------------------------------------------- 1 | import socketserver 2 | 3 | # https://docs.python.org/3/library/socketserver.html 4 | 5 | class MyTCPHandler(socketserver.BaseRequestHandler): 6 | """ 7 | The request handler class for our server. 8 | 9 | It is instantiated once per connection to the server, and must 10 | override the handle() method to implement communication to the 11 | client. 12 | """ 13 | 14 | def handle(self): 15 | # self.request is the TCP socket connected to the client 16 | self.data = self.request.recv(1024).strip() 17 | print("{} wrote:".format(self.client_address[0])) 18 | print(self.data) 19 | # just send back the same data, but upper-cased 20 | self.request.sendall(self.data.upper()) 21 | 22 | if __name__ == "__main__": 23 | HOST, PORT = "localhost", 9999 24 | 25 | # Create the server, binding to localhost on port 9999 26 | with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server: 27 | # Activate the server; this will keep running until you 28 | # interrupt the program with Ctrl-C 29 | server.serve_forever() 30 | -------------------------------------------------------------------------------- /bab04/client-queue-thread.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # client.py 3 | 4 | import sys 5 | import socket 6 | 7 | 8 | def main(elements): 9 | try: 10 | for e in elements: 11 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 | host = socket.gethostname() 13 | client.connect((host, 5001)) 14 | client.send(bytes(e, encoding='utf-8')) 15 | client.shutdown(socket.SHUT_RDWR) 16 | client.close() 17 | except Exception as msg: 18 | print(msg) 19 | 20 | 21 | if __name__ == "__main__": 22 | main(sys.argv[1:]) 23 | -------------------------------------------------------------------------------- /bab04/client-thread.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # client.py 3 | 4 | import sys 5 | import socket 6 | import time 7 | 8 | 9 | def main(elements): 10 | try: 11 | for e in elements: 12 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | 14 | client.connect(('127.0.0.1', 5000)) 15 | client.send(bytes(e, encoding='utf-8')) 16 | client.shutdown(socket.SHUT_RDWR) 17 | client.close() 18 | time.sleep(1) 19 | except Exception as msg: 20 | print(msg) 21 | 22 | 23 | if __name__ == "__main__": 24 | main(sys.argv[1:]) 25 | -------------------------------------------------------------------------------- /bab04/echoserver-thread.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # http://ilab.cs.byu.edu/python/threadingmodule.html 3 | 4 | import select 5 | import socket 6 | import sys 7 | import threading 8 | 9 | 10 | class Server: 11 | def __init__(self): 12 | # self.host = '10.211.55.2' 13 | self.host = '127.0.0.1' 14 | self.port = 5000 15 | self.backlog = 5 16 | self.size = 1024 17 | self.server = None 18 | self.threads = [] 19 | 20 | def open_socket(self): 21 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 22 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 23 | self.server.bind((self.host, self.port)) 24 | self.server.listen(5) 25 | 26 | def run(self): 27 | self.open_socket() 28 | input_list = [self.server, sys.stdin] 29 | running = 1 30 | while running: 31 | input_ready, _, _ = select.select(input_list, [], []) 32 | 33 | for s in input_ready: 34 | 35 | if s == self.server: 36 | # handle the server socket 37 | client_socket, client_address = self.server.accept() 38 | c = Client(client_socket, client_address) 39 | c.start() 40 | self.threads.append(c) 41 | 42 | elif s == sys.stdin: 43 | # handle standard input 44 | _ = sys.stdin.readline() 45 | running = 0 46 | 47 | # close all threads 48 | 49 | self.server.close() 50 | for c in self.threads: 51 | c.join() 52 | 53 | 54 | class Client(threading.Thread): 55 | def __init__(self, client, address): 56 | threading.Thread.__init__(self) 57 | self.client = client 58 | self.address = address 59 | self.size = 1024 60 | 61 | def run(self): 62 | running = 1 63 | while running: 64 | try: 65 | data = self.client.recv(self.size) 66 | print('received: ', self.address, data) 67 | if data: 68 | self.client.send(data) 69 | else: 70 | self.client.close() 71 | running = 0 72 | except ConnectionResetError: 73 | print('Connection reset') 74 | 75 | 76 | if __name__ == "__main__": 77 | server = Server() 78 | server.run() 79 | -------------------------------------------------------------------------------- /bab04/queue-1.py: -------------------------------------------------------------------------------- 1 | from queue import Queue 2 | 3 | q = Queue() 4 | 5 | for i in range(5): 6 | q.put(i) 7 | 8 | while not q.empty(): 9 | print(q.get()) 10 | -------------------------------------------------------------------------------- /bab04/queue-2.py: -------------------------------------------------------------------------------- 1 | from queue import LifoQueue 2 | 3 | q = LifoQueue() 4 | 5 | for i in range(5): 6 | q.put(i) 7 | 8 | while not q.empty(): 9 | print(q.get()) 10 | -------------------------------------------------------------------------------- /bab04/queue-thread.py: -------------------------------------------------------------------------------- 1 | import threading 2 | from queue import Queue 3 | import time 4 | 5 | myQueue = Queue() 6 | 7 | 8 | class CountStuff(threading.Thread): 9 | """ 10 | A thread class that will count a number, sleep and output that number 11 | """ 12 | 13 | def __init__(self, start_num, end, q): 14 | self.num = start_num 15 | self.end = end 16 | self.q = q 17 | threading.Thread.__init__(self) 18 | 19 | def run(self): 20 | while True: 21 | if self.num != self.end: 22 | self.num += 1 23 | self.q.put(self.num) 24 | time.sleep(1) 25 | else: 26 | break 27 | 28 | 29 | myThread = CountStuff(1, 5, myQueue) 30 | myThread.start() 31 | 32 | while True: 33 | if not myQueue.empty(): 34 | val = myQueue.get() 35 | print("Outputting: ", val) 36 | else: 37 | break 38 | time.sleep(1) 39 | -------------------------------------------------------------------------------- /bab04/server-queue-thread.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # server.py 3 | 4 | import socket 5 | import select 6 | import queue 7 | from threading import Thread 8 | from time import sleep 9 | from random import randint 10 | import sys 11 | 12 | 13 | class ProcessThread(Thread): 14 | def __init__(self): 15 | Thread.__init__(self) 16 | self.running = True 17 | self.q = queue.Queue() 18 | 19 | def add(self, data): 20 | self.q.put(data) 21 | 22 | def stop(self): 23 | self.running = False 24 | 25 | def run(self): 26 | q = self.q 27 | while self.running: 28 | try: 29 | # block for 1 second only: 30 | value = q.get(block=True, timeout=1) 31 | process(value) 32 | except queue.Empty: 33 | sys.stdout.write('.') 34 | sys.stdout.flush() 35 | 36 | if not q.empty(): 37 | print("Elements left in the queue:") 38 | while not q.empty(): 39 | print(q.get()) 40 | 41 | 42 | t = ProcessThread() 43 | t.start() 44 | 45 | 46 | def process(value): 47 | """ 48 | Implement this. Do something useful with the received data. 49 | """ 50 | print(value) 51 | sleep(randint(1, 5)) # emulating processing time 52 | 53 | 54 | def main(): 55 | s = socket.socket() # Create a socket object 56 | host = socket.gethostname() # Get local machine name 57 | port = 5001 # Reserve a port for your service. 58 | s.bind((host, port)) # Bind to the port 59 | print("Listening on port {p}...".format(p=port)) 60 | 61 | s.listen(5) # Now wait for client connection. 62 | while True: 63 | try: 64 | client, address = s.accept() 65 | ready = select.select([client, ], [], [], 2) 66 | if ready[0]: 67 | data = client.recv(4096) 68 | # print data 69 | t.add(data) 70 | except KeyboardInterrupt: 71 | print("Stop.") 72 | break 73 | except socket.error: 74 | print("Socket error!") 75 | break 76 | cleanup() 77 | 78 | 79 | def cleanup(): 80 | t.stop() 81 | t.join() 82 | 83 | 84 | if __name__ == "__main__": 85 | main() 86 | -------------------------------------------------------------------------------- /bab04/thread1.py: -------------------------------------------------------------------------------- 1 | # http://www.ibm.com/developerworks/aix/library/au-threadingpython/ 2 | 3 | import threading 4 | import datetime 5 | 6 | 7 | class ThreadClass(threading.Thread): 8 | def run(self): 9 | now = datetime.datetime.now() 10 | print("%s says Hello World at time: %s\n" % (self.getName(), now)) 11 | 12 | 13 | for i in range(5): 14 | t = ThreadClass() 15 | t.start() 16 | -------------------------------------------------------------------------------- /bab04/thread2.py: -------------------------------------------------------------------------------- 1 | # http://pymotw.com/2/threading/index.html 2 | import threading 3 | 4 | 5 | def worker(): 6 | print('Worker') 7 | 8 | 9 | threads = [] 10 | for i in range(5): 11 | t = threading.Thread(target=worker) 12 | threads.append(t) 13 | t.start() 14 | -------------------------------------------------------------------------------- /bab04/thread3.py: -------------------------------------------------------------------------------- 1 | # http://pymotw.com/2/threading/index.html 2 | import threading 3 | 4 | 5 | def worker(num): 6 | print('Worker: %s' % num) 7 | 8 | 9 | threads = [] 10 | for i in range(5): 11 | t = threading.Thread(target=worker, args=(i,)) 12 | threads.append(t) 13 | t.start() 14 | -------------------------------------------------------------------------------- /bab04/thread4.py: -------------------------------------------------------------------------------- 1 | # http://pymotw.com/2/threading/index.html 2 | import threading 3 | import logging 4 | 5 | logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s', ) 6 | 7 | 8 | class MyThread(threading.Thread): 9 | def run(self): 10 | logging.debug('running') 11 | 12 | 13 | for i in range(5): 14 | t = MyThread() 15 | t.start() 16 | -------------------------------------------------------------------------------- /bab04/thread5.py: -------------------------------------------------------------------------------- 1 | # http://pymotw.com/2/threading/ 2 | 3 | import threading 4 | import logging 5 | 6 | logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-10s) %(message)s', ) 7 | 8 | 9 | class MyThread(threading.Thread): 10 | def __init__(self, num): 11 | threading.Thread.__init__(self) 12 | self.num = num 13 | 14 | def run(self): 15 | logging.debug(str(self.num) + ' running') 16 | 17 | 18 | for i in range(5): 19 | t = MyThread(i) 20 | t.start() 21 | -------------------------------------------------------------------------------- /bab05/http-client-socket.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | def get_first_length(data): 4 | header = data.split('\r\n\r\n')[0] 5 | header_length = len(header) 6 | for line in header.split('\r\n'): 7 | if line.lower().startswith('content-length:'): 8 | parts = line.split(':') 9 | if len(parts) == 2: 10 | try: 11 | content_length = int(parts[1].strip()) 12 | except ValueError: 13 | return 0 14 | 15 | return header_length + int(content_length) 16 | 17 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 18 | # server_address = ('www.python.org', 80) 19 | server_address = ('localhost', 8080) 20 | client_socket.connect(server_address) 21 | 22 | # request_header = b'GET / HTTP/1.1\r\nHost: www.python.org\r\n\r\n' 23 | request_header = b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n' 24 | client_socket.send(request_header) 25 | 26 | response = '' 27 | while True: 28 | received = client_socket.recv(1024) 29 | decoded_received = received.decode('utf-8') 30 | first_length = get_first_length(decoded_received) 31 | response += decoded_received 32 | 33 | if not received or first_length < 1024: 34 | break 35 | 36 | 37 | print(response) 38 | client_socket.close() 39 | -------------------------------------------------------------------------------- /bab05/http-htmlparser.py: -------------------------------------------------------------------------------- 1 | from html.parser import HTMLParser 2 | from urllib.request import urlopen 3 | import gzip 4 | from io import BytesIO 5 | 6 | class MyHTMLParser(HTMLParser): 7 | def __init__(self): 8 | super().__init__() 9 | self.in_h2_tag = False 10 | 11 | def handle_starttag(self, tag, attrs): 12 | if tag == 'h2': 13 | self.in_h2_tag = True 14 | 15 | def handle_endtag(self, tag): 16 | if tag == 'h2': 17 | self.in_h2_tag = False 18 | 19 | def handle_data(self, data): 20 | if self.in_h2_tag: 21 | print("Section Title:", data) 22 | 23 | def get_url_content(url): 24 | response = urlopen(url) 25 | if response.info().get('Content-Encoding') == 'gzip': 26 | buf = BytesIO(response.read()) 27 | f = gzip.GzipFile(fileobj=buf) 28 | content = f.read() 29 | else: 30 | content = response.read() 31 | return content 32 | 33 | parser = MyHTMLParser() 34 | url = 'http://www.python.org' 35 | content = get_url_content(url) 36 | parser.feed(content.decode('utf-8')) 37 | -------------------------------------------------------------------------------- /bab05/http-simple-server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import select 3 | import sys 4 | 5 | server_address = ('localhost', 8080) 6 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 8 | server_socket.bind(server_address) 9 | server_socket.listen(5) 10 | 11 | input_socket = [server_socket] 12 | 13 | try: 14 | while True: 15 | read_ready, write_ready, exception = select.select(input_socket, [], []) 16 | 17 | for sock in read_ready: 18 | if sock == server_socket: 19 | client_socket, client_address = server_socket.accept() 20 | input_socket.append(client_socket) 21 | 22 | else: 23 | # receive data from client, break when null received 24 | data = sock.recv(4096) 25 | print('data:', data) 26 | 27 | if data: 28 | data = data.decode('utf-8') 29 | request_header = data.split('\r\n') 30 | print('request header:', request_header) 31 | request_file = request_header[0].split()[1] 32 | response_header = b'' 33 | response_data = b'' 34 | 35 | if request_file == 'index.html' or request_file == '/' or request_file == '/index.html': 36 | f = open('index.html', 'r') 37 | response_data = f.read() 38 | f.close() 39 | 40 | content_length = len(response_data) 41 | response_header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ' \ 42 | + str(content_length) + '\r\n\r\n' 43 | 44 | sock.sendall(response_header.encode('utf-8') + response_data.encode('utf-8')) 45 | 46 | else: 47 | sock.sendall(b'HTTP/1.1 404 Not found\r\n\r\n') 48 | else: 49 | input_socket.remove(sock) 50 | sock.close() 51 | 52 | except KeyboardInterrupt: 53 | server_socket.close() 54 | sys.exit(0) 55 | -------------------------------------------------------------------------------- /bab05/httplib-example.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import http.client 4 | 5 | connection = http.client.HTTPSConnection("www.its.ac.id") 6 | connection.request("GET", "/") 7 | response = connection.getresponse() 8 | header = response.getheaders() 9 | 10 | print("Status:", response.status, response.reason) 11 | print("Response header:") 12 | for item in header: 13 | print(item) 14 | 15 | # print("Content:", response.read()) 16 | -------------------------------------------------------------------------------- /bab05/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | This is a title 6 | 7 | 8 | Hello world! 9 | 10 | -------------------------------------------------------------------------------- /bab05/requests-bsoup.py: -------------------------------------------------------------------------------- 1 | import requests 2 | from bs4 import BeautifulSoup 3 | 4 | def fetch_and_parse_url(url): 5 | # Fetch the content of the URL 6 | response = requests.get(url) 7 | # Parse the content with BeautifulSoup using the HTML parser 8 | soup = BeautifulSoup(response.text, 'html.parser') 9 | return soup 10 | 11 | def print_links(soup): 12 | # Find all 'a' tags, which define hyperlinks 13 | links = soup.find_all('a') 14 | for link in links: 15 | href = link.get('href') # Get the href attribute of the link 16 | text = link.text.strip() # Get the text enclosed in the tag 17 | print(f"URL: {href}, Text: {text}") 18 | 19 | # URL to be parsed 20 | url = 'http://www.python.org' 21 | # Fetch and parse the URL 22 | soup = fetch_and_parse_url(url) 23 | # Print all links found in the HTML 24 | print_links(soup) 25 | -------------------------------------------------------------------------------- /bab05/socket-ssl.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import ssl 3 | import gzip 4 | from io import BytesIO 5 | from bs4 import BeautifulSoup 6 | 7 | # Define hostname and SSL context 8 | hostname = 'www.python.org' 9 | context = ssl.create_default_context() 10 | 11 | # Create an SSL socket connection 12 | with socket.create_connection((hostname, 443)) as sock: 13 | with context.wrap_socket(sock, server_hostname=hostname) as ssock: 14 | # Prepare request header with HTTP/1.1 and necessary headers 15 | request_header = f"GET / HTTP/1.1\r\nHost: {hostname}\r\nAccept-Encoding: gzip\r\nConnection: close\r\n\r\n" 16 | ssock.send(request_header.encode()) 17 | 18 | # Initialize buffer for response 19 | response = bytearray() 20 | try: 21 | while True: 22 | chunk = ssock.recv(1024) 23 | if not chunk: 24 | break 25 | response += chunk 26 | except socket.timeout: 27 | print("Connection timed out") 28 | 29 | # Parse response into header and content 30 | header, _, content = response.partition(b'\r\n\r\n') 31 | 32 | # Decode and print headers 33 | header = header.decode() 34 | print(header) 35 | 36 | # Check if the content is gzip compressed 37 | if 'gzip' in header: 38 | buf = BytesIO(content) 39 | f = gzip.GzipFile(fileobj=buf) 40 | content = f.read() 41 | 42 | # Parse HTML content with BeautifulSoup 43 | soup = BeautifulSoup(content, 'lxml') 44 | print(soup.get_text()) 45 | -------------------------------------------------------------------------------- /bab06/ftp-delete.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the file name to be deleted 13 | file_to_delete = 'unwantedfile.txt' 14 | 15 | # Use delete to remove the file from the server 16 | ftp.delete(file_to_delete) 17 | 18 | # Print confirmation message 19 | print(f"The file {file_to_delete} has been successfully deleted.") 20 | 21 | # Properly close the connection 22 | ftp.quit() 23 | -------------------------------------------------------------------------------- /bab06/ftp-mkd.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the name of the new directory to create 13 | new_directory = '/new_folder' 14 | 15 | # Create the new directory 16 | try: 17 | result = ftp.mkd(new_directory) 18 | print(f"Directory created: {result}") 19 | except Exception as e: 20 | print(f"Error: {e}") 21 | 22 | # Properly close the connection 23 | ftp.quit() 24 | -------------------------------------------------------------------------------- /bab06/ftp-mlsd.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the directory to list, use '.' for current directory 13 | directory = '.' 14 | 15 | # Use mlsd to get detailed list of files in the specified directory 16 | print(f"Contents of directory {directory}:") 17 | for filename, facts in ftp.mlsd(directory): 18 | print(f"{filename}:") 19 | for fact_name, fact_value in facts.items(): 20 | print(f" {fact_name}: {fact_value}") 21 | 22 | # Properly close the connection 23 | ftp.quit() 24 | -------------------------------------------------------------------------------- /bab06/ftp-pwd.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | f = FTP('localhost') 4 | print("Welcome:", f.getwelcome()) 5 | 6 | f.login('hudan', '123') 7 | print("Current working directory:", f.pwd()) 8 | names = f.nlst() 9 | print('List of directory: ', names) 10 | f.quit() 11 | -------------------------------------------------------------------------------- /bab06/ftp-rename.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the current file name and the new file name 13 | old_file_name = 'upload.txt' 14 | new_file_name = 'new-upload.txt' 15 | 16 | # Use rename to change the file name on the server 17 | ftp.rename(old_file_name, new_file_name) 18 | 19 | # Print confirmation message 20 | print(f"The file {old_file_name} has been renamed to {new_file_name}.") 21 | 22 | # Properly close the connection 23 | ftp.quit() 24 | -------------------------------------------------------------------------------- /bab06/ftp-retr.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | f = FTP('localhost') 4 | f.login('hudan', '123') 5 | 6 | fd = open('2019.pdf', 'wb') 7 | f.retrbinary('RETR 2019.pdf', fd.write) 8 | fd.close() 9 | f.quit() 10 | -------------------------------------------------------------------------------- /bab06/ftp-retrbinary.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | def handle_binary_data(data): 4 | """Callback function to write binary data to a local file.""" 5 | with open('downloaded_file.zip', 'ab') as f: 6 | f.write(data) 7 | 8 | # Specify the FTP server host 9 | host = 'localhost' 10 | 11 | # Create an FTP object and connect to the FTP server 12 | ftp = FTP(host) 13 | 14 | # Log in to the server with the default user (anonymous) 15 | ftp.login('hudan', '123') 16 | 17 | # Specify the name of the file you want to download 18 | remote_file = 'example.zip' 19 | 20 | # Open the local file for appending binary data 21 | with open('downloaded_file.zip', 'wb') as f: 22 | # Use retrbinary to download the file 23 | ftp.retrbinary(f'RETR {remote_file}', handle_binary_data) 24 | 25 | # Print completion message 26 | print("File has been downloaded successfully.") 27 | 28 | # Properly close the connection 29 | ftp.quit() 30 | -------------------------------------------------------------------------------- /bab06/ftp-rmd.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the name of the directory to be removed 13 | directory_to_remove = '/new_folder' 14 | 15 | # Remove the directory 16 | try: 17 | ftp.rmd(directory_to_remove) 18 | print(f"Directory '{directory_to_remove}' has been removed successfully.") 19 | except Exception as e: 20 | print(f"Failed to remove directory: {e}") 21 | 22 | # Properly close the connection 23 | ftp.quit() 24 | -------------------------------------------------------------------------------- /bab06/ftp-sendcmd.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Send a custom command (e.g., 'FEAT' to list all features the server supports) 13 | response = ftp.sendcmd('FEAT') 14 | 15 | # Print the server's response to the 'FEAT' command 16 | print("Server features:\n", response) 17 | 18 | # Properly close the connection 19 | ftp.quit() 20 | -------------------------------------------------------------------------------- /bab06/ftp-stor.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | f = FTP('localhost') 4 | f.login('anonymous', '') 5 | 6 | fd = open('icons.svg', 'rb') 7 | f.storbinary('STOR icons.svg', fd) 8 | fd.close() 9 | 10 | f.quit() 11 | 12 | 13 | -------------------------------------------------------------------------------- /bab06/ftp-storbinary.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Specify the local file name to upload 13 | local_file = 'example.zip' 14 | 15 | # Specify the remote file name under which to store the file 16 | remote_file = 'uploaded_example.zip' 17 | 18 | # Open the local file in binary read mode 19 | with open(local_file, 'rb') as file: 20 | # Use storbinary to upload the file, including the command to start the transfer 21 | ftp.storbinary(f'STOR {remote_file}', file) 22 | 23 | # Print completion message 24 | print(f"File {local_file} has been uploaded successfully as {remote_file}.") 25 | 26 | # Properly close the connection 27 | ftp.quit() 28 | -------------------------------------------------------------------------------- /bab06/ftp-welcome.py: -------------------------------------------------------------------------------- 1 | from ftplib import FTP 2 | 3 | # Specify the FTP server host 4 | host = 'localhost' 5 | 6 | # Create an FTP object and connect to the FTP server 7 | ftp = FTP(host) 8 | 9 | # Log in to the server with the default user (anonymous) 10 | ftp.login('hudan', '123') 11 | 12 | # Retrieve and print the welcome message from the FTP server 13 | welcome_message = ftp.getwelcome() 14 | print("Welcome message from the server:", welcome_message) 15 | 16 | # Close the connection 17 | ftp.quit() 18 | -------------------------------------------------------------------------------- /bab06/raw-list.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | class FTP: 4 | def __init__(self, host='', user='', passwd='', timeout=socket._GLOBAL_DEFAULT_TIMEOUT): 5 | self.host = host 6 | self.user = user 7 | self.passwd = passwd 8 | self.timeout = timeout 9 | self.sock = None 10 | self.file = None 11 | 12 | def connect(self): 13 | self.sock = socket.create_connection((self.host, 21), self.timeout) 14 | self.file = self.sock.makefile('r') 15 | self.getresp() 16 | 17 | def getresp(self): 18 | resp = self.file.readline() 19 | return resp 20 | 21 | def sendcmd(self, cmd): 22 | self.sock.sendall(cmd.encode('ascii') + b'\r\n') 23 | return self.getresp() 24 | 25 | def login(self): 26 | self.sendcmd(f'USER {self.user}') 27 | self.sendcmd(f'PASS {self.passwd}') 28 | 29 | def retrlines(self, cmd='LIST'): 30 | self.sendcmd('PASV') 31 | resp = self.getresp() 32 | start = resp.find('(') + 1 33 | end = resp.find(')', start) 34 | numbers = list(map(int, resp[start:end].split(','))) 35 | ip = '.'.join(map(str, numbers[:4])) 36 | port = (numbers[4] << 8) + numbers[5] 37 | 38 | data_sock = socket.create_connection((ip, port), self.timeout) 39 | self.sendcmd(cmd) 40 | data_file = data_sock.makefile('r') 41 | for line in data_file: 42 | print(line.strip()) 43 | data_sock.close() 44 | self.getresp() 45 | 46 | def quit(self): 47 | self.sendcmd('QUIT') 48 | self.sock.close() 49 | self.file.close() 50 | 51 | # Usage example 52 | ftp = FTP('127.0.0.1', 'hudan', '123') 53 | ftp.connect() 54 | ftp.login() 55 | ftp.retrlines() 56 | ftp.quit() 57 | -------------------------------------------------------------------------------- /bab07/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/bab07/.DS_Store -------------------------------------------------------------------------------- /bab07/client-udp1.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | server_address = ('127.0.0.1', 5000) 4 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | 6 | message = b'Hi ...' 7 | client_socket.sendto(message, server_address) 8 | 9 | recv_message, server_address = client_socket.recvfrom(1024) 10 | print(recv_message, server_address) 11 | -------------------------------------------------------------------------------- /bab07/client-udp2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | server_address = ('127.0.0.1', 5001) 4 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | client_socket.connect(server_address) 6 | 7 | message = b'Hi ...' 8 | client_socket.send(message) 9 | recv_message = client_socket.recv(1024) 10 | print(recv_message) 11 | -------------------------------------------------------------------------------- /bab07/client-udp3.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | server_address = ('127.0.0.1', 5001) 4 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | client_socket.connect(server_address) 6 | 7 | message = b'Hi ...' 8 | delay = 1 9 | 10 | while True: 11 | client_socket.send(message) 12 | client_socket.settimeout(delay) 13 | try: 14 | recv_message = client_socket.recv(1024) 15 | print(recv_message, 'delay:', delay) 16 | except socket.timeout: 17 | delay *= 2 18 | if delay > 2: 19 | print('Server is down') 20 | 21 | else: 22 | break 23 | -------------------------------------------------------------------------------- /bab07/server-udp.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | server_address = ('127.0.0.1', 5000) 4 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 5 | server_socket.bind(server_address) 6 | 7 | data, client_address = server_socket.recvfrom(1024) 8 | server_socket.sendto(data, client_address) 9 | print('data:', data, 'client address', client_address) 10 | print('sock name', server_socket.getsockname()) 11 | -------------------------------------------------------------------------------- /bab07/server-udp2.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import random 3 | 4 | server_address = ('127.0.0.1', 5001) 5 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 7 | server_socket.bind(server_address) 8 | 9 | while True: 10 | data, client_address = server_socket.recvfrom(1024) 11 | 12 | if random.randint(0, 1): 13 | server_socket.sendto(data, client_address) 14 | print('data:', data, 'client address', client_address) 15 | print('sock name', server_socket.getsockname()) 16 | 17 | else: 18 | print('server is down') 19 | -------------------------------------------------------------------------------- /bab08/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/bab08/.DS_Store -------------------------------------------------------------------------------- /bab08/serialization-client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | import time 5 | import sys 6 | import pickle 7 | import json 8 | 9 | def now(): 10 | return time.asctime(time.localtime(time.time())) 11 | 12 | # building socket 13 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 14 | s.connect(('localhost', 5001)) 15 | 16 | while 1: 17 | # infinite send pickled message 18 | try: 19 | # arranging message 20 | msg = [] 21 | msg.append("Hi Server") 22 | msg.append(now()) 23 | msg_dict = { 24 | 'message': 'Hi Server', 25 | 'timestamp': now() 26 | } 27 | 28 | # pickling message 29 | msg = pickle.dumps(msg) 30 | msg_dict = bytes(json.dumps(msg_dict), 'utf-8') 31 | 32 | # s.send(msg) 33 | s.send(msg_dict) 34 | time.sleep(1) 35 | # exception 36 | except(KeyboardInterrupt, SystemExit): 37 | s.close() 38 | sys.exit(0) 39 | -------------------------------------------------------------------------------- /bab08/serialization-server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import socket 4 | import pickle 5 | import sys 6 | import json 7 | 8 | # some definitions 9 | SIZE = 1024 10 | 11 | # building socket 12 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | s.bind(('localhost', 5001)) 14 | s.listen(5) 15 | 16 | while 1: 17 | try: 18 | # receive connected client 19 | client, client_address = s.accept() 20 | 21 | # receive client message 22 | while 1: 23 | try: 24 | message = client.recv(SIZE) 25 | 26 | if not message: 27 | break 28 | 29 | # unpickle message and print it 30 | # message = pickle.loads(message) 31 | message = message.decode('utf-8') 32 | message = json.loads(message) 33 | print(client_address, message) 34 | except(KeyboardInterrupt, SystemExit): 35 | sys.exit(0) 36 | except(KeyboardInterrupt, SystemExit): 37 | sys.exit(0) 38 | 39 | -------------------------------------------------------------------------------- /bab08/serialization-standalone.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import pickle 4 | 5 | # declare a list 6 | mylist = [] 7 | 8 | # assigning value to list 9 | mylist.append('This is string') # string 10 | mylist.append(5) # integer 11 | mylist.append(('localhost', 5000)) # tuple 12 | 13 | # print list 14 | print("----- This is original list: -----\n") 15 | print(mylist, "\n\n") 16 | 17 | # pickle object (in this example, the object is a list) 18 | p = pickle.dumps(mylist) 19 | 20 | # print pickled object 21 | print("----- This is pickled list: -----\n") 22 | print(p, "\n\n") 23 | 24 | # unpickle object 25 | u = pickle.loads(p) 26 | 27 | # print unpickled object 28 | print("----- This is unpickled list: -----\n") 29 | print(u, "\n\n") 30 | -------------------------------------------------------------------------------- /bab09/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/bab09/.DS_Store -------------------------------------------------------------------------------- /bab09/xmlrpc-boolean-client.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | import xmlrpc.client 4 | 5 | with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy: 6 | print("3 is even: %s" % str(proxy.is_even(3))) 7 | print("100 is even: %s" % str(proxy.is_even(100))) 8 | -------------------------------------------------------------------------------- /bab09/xmlrpc-boolean-server.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | from xmlrpc.server import SimpleXMLRPCServer 4 | 5 | def is_even(n): 6 | return n % 2 == 0 7 | 8 | server = SimpleXMLRPCServer(("localhost", 8000)) 9 | print("Listening on port 8000...") 10 | server.register_function(is_even, "is_even") 11 | server.serve_forever() 12 | -------------------------------------------------------------------------------- /bab09/xmlrpc-datetime-client.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | import xmlrpc.client 4 | import datetime 5 | 6 | proxy = xmlrpc.client.ServerProxy("http://localhost:8000/") 7 | 8 | today = proxy.today() 9 | # convert the ISO8601 string to a datetime object 10 | converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S") 11 | print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M")) 12 | -------------------------------------------------------------------------------- /bab09/xmlrpc-datetime-server.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | import datetime 4 | from xmlrpc.server import SimpleXMLRPCServer 5 | import xmlrpc.client 6 | 7 | def today(): 8 | today = datetime.datetime.today() 9 | return xmlrpc.client.DateTime(today) 10 | 11 | server = SimpleXMLRPCServer(("localhost", 8000)) 12 | print("Listening on port 8000...") 13 | server.register_function(today, "today") 14 | server.serve_forever() 15 | -------------------------------------------------------------------------------- /bab09/xmlrpc-multicall-client.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | import xmlrpc.client 4 | 5 | proxy = xmlrpc.client.ServerProxy("http://localhost:8000/") 6 | multicall = xmlrpc.client.MultiCall(proxy) 7 | multicall.add(7, 3) 8 | multicall.subtract(7, 3) 9 | multicall.multiply(7, 3) 10 | multicall.divide(7, 3) 11 | result = multicall() 12 | 13 | print("7+3=%d, 7-3=%d, 7*3=%d, 7//3=%d" % tuple(result)) 14 | -------------------------------------------------------------------------------- /bab09/xmlrpc-multicall-server.py: -------------------------------------------------------------------------------- 1 | # https://docs.python.org/3/library/xmlrpc.client.html 2 | 3 | from xmlrpc.server import SimpleXMLRPCServer 4 | 5 | def add(x, y): 6 | return x + y 7 | 8 | def subtract(x, y): 9 | return x - y 10 | 11 | def multiply(x, y): 12 | return x * y 13 | 14 | def divide(x, y): 15 | return x // y 16 | 17 | # A simple server with simple arithmetic functions 18 | server = SimpleXMLRPCServer(("localhost", 8000)) 19 | print("Listening on port 8000...") 20 | server.register_multicall_functions() 21 | server.register_function(add, 'add') 22 | server.register_function(subtract, 'subtract') 23 | server.register_function(multiply, 'multiply') 24 | server.register_function(divide, 'divide') 25 | server.serve_forever() 26 | -------------------------------------------------------------------------------- /bab10/smtp1.py: -------------------------------------------------------------------------------- 1 | import smtplib 2 | 3 | def prompt(prompt): 4 | return input(prompt).strip() 5 | 6 | fromaddr = prompt("From: ") 7 | toaddrs = prompt("To: ").split() 8 | print("Enter message, end with ^D (Unix) or ^Z (Windows):") 9 | 10 | # Add the From: and To: headers at the start! 11 | msg = ("From: %s\r\nTo: %s\r\n\r\n" 12 | % (fromaddr, ", ".join(toaddrs))) 13 | while True: 14 | try: 15 | line = input() 16 | except EOFError: 17 | break 18 | if not line: 19 | break 20 | msg = msg + line 21 | 22 | print("Message length is", len(msg)) 23 | 24 | server = smtplib.SMTP('smtp.office365.com', 587) 25 | server.set_debuglevel(1) 26 | server.starttls() 27 | server.login('hudan@its.ac.id', 'xxx') 28 | server.sendmail(fromaddr, toaddrs, msg) 29 | server.quit() 30 | -------------------------------------------------------------------------------- /javaSocket/README.txt: -------------------------------------------------------------------------------- 1 | This is javaSocket readme. Every folder is a Netbeans project folder. 2 | 3 | echoServer - simple server that echoing message back to client 4 | echoClient - simple client that send message to server 5 | echoServerSerialization - improvisation of echoServer with object serialization 6 | echoClientSerialization - echoClient with object serialization 7 | messageSerialization - a simple class that implements Serialization in Java. This project should be imported to echoServerSerialization and echoServerSerialization 8 | echoServerMulticlient - echoServer with multiclient support 9 | README.txt - The file you read just now. 10 | -------------------------------------------------------------------------------- /javaSocket/echoClient/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project echoClient. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/echoClient/build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClient/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /javaSocket/echoClient/build/classes/.netbeans_update_resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClient/build/classes/.netbeans_update_resources -------------------------------------------------------------------------------- /javaSocket/echoClient/build/classes/echoclient/EchoClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClient/build/classes/echoclient/EchoClient.class -------------------------------------------------------------------------------- /javaSocket/echoClient/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/build-impl.xml: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | Must set src.dir 232 | Must set test.src.dir 233 | Must set build.dir 234 | Must set dist.dir 235 | Must set build.classes.dir 236 | Must set dist.javadoc.dir 237 | Must set build.test.classes.dir 238 | Must set build.test.results.dir 239 | Must set build.classes.excludes 240 | Must set dist.jar 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | Must set javac.includes 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | No tests executed. 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | Must set JVM to use for profiling in profiler.info.jvm 719 | Must set profiler agent JVM arguments in profiler.info.jvmargs.agent 720 | 721 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | Must select some files in the IDE or set javac.includes 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | To run this application from the command line without Ant, try: 982 | 983 | 984 | 985 | 986 | 987 | 988 | java -cp "${run.classpath.with.dist.jar}" ${main.class} 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | To run this application from the command line without Ant, try: 1014 | 1015 | java -jar "${dist.jar.resolved}" 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | Must select one file in the IDE or set run.class 1045 | 1046 | 1047 | 1048 | Must select one file in the IDE or set run.class 1049 | 1050 | 1051 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | Must select one file in the IDE or set debug.class 1076 | 1077 | 1078 | 1079 | 1080 | Must select one file in the IDE or set debug.class 1081 | 1082 | 1083 | 1084 | 1085 | Must set fix.includes 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1097 | 1100 | 1101 | This target only works when run from inside the NetBeans IDE. 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | Must select one file in the IDE or set profile.class 1111 | This target only works when run from inside the NetBeans IDE. 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | This target only works when run from inside the NetBeans IDE. 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | This target only works when run from inside the NetBeans IDE. 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | Must select one file in the IDE or set run.class 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | Must select some files in the IDE or set test.includes 1178 | 1179 | 1180 | 1181 | 1182 | Must select one file in the IDE or set run.class 1183 | 1184 | 1185 | 1186 | 1187 | Must select one file in the IDE or set applet.url 1188 | 1189 | 1190 | 1191 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | Must select some files in the IDE or set javac.includes 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1280 | 1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | Some tests failed; see details above. 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | Must select some files in the IDE or set test.includes 1297 | 1298 | 1299 | 1300 | Some tests failed; see details above. 1301 | 1302 | 1303 | 1304 | Must select some files in the IDE or set test.class 1305 | Must select some method in the IDE or set test.method 1306 | 1307 | 1308 | 1309 | Some tests failed; see details above. 1310 | 1311 | 1312 | 1317 | 1318 | Must select one file in the IDE or set test.class 1319 | 1320 | 1321 | 1322 | Must select one file in the IDE or set test.class 1323 | Must select some method in the IDE or set test.method 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1340 | 1341 | Must select one file in the IDE or set applet.url 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1353 | 1354 | Must select one file in the IDE or set applet.url 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=1fa197e6 2 | build.xml.script.CRC32=f7847161 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=1fa197e6 7 | nbproject/build-impl.xml.script.CRC32=a8ba1fb6 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\Hudan\\AppData\\Roaming\\NetBeans\\7.3\\build.properties 3 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | file:/C:/Users/Hudan/Documents/NetBeansProjects/echoClient/src/echoclient/EchoClient.java 6 | 7 | 8 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/echoClient.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath= 31 | # Space-separated list of extra javac options 32 | javac.compilerargs= 33 | javac.deprecation=false 34 | javac.processorpath=\ 35 | ${javac.classpath} 36 | javac.source=1.7 37 | javac.target=1.7 38 | javac.test.classpath=\ 39 | ${javac.classpath}:\ 40 | ${build.classes.dir} 41 | javac.test.processorpath=\ 42 | ${javac.test.classpath} 43 | javadoc.additionalparam= 44 | javadoc.author=false 45 | javadoc.encoding=${source.encoding} 46 | javadoc.noindex=false 47 | javadoc.nonavbar=false 48 | javadoc.notree=false 49 | javadoc.private=false 50 | javadoc.splitindex=true 51 | javadoc.use=true 52 | javadoc.version=false 53 | javadoc.windowtitle= 54 | main.class=echoclient.EchoClient 55 | manifest.file=manifest.mf 56 | meta.inf.dir=${src.dir}/META-INF 57 | mkdist.disabled=false 58 | platform.active=default_platform 59 | run.classpath=\ 60 | ${javac.classpath}:\ 61 | ${build.classes.dir} 62 | # Space-separated list of JVM arguments used when running the project. 63 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 64 | # To set system properties for unit tests define test-sys-prop.name=value: 65 | run.jvmargs= 66 | run.test.classpath=\ 67 | ${javac.test.classpath}:\ 68 | ${build.test.classes.dir} 69 | source.encoding=UTF-8 70 | src.dir=src 71 | test.src.dir=test 72 | -------------------------------------------------------------------------------- /javaSocket/echoClient/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | echoClient 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /javaSocket/echoClient/src/echoclient/EchoClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package echoclient; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | 12 | import java.net.*; 13 | import java.io.*; 14 | 15 | public class EchoClient { 16 | 17 | /** 18 | * @param args the command line arguments 19 | */ 20 | public static void main(String[] args) { 21 | // TODO code application logic here 22 | try { 23 | /* 24 | * variable initialization 25 | */ 26 | Socket clientSocket; 27 | DataInputStream inputStream; 28 | DataOutputStream outputStream; 29 | String message; 30 | 31 | /* 32 | * create socket, preapring data output stream, send to server 33 | */ 34 | clientSocket = new Socket("localhost", 5000); 35 | outputStream = new DataOutputStream(clientSocket.getOutputStream()); 36 | outputStream.writeUTF("Message from client"); 37 | 38 | /* 39 | * preparing data input stream, receive message from server, print 40 | */ 41 | inputStream = new DataInputStream(clientSocket.getInputStream()); 42 | message = inputStream.readUTF(); 43 | System.out.println("From server: " + message); 44 | 45 | /* 46 | * close data output stream, data input stream, and client socket 47 | */ 48 | outputStream.close(); 49 | inputStream.close(); 50 | clientSocket.close(); 51 | } 52 | catch(IOException e) { 53 | System.out.println("IO exception: " + e.getMessage()); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project echoClientSerialization. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClientSerialization/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/build/classes/.netbeans_update_resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClientSerialization/build/classes/.netbeans_update_resources -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/build/classes/echoclientserialization/EchoClientSerialization.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoClientSerialization/build/classes/echoclientserialization/EchoClientSerialization.class -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=2eac1ae8 2 | build.xml.script.CRC32=63da0d94 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=2eac1ae8 7 | nbproject/build-impl.xml.script.CRC32=3ecd0a0a 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\Hudan\\AppData\\Roaming\\NetBeans\\7.3\\build.properties 3 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/echoClientSerialization.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath=\ 31 | ${reference.messageSerialization.jar} 32 | # Space-separated list of extra javac options 33 | javac.compilerargs= 34 | javac.deprecation=false 35 | javac.processorpath=\ 36 | ${javac.classpath} 37 | javac.source=1.7 38 | javac.target=1.7 39 | javac.test.classpath=\ 40 | ${javac.classpath}:\ 41 | ${build.classes.dir} 42 | javac.test.processorpath=\ 43 | ${javac.test.classpath} 44 | javadoc.additionalparam= 45 | javadoc.author=false 46 | javadoc.encoding=${source.encoding} 47 | javadoc.noindex=false 48 | javadoc.nonavbar=false 49 | javadoc.notree=false 50 | javadoc.private=false 51 | javadoc.splitindex=true 52 | javadoc.use=true 53 | javadoc.version=false 54 | javadoc.windowtitle= 55 | main.class=echoclientserialization.EchoClientSerialization 56 | manifest.file=manifest.mf 57 | meta.inf.dir=${src.dir}/META-INF 58 | mkdist.disabled=false 59 | platform.active=default_platform 60 | project.messageSerialization=../messageSerialization 61 | reference.messageSerialization.jar=${project.messageSerialization}/dist/messageSerialization.jar 62 | run.classpath=\ 63 | ${javac.classpath}:\ 64 | ${build.classes.dir} 65 | # Space-separated list of JVM arguments used when running the project. 66 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 67 | # To set system properties for unit tests define test-sys-prop.name=value: 68 | run.jvmargs= 69 | run.test.classpath=\ 70 | ${javac.test.classpath}:\ 71 | ${build.test.classes.dir} 72 | source.encoding=UTF-8 73 | src.dir=src 74 | test.src.dir=test 75 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | echoClientSerialization 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | messageSerialization 17 | jar 18 | 19 | jar 20 | clean 21 | jar 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /javaSocket/echoClientSerialization/src/echoclientserialization/EchoClientSerialization.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package echoclientserialization; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | 12 | import messageserialization.Message; 13 | import java.net.*; 14 | import java.io.*; 15 | 16 | public class EchoClientSerialization { 17 | 18 | /** 19 | * @param args the command line arguments 20 | */ 21 | public static void main(String[] args) { 22 | // TODO code application logic here 23 | try { 24 | /* 25 | * variable initialization 26 | */ 27 | Socket clientSocket; 28 | ObjectInputStream inputStream; 29 | ObjectOutputStream outputStream; 30 | Message message; 31 | 32 | /* 33 | * create socket, preparing object output stream, send object to server 34 | */ 35 | clientSocket = new Socket("localhost", 5000); 36 | outputStream = new ObjectOutputStream(clientSocket.getOutputStream()); 37 | outputStream.writeObject(new Message("Test from client", 1)); 38 | outputStream.flush(); 39 | 40 | /* 41 | * preparing object input stream, receive message from server, print 42 | */ 43 | inputStream = new ObjectInputStream(clientSocket.getInputStream()); 44 | try { 45 | message = (Message) inputStream.readObject(); 46 | System.out.println("From server: " + message.getString() + " " + message.getInteger()); 47 | } catch (ClassNotFoundException ex) { 48 | System.out.println("ClassNotFound: " + ex.getMessage()); 49 | } 50 | 51 | /* 52 | * close data output stream, data input stream, and client socket 53 | */ 54 | outputStream.close(); 55 | inputStream.close(); 56 | clientSocket.close(); 57 | } 58 | catch(IOException e) { 59 | System.out.println("IO exception: " + e.getMessage()); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /javaSocket/echoServer/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project echoServer. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/echoServer/build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServer/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /javaSocket/echoServer/build/classes/.netbeans_update_resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServer/build/classes/.netbeans_update_resources -------------------------------------------------------------------------------- /javaSocket/echoServer/build/classes/echoserver/EchoServer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServer/build/classes/echoserver/EchoServer.class -------------------------------------------------------------------------------- /javaSocket/echoServer/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/echoServer/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=c8ff33f1 2 | build.xml.script.CRC32=e784e4be 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=c8ff33f1 7 | nbproject/build-impl.xml.script.CRC32=b138d6b3 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/echoServer/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\Hudan\\AppData\\Roaming\\NetBeans\\7.3\\build.properties 3 | -------------------------------------------------------------------------------- /javaSocket/echoServer/nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | file:/C:/Users/Hudan/Documents/GitHub/javaSocket/echoServer/src/echoserver/EchoServer.java 6 | 7 | 8 | -------------------------------------------------------------------------------- /javaSocket/echoServer/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/echoServer.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath= 31 | # Space-separated list of extra javac options 32 | javac.compilerargs= 33 | javac.deprecation=false 34 | javac.processorpath=\ 35 | ${javac.classpath} 36 | javac.source=1.7 37 | javac.target=1.7 38 | javac.test.classpath=\ 39 | ${javac.classpath}:\ 40 | ${build.classes.dir} 41 | javac.test.processorpath=\ 42 | ${javac.test.classpath} 43 | javadoc.additionalparam= 44 | javadoc.author=false 45 | javadoc.encoding=${source.encoding} 46 | javadoc.noindex=false 47 | javadoc.nonavbar=false 48 | javadoc.notree=false 49 | javadoc.private=false 50 | javadoc.splitindex=true 51 | javadoc.use=true 52 | javadoc.version=false 53 | javadoc.windowtitle= 54 | main.class=echoserver.EchoServer 55 | manifest.file=manifest.mf 56 | meta.inf.dir=${src.dir}/META-INF 57 | mkdist.disabled=false 58 | platform.active=default_platform 59 | run.classpath=\ 60 | ${javac.classpath}:\ 61 | ${build.classes.dir} 62 | # Space-separated list of JVM arguments used when running the project. 63 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 64 | # To set system properties for unit tests define test-sys-prop.name=value: 65 | run.jvmargs= 66 | run.test.classpath=\ 67 | ${javac.test.classpath}:\ 68 | ${build.test.classes.dir} 69 | source.encoding=UTF-8 70 | src.dir=src 71 | test.src.dir=test 72 | -------------------------------------------------------------------------------- /javaSocket/echoServer/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | echoServer 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /javaSocket/echoServer/src/echoserver/EchoServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package echoserver; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | 12 | import java.net.*; 13 | import java.io.*; 14 | 15 | public class EchoServer { 16 | 17 | /** 18 | * @param args the command line arguments 19 | */ 20 | public static void main(String[] args) { 21 | // TODO code application logic here 22 | try { 23 | /* 24 | * variable initialization 25 | */ 26 | ServerSocket serverSocket; 27 | Socket clientSocket; 28 | DataInputStream inputStream; 29 | DataOutputStream outputStream; 30 | String message; 31 | 32 | /* 33 | * create socket server, accept client, preparing input stream 34 | * receive message, and print to screen 35 | */ 36 | serverSocket = new ServerSocket(5000); 37 | clientSocket = serverSocket.accept(); 38 | inputStream = new DataInputStream(clientSocket.getInputStream()); 39 | message = inputStream.readUTF(); 40 | System.out.println("From client: " + message); 41 | 42 | /* 43 | * preparing output stream, send message back to client 44 | */ 45 | outputStream = new DataOutputStream(clientSocket.getOutputStream()); 46 | outputStream.writeUTF(message); 47 | 48 | /* 49 | * close input stream, output stream 50 | * close client socket and server socket 51 | */ 52 | inputStream.close(); 53 | outputStream.close(); 54 | clientSocket.close(); 55 | serverSocket.close(); 56 | } 57 | catch(IOException e) { 58 | System.out.println("Listen: " + e.getMessage()); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project echoServerMulticlient. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=c72cd01f 2 | build.xml.script.CRC32=57d47e65 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=c72cd01f 7 | nbproject/build-impl.xml.script.CRC32=f613ff8c 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/echoServerMulticlient.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath= 31 | # Space-separated list of extra javac options 32 | javac.compilerargs= 33 | javac.deprecation=false 34 | javac.processorpath=\ 35 | ${javac.classpath} 36 | javac.source=1.7 37 | javac.target=1.7 38 | javac.test.classpath=\ 39 | ${javac.classpath}:\ 40 | ${build.classes.dir} 41 | javac.test.processorpath=\ 42 | ${javac.test.classpath} 43 | javadoc.additionalparam= 44 | javadoc.author=false 45 | javadoc.encoding=${source.encoding} 46 | javadoc.noindex=false 47 | javadoc.nonavbar=false 48 | javadoc.notree=false 49 | javadoc.private=false 50 | javadoc.splitindex=true 51 | javadoc.use=true 52 | javadoc.version=false 53 | javadoc.windowtitle= 54 | main.class=echoservermulticlient.EchoServerMulticlient 55 | manifest.file=manifest.mf 56 | meta.inf.dir=${src.dir}/META-INF 57 | mkdist.disabled=false 58 | platform.active=default_platform 59 | run.classpath=\ 60 | ${javac.classpath}:\ 61 | ${build.classes.dir} 62 | # Space-separated list of JVM arguments used when running the project. 63 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 64 | # To set system properties for unit tests define test-sys-prop.name=value: 65 | run.jvmargs= 66 | run.test.classpath=\ 67 | ${javac.test.classpath}:\ 68 | ${build.test.classes.dir} 69 | source.encoding=UTF-8 70 | src.dir=src 71 | test.src.dir=test 72 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | echoServerMulticlient 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /javaSocket/echoServerMulticlient/src/echoservermulticlient/EchoServerMulticlient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package echoservermulticlient; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | 12 | import java.net.*; 13 | import java.io.*; 14 | 15 | public class EchoServerMulticlient { 16 | 17 | /** 18 | * @param args the command line arguments 19 | */ 20 | public static void main(String[] args) { 21 | // TODO code application logic here 22 | try { 23 | ServerSocket serverSocket; 24 | Socket clientSocket; 25 | 26 | serverSocket = new ServerSocket(5000); 27 | while(true) { 28 | clientSocket = serverSocket.accept(); 29 | Connection c = new Connection(clientSocket); 30 | } 31 | } 32 | catch(IOException ex) { 33 | System.out.println("IO: " + ex.getMessage()); 34 | } 35 | } 36 | } 37 | 38 | class Connection extends Thread { 39 | DataInputStream inputStream; 40 | DataOutputStream outputStream; 41 | Socket clientSocket; 42 | 43 | public Connection(Socket client) { 44 | try { 45 | clientSocket = client; 46 | inputStream = new DataInputStream(clientSocket.getInputStream()); 47 | outputStream = new DataOutputStream(clientSocket.getOutputStream()); 48 | this.start(); 49 | } 50 | catch(IOException ex) { 51 | System.out.println("IO: " + ex.getMessage()); 52 | } 53 | } 54 | 55 | @Override 56 | public void run() { 57 | try { 58 | String message = inputStream.readUTF(); 59 | System.out.println("From client: " + message); 60 | outputStream.writeUTF(message); 61 | } 62 | catch(IOException ex) { 63 | System.out.println(ex.getMessage()); 64 | } 65 | finally { 66 | try { 67 | clientSocket.close(); 68 | } catch (IOException ex) { 69 | System.out.println(ex.getMessage()); 70 | } 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project echoServerSerialization. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServerSerialization/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/build/classes/.netbeans_update_resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServerSerialization/build/classes/.netbeans_update_resources -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/build/classes/echoserverserialization/EchoServerSerialization.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/echoServerSerialization/build/classes/echoserverserialization/EchoServerSerialization.class -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=5ba3b3eb 2 | build.xml.script.CRC32=b78b1cd6 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=5ba3b3eb 7 | nbproject/build-impl.xml.script.CRC32=55814d99 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\Hudan\\AppData\\Roaming\\NetBeans\\7.3\\build.properties 3 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/echoServerSerialization.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath=\ 31 | ${reference.messageSerialization.jar} 32 | # Space-separated list of extra javac options 33 | javac.compilerargs= 34 | javac.deprecation=false 35 | javac.processorpath=\ 36 | ${javac.classpath} 37 | javac.source=1.7 38 | javac.target=1.7 39 | javac.test.classpath=\ 40 | ${javac.classpath}:\ 41 | ${build.classes.dir} 42 | javac.test.processorpath=\ 43 | ${javac.test.classpath} 44 | javadoc.additionalparam= 45 | javadoc.author=false 46 | javadoc.encoding=${source.encoding} 47 | javadoc.noindex=false 48 | javadoc.nonavbar=false 49 | javadoc.notree=false 50 | javadoc.private=false 51 | javadoc.splitindex=true 52 | javadoc.use=true 53 | javadoc.version=false 54 | javadoc.windowtitle= 55 | main.class=echoserverserialization.EchoServerSerialization 56 | manifest.file=manifest.mf 57 | meta.inf.dir=${src.dir}/META-INF 58 | mkdist.disabled=false 59 | platform.active=default_platform 60 | project.messageSerialization=../messageSerialization 61 | reference.messageSerialization.jar=${project.messageSerialization}/dist/messageSerialization.jar 62 | run.classpath=\ 63 | ${javac.classpath}:\ 64 | ${build.classes.dir} 65 | # Space-separated list of JVM arguments used when running the project. 66 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 67 | # To set system properties for unit tests define test-sys-prop.name=value: 68 | run.jvmargs= 69 | run.test.classpath=\ 70 | ${javac.test.classpath}:\ 71 | ${build.test.classes.dir} 72 | source.encoding=UTF-8 73 | src.dir=src 74 | test.src.dir=test 75 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | echoServerSerialization 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | messageSerialization 17 | jar 18 | 19 | jar 20 | clean 21 | jar 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /javaSocket/echoServerSerialization/src/echoserverserialization/EchoServerSerialization.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package echoserverserialization; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | 12 | import messageserialization.Message; 13 | import java.net.*; 14 | import java.io.*; 15 | 16 | public class EchoServerSerialization { 17 | 18 | /** 19 | * @param args the command line arguments 20 | */ 21 | public static void main(String[] args) { 22 | // TODO code application logic here 23 | try { 24 | /* 25 | * variable initialization 26 | */ 27 | ServerSocket serverSocket; 28 | Socket clientSocket; 29 | ObjectInputStream inputStream; 30 | ObjectOutputStream outputStream; 31 | Message message = null; 32 | 33 | /* 34 | * create socket server, accept client, preparing object input stream 35 | * receive message, and print to screen 36 | */ 37 | serverSocket = new ServerSocket(5000); 38 | clientSocket = serverSocket.accept(); 39 | inputStream = new ObjectInputStream(clientSocket.getInputStream()); 40 | try { 41 | message = (Message) inputStream.readObject(); 42 | System.out.println("From client: " + message.getString() + " " + message.getInteger()); 43 | } catch (ClassNotFoundException ex) { 44 | System.out.println("ClassNotFound: " + ex.getMessage()); 45 | } 46 | /* 47 | * preparing object output stream, send message back to client 48 | */ 49 | outputStream = new ObjectOutputStream(clientSocket.getOutputStream()); 50 | outputStream.writeObject(new Message(message.getString(), message.getInteger())); 51 | outputStream.flush(); 52 | 53 | /* 54 | * close input stream, output stream 55 | * close client socket and server socket 56 | */ 57 | inputStream.close(); 58 | outputStream.close(); 59 | clientSocket.close(); 60 | serverSocket.close(); 61 | } 62 | catch(IOException e) { 63 | System.out.println("Listen: " + e.getMessage()); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project messageSerialization. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build/built-jar.properties: -------------------------------------------------------------------------------- 1 | #Mon, 11 Mar 2013 09:01:52 +0700 2 | 3 | 4 | C\:\\Users\\Hudan\\Documents\\GitHub\\javaSocket\\messageSerialization= 5 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build/classes/.netbeans_automatic_build: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/messageSerialization/build/classes/.netbeans_automatic_build -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build/classes/.netbeans_update_resources: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/messageSerialization/build/classes/.netbeans_update_resources -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build/classes/messageserialization/Message.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/messageSerialization/build/classes/messageserialization/Message.class -------------------------------------------------------------------------------- /javaSocket/messageSerialization/build/classes/messageserialization/MessageSerialization.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/messageSerialization/build/classes/messageserialization/MessageSerialization.class -------------------------------------------------------------------------------- /javaSocket/messageSerialization/dist/README.TXT: -------------------------------------------------------------------------------- 1 | ======================== 2 | BUILD OUTPUT DESCRIPTION 3 | ======================== 4 | 5 | When you build an Java application project that has a main class, the IDE 6 | automatically copies all of the JAR 7 | files on the projects classpath to your projects dist/lib folder. The IDE 8 | also adds each of the JAR files to the Class-Path element in the application 9 | JAR files manifest file (MANIFEST.MF). 10 | 11 | To run the project from the command line, go to the dist folder and 12 | type the following: 13 | 14 | java -jar "messageSerialization.jar" 15 | 16 | To distribute this project, zip up the dist folder (including the lib folder) 17 | and distribute the ZIP file. 18 | 19 | Notes: 20 | 21 | * If two JAR files on the project classpath have the same name, only the first 22 | JAR file is copied to the lib folder. 23 | * Only JAR files are copied to the lib folder. 24 | If the classpath contains other types of files or folders, these files (folders) 25 | are not copied. 26 | * If a library on the projects classpath also has a Class-Path element 27 | specified in the manifest,the content of the Class-Path element has to be on 28 | the projects runtime path. 29 | * To set a main class in a standard Java project, right-click the project node 30 | in the Projects window and choose Properties. Then click Run and enter the 31 | class name in the Main Class field. Alternatively, you can manually type the 32 | class name in the manifest Main-Class element. 33 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/dist/messageSerialization.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/studiawan/network-programming/10c3e4cb9b2bab9d2ee1e07fa72274f50b3c942c/javaSocket/messageSerialization/dist/messageSerialization.jar -------------------------------------------------------------------------------- /javaSocket/messageSerialization/manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=4be163f7 2 | build.xml.script.CRC32=15158839 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=4be163f7 7 | nbproject/build-impl.xml.script.CRC32=d313c459 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=true 2 | user.properties.file=C:\\Users\\Hudan\\AppData\\Roaming\\NetBeans\\7.3\\build.properties 3 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/messageSerialization.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | includes=** 29 | jar.compress=false 30 | javac.classpath= 31 | # Space-separated list of extra javac options 32 | javac.compilerargs= 33 | javac.deprecation=false 34 | javac.processorpath=\ 35 | ${javac.classpath} 36 | javac.source=1.7 37 | javac.target=1.7 38 | javac.test.classpath=\ 39 | ${javac.classpath}:\ 40 | ${build.classes.dir} 41 | javac.test.processorpath=\ 42 | ${javac.test.classpath} 43 | javadoc.additionalparam= 44 | javadoc.author=false 45 | javadoc.encoding=${source.encoding} 46 | javadoc.noindex=false 47 | javadoc.nonavbar=false 48 | javadoc.notree=false 49 | javadoc.private=false 50 | javadoc.splitindex=true 51 | javadoc.use=true 52 | javadoc.version=false 53 | javadoc.windowtitle= 54 | main.class=messageserialization.MessageSerialization 55 | manifest.file=manifest.mf 56 | meta.inf.dir=${src.dir}/META-INF 57 | mkdist.disabled=false 58 | platform.active=default_platform 59 | run.classpath=\ 60 | ${javac.classpath}:\ 61 | ${build.classes.dir} 62 | # Space-separated list of JVM arguments used when running the project. 63 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 64 | # To set system properties for unit tests define test-sys-prop.name=value: 65 | run.jvmargs= 66 | run.test.classpath=\ 67 | ${javac.test.classpath}:\ 68 | ${build.test.classes.dir} 69 | source.encoding=UTF-8 70 | src.dir=src 71 | test.src.dir=test 72 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | messageSerialization 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/src/messageserialization/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package messageserialization; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * 11 | * @author Hudan 12 | */ 13 | public class Message implements Serializable { 14 | private String message; 15 | private int value; 16 | 17 | public Message(String message, int value) { 18 | this.message = message; 19 | this.value = value; 20 | } 21 | 22 | public String getString() { 23 | return message; 24 | } 25 | 26 | public int getInteger() { 27 | return value; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /javaSocket/messageSerialization/src/messageserialization/MessageSerialization.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package messageserialization; 6 | 7 | /** 8 | * 9 | * @author Hudan 10 | */ 11 | public class MessageSerialization { 12 | 13 | /** 14 | * @param args the command line arguments 15 | */ 16 | public static void main(String[] args) { 17 | // TODO code application logic here 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /pythonSocket/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | echo 4 | 5 | 6 | 7 | 8 | 9 | org.python.pydev.PyDevBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.python.pydev.pythonNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /pythonSocket/.pydevproject: -------------------------------------------------------------------------------- 1 | 2 | 3 | python 2.7 4 | Default 5 | 6 | -------------------------------------------------------------------------------- /pythonSocket/README.txt: -------------------------------------------------------------------------------- 1 | Implementation of simple socket programming in Python. 2 | 3 | echoServer - server that receives message and echoes it back to client 4 | echoClient - send message to server and receive the same message from server 5 | echoServerMulticlient - echoServer with multi client support -------------------------------------------------------------------------------- /pythonSocket/echoClient.py: -------------------------------------------------------------------------------- 1 | # import socket module 2 | import socket 3 | 4 | # creating socket client 5 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | 7 | # connect to server in defined address and port 8 | client_socket.connect(('localhost', 5000)) 9 | 10 | # send message to server 11 | client_socket.send("Hi server ... ") 12 | 13 | # receive message from server, 1024 is buffer size in bytes 14 | message = client_socket.recv(1024) 15 | 16 | # print message 17 | print "From server: " + message 18 | 19 | # close socket client 20 | client_socket.close() -------------------------------------------------------------------------------- /pythonSocket/echoClientSerialization.py: -------------------------------------------------------------------------------- 1 | # import socket module 2 | import socket 3 | import pickle 4 | 5 | # creating socket client 6 | client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | 8 | # connect to server in defined address and port 9 | client_socket.connect(('localhost', 5000)) 10 | 11 | # send message to server 12 | message = "Hi server ... " 13 | message = pickle.dumps(message) 14 | client_socket.send(message) 15 | 16 | # receive message from server, 1024 is buffer size in bytes 17 | message = client_socket.recv(1024) 18 | message = pickle.loads(message) 19 | 20 | # print message 21 | print "From server: " + message 22 | 23 | # close socket client 24 | client_socket.close() -------------------------------------------------------------------------------- /pythonSocket/echoServer.py: -------------------------------------------------------------------------------- 1 | # import socket module 2 | import socket 3 | import sys 4 | 5 | # creating socket server object 6 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | 8 | # bind socket server to defined server address and port in tuple 9 | server_socket.bind(('localhost', 5000)) 10 | 11 | # listening connection from client, only 1 backlog 12 | server_socket.listen(1) 13 | 14 | # infinite loop for receiving message from client 15 | try: 16 | while 1: 17 | # receiving client socket 18 | client_socket, client_address = server_socket.accept() 19 | 20 | # receive message from client, 1024 is buffer size in bytes 21 | message = client_socket.recv(1024) 22 | print "From client: " + message 23 | 24 | # send message back to client 25 | client_socket.send(message) 26 | 27 | # close client socket 28 | client_socket.close() 29 | 30 | # when user press CTRL + C (in Linux), close socket server and exit 31 | except KeyboardInterrupt: 32 | server_socket.close() 33 | sys.exit(0) -------------------------------------------------------------------------------- /pythonSocket/echoServerMulticlient.py: -------------------------------------------------------------------------------- 1 | # import required module 2 | import socket 3 | import select 4 | import sys 5 | 6 | # creating socket server object, bind, and listen 7 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | server_socket.bind(('localhost', 5000)) 9 | server_socket.listen(5) 10 | 11 | # list to store accepted client 12 | input_list = [server_socket] 13 | 14 | try: 15 | while 1: 16 | # serving multiple client alternately; one socket in a time 17 | input, output, exception = select.select(input_list, [], []) 18 | 19 | for socket in input: 20 | # accept client and add it to list input 21 | if socket == server_socket: 22 | client_socket, client_address = server_socket.accept() 23 | input_list.append(client_socket) 24 | print "Accepted client: ", client_address 25 | 26 | # handle sending and receiving message 27 | else: 28 | message = socket.recv(1024) 29 | if message: 30 | socket.send(message) 31 | print "Send to client : ", client_address, message 32 | else: 33 | socket.close() 34 | input_list.remove(socket) 35 | 36 | # when user press CTRL + C (in Linux), close socket server and exit 37 | except KeyboardInterrupt: 38 | server_socket.close() 39 | sys.exit(0) -------------------------------------------------------------------------------- /pythonSocket/echoServerSerialization.py: -------------------------------------------------------------------------------- 1 | # import required module 2 | import socket 3 | import select 4 | import sys 5 | import pickle # module for object serialization in python 6 | 7 | # creating socket server object, bind, and listen 8 | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | server_socket.bind(('localhost', 5000)) 10 | server_socket.listen(5) 11 | 12 | # list to store accepted client 13 | input_list = [server_socket] 14 | 15 | try: 16 | while 1: 17 | # serving multiple client alternately; one socket in a time 18 | input, output, exception = select.select(input_list, [], []) 19 | 20 | for socket in input: 21 | # accept client and add it to list input 22 | if socket == server_socket: 23 | client_socket, client_address = server_socket.accept() 24 | input_list.append(client_socket) 25 | print "Accepted client: ", client_address 26 | 27 | # handle sending and receiving message 28 | else: 29 | message = socket.recv(1024) 30 | if message: 31 | # unpickle message 32 | message = pickle.loads(message) 33 | print "Send to client : ", client_address, message 34 | 35 | # pickle message and send it back to client 36 | message = pickle.dumps(message) 37 | socket.send(message) 38 | 39 | else: 40 | socket.close() 41 | input_list.remove(socket) 42 | 43 | # when user press CTRL + C (in Linux), close socket server and exit 44 | except (KeyboardInterrupt, SystemExit): 45 | server_socket.close() 46 | sys.exit(0) --------------------------------------------------------------------------------