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