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 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...
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,...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
I am trying to make a new code that uses functions to make it. My functions...
I am trying to make a new code that uses functions to make it. My functions are below the code. <?php */ $input; $TenBills = 1000; $FiveBills = 500; $OneBills = 100; $Quarters = 25; $Dimes = 10; $Nickels = 5; $Pennies = 1; $YourChange = 0; $input = readline("Hello, please enter your amount of cents:\n"); if(ctype_digit($input)) { $dollars =(int)($input/100); $cents = $input%100;    $input >= $TenBills; $YourChange = (int)($input/$TenBills); $input -= $TenBills * $YourChange; print "Change for $dollars dollars...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
I have this mystery code. I dont know what the code does. Can somone please explain...
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx) from typing import Dict, TextIO, Tuple, List def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) -> None: """ *Mystery code* """ for key in d1: value = d1[key] for i in range(len(value)): value[i] = d2[value[i]]   
Hello! I am learning abaqus currently, but I do not know what the Analytic field is!...
Hello! I am learning abaqus currently, but I do not know what the Analytic field is! Can you tell me what is it? And how to use it with some examples! Thank in advance! I will thumb you up!
C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT