In this tutorial I will explain some of the basics of python socket programing.
Let’s run the server program on beaglebone black and client program on local machine.
First connect the beaglebone black to the local machine using the USB cable provided.
Here, Ubuntu 14.04 is the OS of my local machine and to connect to the beaglebone black I used the REMINA remote desktop client. But REMINA has nothing to do with our test of TCP client – server programs.
As prerequisites you need to install python on both machines.
First lets connect to the remote desktop of the beaglebone black. Open a python editor. As usual I use IDLE. In a new file type the server code or use the file I have given.
First we import the socket module
import socket
Lets put the function of the server in to a python function. The beaglebone black’s default IP address is ‘192.168.7.2’ and we use a socket number which is somewhat easier to remember ‘6666’.
def Main():
host = ‘192.168.7.2’
port = 6666
Lets use socket.socket() function to comes with socket module to create a socket. Then we use bind method to bind the socket with host and port. Remember to use double parentheses.
TestServer = socket.socket()
TestServer.bind((host,port))
To start up a listner (and to set up) we use listen method.
TestServer.listen(1)
Then the server will waits for client’s connection and accept it with accept method.
c, addr = TestServer.accept()
Lets print whose connected.
print “Connection from: ” + str(addr)
Then we will loop through the interaction. Within the loop we use recv() method to receive data from the socket. Please note that the return value is a string, representing the data received. The maximum amount of data to be received at once is specified by buffer size. We use a 1024 buffer size.
while True:
data = int(c.recv(1024))
The int () function is used to convert the received data into an integer value for we use it for a mathematical calculation. To reduce the errors we use an if function to break the loop if nothing received.
if not data:
break
print “from connected user: ” + str(data)
We just add 34 to the number sent by the client. And send it back to the client using send() method.
data = data + 34
print “sending: ” + str(data)
c.send(str(data))
Finally we do close the connection.
c.close()
This will sets the def Main() as the main program.
if __name__== ‘__main__’:
Main()
Save the file as server.py
Thats all for the server program. Now open a terminal (I mean a real terminal Alt + Ctrl + T, Not IDLE shell). Then go to your python file location and run following command.
python server.py
Then open an IDLE new file in local machine. Type the client code or directly use the file I have given.
First line is to import necessary library.
The default IP address of beaglebone black is 192.168.7.2. So it will be our server IP address. We connect to via port number ‘6666’.
serverAddr = (‘192.168.7.2′, 6666)
Then we define the socket types we need.
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Next, let’s connect to the server using connect() mothod.
client.connect(serverAddr)
We define a new variable messg as a string input. This will take our keyboard input. If you are interested, you can try raw_input Vs input and see what are the issues.
messg = raw_input(“Enter your Number: “)
Using a while loop, we send our message to the server and wait for server response. And it is repeated.
while messg !=’q’:
client.send(messg)
data = client.recv(4096)
print ‘Receive from server: ‘ + str(data)
messg = raw_input(“Enter your Number: “)
messg !=’q’ means, you are going to break the loop by typing “q”. (without quotes)
For receiving message or the server response you have to give a buffer size. So here we used client.recv(4096).
After printing what we received, we ask the client to insert a new value raw_input(“Enter your Number: “). The loop will continue until the client’s response is “q”.
If you break the loop by typing “q”, the client connection will be closed.
client.close()
Save the file as client.py
Now open a terminal (Alt+Ctrl+t). Then go to your python file location and run following command.
python client.py
Following prompt will appear. Type a value and you can see the server response.
Download the python files Here.