Question

In: Computer Science

I am new to socket programming and I wish to know what these lines of code...

I am new to socket programming and I wish to know what these lines of code do. I know they are creating UDP sockets, but it would help if someone explained what the code means. Thank you.

Python Code:

import socket

testingSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
testingSocket.bind(('0.0.0.0', 50000))
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
send_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
receiving_thread = Thread(target=self.receivingFunction)
send_thread = Thread(target=self.sendMessage)
broadcast_online_status_thread = Thread(target=onlineStatus)

Solutions

Expert Solution

Explanation is given with # before every code line.

CODE:

# this module can give access to low-level socket functionalities.

# in this module some of the functions are platform dependant

import socket

# The AF_INET is in reference to th family or domain, it means ipv4, as opposed to ipv6 with AF_INET6. The SOCK_DGRAM means it will be a UDP socket. UDP means it will be connectionless.

testingSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# configuring the socket with the available options here, setsockopt function used to set the socket options. When retrieving a socket option, or setting it, you specify the option name as well as the level. When level = SOL_SOCKET, the item will be searched for in the socket itself. SO_REUSEADDR is set to 1 means that the validated addresses supplied to bind() should allow reuse of local addresses.

testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)

# SO_BROADCAST this option is used check the reports whether transmission of broadcast messages is set to 1 here means allowed.

testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)


# bind the socket with the provided < ip address and the port number >

testingSocket.bind(('0.0.0.0', 50000))

#creating another socket by name send socket

send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

#configures the send socket and checking the whether broadcast is set to True

send_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

# This constructor should always be called with keyword arguments. Arguments target is the callable object to be invoked by the run() method. Defaults is None means nothing is called.

#receiving function is called

receiving_thread = Thread(target=self.receivingFunction)

# send Message is called

send_thread = Thread(target=self.sendMessage)

#onlineStatus is called

broadcast_online_status_thread = Thread(target=onlineStatus)


Related Solutions

I am needing to initialize a server socket and wait for incoming connections in C programming....
I am needing to initialize a server socket and wait for incoming connections in C programming. I am also required to create a thread to handle my client and able to pass messages between them asynchronously. I would ideally like to start my server with pthreads to allow for multiple connections. I have 2 functions started: int serverRun(){ this will be the meat of the server code with sockets } int serverStart() { This is where I want to initiate...
New to C programming and I am stuck. The program will print "no input" if no...
New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly? #include <stdio.h> #include <stdlib.h> int main () { char input; scanf("%c", &input); if (input == NULL)...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT TO KNOW THE DETAILS! straight C Program. write a quiz game where a number of questions are asked and the user will win each time he answers right. and he loses each time he answers wrong. the type of questions asked can be about sports or anything else. You can put 5 quiz questions. It should only include the following: #include <stdio.h> and #include...
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
why we need socket programming? what its application.
why we need socket programming? what its application.
I am trying to write code for a program in Visual Studo using Visual Basic programming...
I am trying to write code for a program in Visual Studo using Visual Basic programming language that computes the factorial of an entered number by using a For Loop. My problem is that it keeps re-setting the variable for Factorial. Below is my code if anyone can help. I want it to multiply the given number by that number - 1, then continuing to do so until it hits zero without multiplying by zero. Private Sub BtnCalculate_Click(sender As Object,...
Python Programming I have a skeleton code here for this project but do not know exactly...
Python Programming I have a skeleton code here for this project but do not know exactly where to start. My task is to implement a reliable File Transfer Protocol (FTP) service over UDP. I need help to write the reliable FTP client and server programs based on an Alternating Bit Protocol (ABP) that will communicate with a specially designated server. In socket-level programming as well... # program description # Library imports from socket import * import sys import time #...
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
Assembly Question: I am trying to annotate this code and am struggling to understand what it...
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments? .data .star: .string "*" .line: .string "\n" .input: .string "%d" .count: .space 8 .text .global main printstars: push %rsi push %rdi _printstars: push %rdi mov $.star, %rdi xor %rax, %rax call printf pop %rdi dec %rdi cmp $0, %rdi jg _printstars mov $.line, %rdi xor %rax, %rax call printf pop %rdi pop %rsi ret printstarpyramid: mov %rdi,...
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT