Video Chat app using Socket Programming.

Devanshu Singh
3 min readJun 10, 2021

Today, We are using socket programming & OpenCV to extract the video live stream from the client-side to the server-side. The server & client codes can either be run on the same computer or different computers connected to the same WiFi.

What is a Network Socket?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect.

A socket has a typical flow of events. In a connection-oriented client-to-server model, the socket on the server process waits for requests from a client. To do this, the server first establishes (binds) an address that clients can use to find the server. When the address is established, the server waits for clients to request a service. The client-to-server data exchange takes place when a client connects to the server through a socket. The server performs the client’s request and sends the reply back to the client.

Socket Address:

An application can communicate with a remote process by exchanging data with TCP/IP by knowing the combination of protocol type, IP address, and port number. This combination is often known as a socket address. It is the network-facing access handle to the network socket. The remote process establishes a network socket in its own instance of the protocol stack and uses the networking API to connect to the application, presenting its own socket address for use by the application.

In standard IP like TCP & UDP:

socket address = (IP address, port number)

Sevral types of Internet sockets:

  1. Datagram Sockets:
  • Connectionless socket User Datagram Protocol (UDP).
  • Each packet sent or received is individually addressed.
  • Order and reliability are not guranteed.

2. Stream Sockets:

  • Connection-oriented sockets Transmission control Protocol (TCP).
  • Packets are sequenced with a unique flow of error-free data.
  • Order and reliability are guaranteed.

3. Raw Sockets:

  • Allow direct sending and receiving of IP packets.
  • Without any protocol-specific transport layer formatting
  • When transmitting packets automatic addition of a header is optional
  • Mostly used in security-related applications
  • Raw sockets are typically available in network equipments.

Click to know more about the NETWORK SOCKET.

Client-Server model:

  • Server create sockets on startup.
  • May serve several clients concurrently.
  • A client should know the server IP & port.
Client-Server model
Client-Server Demo

Video data transmission:

At server side

  • With OpenCV get video frames of webcam.
  • With pickle serialize frame to byte data
  • Pack each frame data using struct module
  • Send data to client and display frame

At client side:

  • Recive packets and append them to data
  • Unpack the data using struct module
  • Load the frame using pickle
  • Display the frame at Client side

Let's get started with the codes.

Below is the server side code which will be run on the server only.

Below is the Client side code which will be run on the Client only.

Now after this run both the Files. You can run both files in same computer or two different computers connected to the same WiFi Then only this will work.

Running Server.py
Running Client.py
Video Live stream Between Server & Client.

so its done we have created the Video stream between the Client & server using the Socket Programming.

Thank you!!

GitHub repo Link: https://github.com/devanshu06/SocketProgramming_VideoStream

--

--