Differences between revisions 5 and 6
Revision 5 as of 2009-03-16 08:55:05
Size: 1447
Editor: 124-168-113-153
Comment:
Revision 6 as of 2012-09-05 08:32:33
Size: 1511
Editor: dhcp-069093
Comment: wiki restore 2013-01-23
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 2: Line 3:
Line 5: Line 7:
See also: UdpCommunication See also: [[UdpCommunication|UdpCommunication]]
Line 9: Line 12:
Line 11: Line 15:
{{{
#!python






{{{#!python
Line 21: Line 30:
MESSAGE="Hello, World!" MESSAGE = "Hello, World!"
Line 33: Line 42:
Line 34: Line 44:
Line 37: Line 48:
{{{
#!python






{{{#!python
Line 46: Line 62:
BUFFER_SIZE = 10 # Normally 1024, but we want fast response BUFFER_SIZE = 20 # Normally 1024, but we want fast response
Line 62: Line 78:

Line 63: Line 81:
Line 68: Line 87:
Line 70: Line 90:
  (none yet!)
. (none yet!)

TCP Communication

See also: UdpCommunication

Client

Here's simple code to send and receive data by TCP in Python:

   1 #!/usr/bin/env python
   2 
   3 import socket
   4 
   5 
   6 TCP_IP = '127.0.0.1'
   7 TCP_PORT = 5005
   8 BUFFER_SIZE = 1024
   9 MESSAGE = "Hello, World!"
  10 
  11 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12 s.connect((TCP_IP, TCP_PORT))
  13 s.send(MESSAGE)
  14 data = s.recv(BUFFER_SIZE)
  15 s.close()
  16 
  17 print "received data:", data

Server

Here's simple code to serve TCP in Python:

   1 #!/usr/bin/env python
   2 
   3 import socket
   4 
   5 
   6 TCP_IP = '127.0.0.1'
   7 TCP_PORT = 5005
   8 BUFFER_SIZE = 20  # Normally 1024, but we want fast response
   9 
  10 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11 s.bind((TCP_IP, TCP_PORT))
  12 s.listen(1)
  13 
  14 conn, addr = s.accept()
  15 print 'Connection address:', addr
  16 while 1:
  17     data = conn.recv(BUFFER_SIZE)
  18     if not data: break
  19     print "received data:", data
  20     conn.send(data)  # echo
  21 conn.close()

Discussion

  • (none yet!)

TcpCommunication (last edited 2012-09-05 08:32:33 by dhcp-069093)

Unable to edit the page? See the FrontPage for instructions.