A network/internet socket is an end-point of interposing communication across a computer network. In Python, the library has a module called socket which offers a low-level networking interface, it’s common across various programming languages since it uses OS-level system calls. When you create a socket, use a function called a socket. It accepts family, type, and prototype arguments. Creating a TCP-socket, for the family, you must use a socket.AF_INET or socket.AF_INET6 and for the type you must use socket.SOCK_STREAM.
Example for Python socket
Main methods to create Python socket objects
- bind() – specific for server sockets.
- listen() – specific for server sockets.
- accept() – specific for server sockets.
- connect() – client sockets.
- send() – both server and client sockets.
- recv() – both server and client sockets.
To create a server socket, bind it to localhost and 50000 port, and start listening for incoming connections. To accept an incoming connection call as accept(), this method will block until a new client connects. When it’s happening, will create a new socket and returns it together with the client’s address. Then, an infinite loop reads data from the socket in batches of 1 KB using method recv() till it returns a blank string. Next, it sends all incoming data back using a method called sendall() which inside repeatedly calls send(). Next, to that, it just closes the client’s connection.
A client-side code looks simply
Here instead of bind() and listen() methods it calls only the connect() method and directly sends data to the server. Then it receives 1 KB back, closes the socket then writes the received data. Each socket method is blocking. For instance, when it reads from a socket or prints to it the program can’t do anything. The possible solution is to delegate working with clients to separate process/threads, creating any process and swap contexts between them is not a cheap operation. To solve this issue, there is a so-called asynchronous way of working with sockets. The main idea is to maintain the socket’s state to an OS and check the program when there is something to read from the socket or when it is ready for writing.
Sataware Technologies is one of the leading Mobile App Development Company
We’re specialist in areas such as Custom Software Development, Mobile App Development, Ionic Application Development, Website Development, E-commerce Solutions, Cloud Computing, Business Analytics, and Business Process Outsourcing (Voice and non-voice process) We believe in just one thing – ON TIME QUALITY DELIVER
App development company
Software development company
Game development company
- Software Development
- Mobile App Development
- Web Development
- UI/UX Design and Development
- AR and VR App Development
- IoT Application Development
- Android App Development
- www.sataware.com/app-developers /
- Windows App Development
Contact us: www.sataware.com
1330 West, Broadway Road,
Tempe, AZ 85282, USA