Example: quiz answers

TE3004 Embedded Systems Design Lab 09 – Socket …

TE3004 Embedded Systems Design Lab 09 Socket programming on the RPi introduction Typically two processes communicate with each other on a single system through one of the following inter-process communication techniques. Pipes Message queues Shared memory There are several other methods. But the above are some of the very classic ways of inter-process communication. The Berkeley version of UNIX introduced a new communication tool, the Socket interface, which is an extension of the concept of a pipe. Socket interfaces are available on Linux. You can use sockets in much the same way as pipes, but they include communication across a network of computers. A process on one machine can use sockets to communicate with a process on another, which allows for client/server Systems that are distributed across a network. sockets may also be used between processes on the same machine.

TE3004 Embedded Systems Design Lab 09 – Socket Programming on the RPi INTRODUCTION Typically two processes communicate with each other on …

Tags:

  Introduction, Programming, System, Design, Sockets, Embedded, Te3004 embedded systems design lab, Te3004, Socket programming on the rpi introduction

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of TE3004 Embedded Systems Design Lab 09 – Socket …

1 TE3004 Embedded Systems Design Lab 09 Socket programming on the RPi introduction Typically two processes communicate with each other on a single system through one of the following inter-process communication techniques. Pipes Message queues Shared memory There are several other methods. But the above are some of the very classic ways of inter-process communication. The Berkeley version of UNIX introduced a new communication tool, the Socket interface, which is an extension of the concept of a pipe. Socket interfaces are available on Linux. You can use sockets in much the same way as pipes, but they include communication across a network of computers. A process on one machine can use sockets to communicate with a process on another, which allows for client/server Systems that are distributed across a network. sockets may also be used between processes on the same machine.

2 Linux functions such as printing, connecting to databases, and serving web pages as well as network utilities such as rlogin for remote login and ftp for file transfer usually use sockets to communicate. sockets may be implemented on Python over a number of different channel types: Unix domain sockets , TCP, UDP, and so on. The Socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. A Simple Server To write Internet servers, the Socket function available in Socket module is used to create a Socket object. A Socket object is then used to call other functions to setup a Socket server. s = (socket_family, socket_type, protocol=0) Then bind(hostname, port) function is called to specify a port for your service on the given host. Next, the accept method is called of the returned object.

3 This method waits until a client connects to the specified port, and then returns a connection object that represents the connection to that client. #!/usr/bin/python # This is file import Socket # Import Socket module s = () # Create a Socket object host = () # Get local machine name port = 12345 # Reserve a port for your service. ((host, port)) # Bind to the port (5) # Now wait for client connection. while True: c, addr = () # Establish connection with client. print 'Got connection from', addr ('Thank you for connecting') () # Close the connection A Simple Client Now we will write a very simple client program which will open a connection to a given port 12345 and given host. This is very simple to create a Socket client using Python's Socket module function.

4 The ( hosname, port ) opens a TCP connection to hostname on the port. Once you have a Socket open, you can read from it like any IO object. When done, remember to close it, as you would close a file. The following code is a very simple client that connects to a given host and port, reads any available data from the Socket , and then exits: #!/usr/bin/python # This is file import Socket # Import Socket module s = () # Create a Socket object host = () # Get local machine name port = 12345 # Reserve a port for your service. ((host, port)) print (1024) # Close the Socket when done Now run this in background and then run above to see the result. # Following would start a server in background. $ python & # Once server is started run client as follows: $ python This would produce following result: Got connection from (' ', 48437) Thank you for connecting ASSIGNMENTS Assignment 1 Write two python programs: one that implements a simple local client and another that implements a simple local server on the RPi.

5 The server program can serve only one client at a time. It just reads a number from the client, increments it, and writes it back. In more sophisticated Systems , where the server has to perform more work on the client s behalf, this would not be acceptable, because other clients would be unable to connect until the server had finished. Assignment 2 For this assignment you will need to work collaboratively with another team. One team will implement a client on their own RPi, and the other team will implement a server on their own RPi. On the client side, when a switch is pressed/depressed (connected to GPIO 24) a client/server interaction starts, a character is sent, then the client disconnects itself. The server reads a character corresponding to the switch action and then turns on/off a led (connected to GPIO 25) accordingly. Please use the schematic diagram provided.

6 Assignment 3 For this assignment you will need also to work collaboratively with another team. On side A, when a switch is pressed/depressed a client/server interaction starts and a character is sent. On side B, a server reads a character from the client and then turns on/off a led accordingly. At the same time, on side A, a server will be waiting for a character. According to the character a led will be turned on/off. On side B a client will send a character if a switch is pressed/depressed. That means that on both sides, two processes will be running simultaneously, one client and one server. Please use the schematic diagram provided. To complete this lab you will: 1. Demonstrate the correct operation of the assignments solutions to your lab instructor. 2. Elaborate a report including the code and the answer to the following questions: List 2 possible valid applications that you can think of for sockets other than the ones that have been mentioned here.

7 For the assignments, did you choose UDP or TCP sockets ? Why? What are the differences between them? Did you choose the Socket module or the socketsever module? Why? What are the differences between them? Include snapshots and personal reflections. Note: Report Due date: April 30, 2013 (4:00 maximum). Send it via email. Resources Network programming using Python. Cuauhtemoc Carbajal, Abril 6, 2013. Socket Low-level networking interface SocketServer A framework for network servers SCHEMATIC DIAGRAM (built using Fritzing)


Related search queries