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...
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,...
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[])    {      ...
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 having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT