1 | NO CONTENT: new file 10644 |
|
NO CONTENT: new file 10644 |
@@ -0,0 +1,4 | |||||
|
1 | from udp import * | |||
|
2 | ||||
|
3 | clientObj = Client() | |||
|
4 | clientObj.start() No newline at end of file |
@@ -0,0 +1,4 | |||||
|
1 | from udp import * | |||
|
2 | ||||
|
3 | serverObj = Server() | |||
|
4 | serverObj.start() No newline at end of file |
@@ -0,0 +1,124 | |||||
|
1 | # Server program | |||
|
2 | # UDP VERSION | |||
|
3 | ||||
|
4 | ||||
|
5 | import socket | |||
|
6 | ||||
|
7 | class Server: | |||
|
8 | ||||
|
9 | # Set the socket parameters | |||
|
10 | host = "localhost" | |||
|
11 | port = 5140 | |||
|
12 | buf = 1024 | |||
|
13 | addr = (host,port) | |||
|
14 | ||||
|
15 | def __init__(self): | |||
|
16 | ||||
|
17 | self.createObjects() | |||
|
18 | ||||
|
19 | def createObjects(self): | |||
|
20 | ||||
|
21 | # Create socket and bind to address | |||
|
22 | self.sock_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |||
|
23 | self.sock_s.bind(self.addr) | |||
|
24 | ||||
|
25 | def listen(self): | |||
|
26 | ||||
|
27 | # Receive messages | |||
|
28 | print "Server initialized ..." | |||
|
29 | ||||
|
30 | while 1: | |||
|
31 | data_rx, addr_rx = self.sock_s.recvfrom(self.buf) | |||
|
32 | if not data_rx: | |||
|
33 | print "Client has exited!" | |||
|
34 | break | |||
|
35 | ||||
|
36 | print "\nSERVER:" | |||
|
37 | print "Received message '", data_rx,"'" | |||
|
38 | print "Sendinf ack ..." | |||
|
39 | self.sock_s.sendto('ack', addr_rx) | |||
|
40 | ||||
|
41 | # Close socket | |||
|
42 | self.sock_s.close() | |||
|
43 | ||||
|
44 | def start(self): | |||
|
45 | ||||
|
46 | self.listen() | |||
|
47 | ||||
|
48 | class Client: | |||
|
49 | ||||
|
50 | ||||
|
51 | # Set the socket parameters | |||
|
52 | host = '0.0.0.0' | |||
|
53 | port = 5140 | |||
|
54 | buf = 1024 | |||
|
55 | multicast_addr = (host,port) | |||
|
56 | ||||
|
57 | def __init__(self): | |||
|
58 | ||||
|
59 | self.createObjects() | |||
|
60 | ||||
|
61 | def createObjects(self): | |||
|
62 | ||||
|
63 | # Create socket | |||
|
64 | self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |||
|
65 | self.socket_c.settimeout(0.2) | |||
|
66 | self.socket_c.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) | |||
|
67 | ||||
|
68 | def sendData(self): | |||
|
69 | ||||
|
70 | def_msg = "===Enter message to send to server==="; | |||
|
71 | print "\n", def_msg | |||
|
72 | ||||
|
73 | ||||
|
74 | # Send messages | |||
|
75 | while (1): | |||
|
76 | print "\nCLIENT" | |||
|
77 | data = raw_input('>> ') | |||
|
78 | if not data or data == 'q': | |||
|
79 | break | |||
|
80 | ||||
|
81 | if(self.socket_c.sendto(data, self.multicast_addr)): | |||
|
82 | ||||
|
83 | print "Sending message '",data,"'....." | |||
|
84 | ||||
|
85 | data_rx, server_rx = self.socket_c.recvfrom(16) | |||
|
86 | ||||
|
87 | print "Data received ", data_rx, server_rx | |||
|
88 | ||||
|
89 | # Close socket | |||
|
90 | self.socket_c.close() | |||
|
91 | ||||
|
92 | def start(self): | |||
|
93 | ||||
|
94 | self.sendData() | |||
|
95 | ||||
|
96 | import threading | |||
|
97 | ||||
|
98 | class MiThread(threading.Thread): | |||
|
99 | def __init__(self, obj): | |||
|
100 | ||||
|
101 | threading.Thread.__init__(self) | |||
|
102 | self.obj = obj | |||
|
103 | ||||
|
104 | def run(self): | |||
|
105 | ||||
|
106 | self.obj.start() | |||
|
107 | ||||
|
108 | ||||
|
109 | if __name__ == '__main__': | |||
|
110 | ||||
|
111 | serverObj = Server() | |||
|
112 | clientObj = Client() | |||
|
113 | ||||
|
114 | ts = MiThread(serverObj) | |||
|
115 | tc = MiThread(clientObj) | |||
|
116 | ||||
|
117 | ts.start() | |||
|
118 | tc.start() | |||
|
119 | ||||
|
120 | tc.join() | |||
|
121 | ts.join(60) | |||
|
122 | ||||
|
123 | ||||
|
124 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now