TRiBByX
1/19/2017 - 4:55 PM

TCP Server and Client

TCP Server and Client

import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 8080
BUFFER_SIZE = 20

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connection Address: ', addr

while True:
        data = conn.recv(BUFFER_SIZE)
        if not data: break
        print "Received data: ", data
        conn.send(data)
conn.close()
import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 8080
BUFFER_SIZE = 2048

MESSAGE = raw_input()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
while True:
        s.send(MESSAGE)
        data = s.recv(BUFFER_SIZE)
        MESSAGE = raw_input()
s.close()

print "Data Received: ", data