top of page

Week 2: Networking.

Updated: Feb 28, 2021

Class 4

It was a Saturday, the year 2021, month of Feb. I woke up at 6:50 am and did my workout, it was my chest day. put on my cloths and left for my university. I reached by around 8:45 am thou the class starts around 10 am. I did a few things related to my enrolment and such; wandered aimlessly around the collage and got in at 10 am. Mr.Mustafa came to class 10 min later. We sat down set up visual studio and started coding trying to make a server and a client.


We used vanilla C# and the inbuilt socket library to get it running. first we made a sockets that listens to a port and waits for a connection. then we made a server that has a socket and tries to establish a connection with the server that we hosted the connection was established and the thing worked perfectly fine. then we tried to connect a friends server through my client and it failed. the teacher said that its because we were not able port forward the router to the certain server. And he said we will be doing it in the next class.


Class 5

A fine lukewarm morning, a Thursday. 2021. I woke up and started working for the place I am hired at the moment. Tried a quiet a bit to get v141 Microsoft Platform Tool to install on my visual studio. I worked till 10:45 am and left my place heading to uni. I reached at my college food-court. I had something to bite with my friend. once done we left for class. When we arrived Mr.Mustafa was already there and was setting up every thing for dong the network lecturer today.


He told us the agenda for today that is we will set up a router which has an internet connection which we get from a phone data. the my pc will be a server and listen for a packet at the port for a certain IP of the device. So first he gave me a static IP, then he port forwarded my IP address and port number which is 4432. So what happens here is the NAT is registered with the device IP and the listening port number so when we send a packet the NAT translates the packet's sender address and port number to public address of the router and a new port number that the router assigns to the device.


We asked Anikith to be the client and send a packet as client, it worked perfectly, then we came to realize that it was within the LAN network which can work with out port forwarding. So we asked Vishnu to connect as client and we gave him the public IP of the router and the port number. he tried to connect it a couple of times but it ain't happening. Mr.Mustafa asked Juma to be the host and did the same set up, there was no avail, he did the same for Aniket as well but still the same. so now we are paling off ways to debug.


Then Mustafa had a doubt weather the mobile phones operating system is causing this issue. so we did some research and found that when mobile data is taken from the mobile the phone acts as the router and we have to do port forwarding for the mobile, so we did, we tried couple of apps from asset store and none of them worked. So we finally decided to make Vishnu the server and make him receive the packets. Vishnu screen shared and Mr.Mustafa asked him to do the configurations, such as setting the static IP for his device port forwarding the router by registering at the NAT. then we tried connection to Vishnu's server and it finally worked. Then we began coding a simple system into the chat app were we can send one message from the client and send one message from the server. I was done in 10 min and we called it a day.


Class 6

the year 2021. It was a silent and lifeless morning, I rolled out of my bed, fell to the ground, laid down there for a couple of minutes contemplating the meaning of existence and life itself. I stood up, decided to go and make some tea, one of my favorite drinks; May god bless its maker. I came back and turned on my laptop, sat down and started planning the tasks for the day. I started doing the task for the company I work for, they have told me that there is an urgent task that needs to be done. its a customer service app, made using React-Native. As i was very busy I decided to attend the online class through the google meet link. I joined the link around 2 o'clock, the class started and the tutor started talking about ways we can improve the Chat App. He said the currently the sockets listens while blocking the whole process of the application. it happens due to the fact that the socket by default is blocking mode. so to make a socket un blocking we have to set the flag to false.

socket.Blocking = false;// sets the socket unblocking

then the other issue that comes to play is that the application will not be able to listen if once the socket listening is attempted. also if the attempt does not provide a result the application will throw an error an crash. to solve these issue what we do is that we place the whole listening attempt section inside a while loop so the action happens over and over indefinitely and to prevent the crashing we use try-catch to run the listen function. here is how it looks


while (true){
              try
              {
                  // new connection socket
             socketConnections.Add(listeningSocket.Accept());
                  Console.WriteLine("Socket Connected...");
              }
              catch (SocketException ex)
              {
                  if (ex.SocketErrorCode != SocketError.WouldBlock) Console.WriteLine(ex);
              }
}

Then in the server-side we have to also listen if any of the clients send the data if it does, we send it across to all the connections except the one who send it. we apply try-catch for receiving the data from the clients as no output from the attempt would result in a crash. so here is how it looks.


for (int i = 0; i < socketConnections.Count; i++)
              {
                  try
                  {
                      int recivedDataSize = socketConnections[i].Receive(reciveBuffer); // try to recive data from the client
                      Console.WriteLine($"Message Recived from client No. {i} and recived data size is {recivedDataSize}");
                      for (int j = 0; j < socketConnections.Count; j++)
                      {
                          if (i != j) socketConnections[j].Send(reciveBuffer, recivedDataSize, SocketFlags.None); // try to send the data to other client
                      }
                  }
                 catch (SocketException ex)
                   {
                       if (ex.SocketErrorCode == SocketError.ConnectionAborted ||
                           ex.SocketErrorCode == SocketError.ConnectionReset)
                       {
                           socketConnections[i].Close();
                           socketConnections.RemoveAt(i);
                       }
 
                       if (ex.SocketErrorCode != SocketError.WouldBlock)
                       {
                           if (ex.SocketErrorCode != SocketError.ConnectionAborted ||
                           ex.SocketErrorCode != SocketError.ConnectionReset) Console.WriteLine(ex);
 
                       }
                   }
}

the catching seams very crucial in this scenario as we need to know some exception error message while not the obvious one the once we know are happening due to the socket listening and failing to get an out put.

   if (ex.SocketErrorCode != SocketError.WouldBlock)
                       {
                           if (ex.SocketErrorCode != SocketError.ConnectionAborted ||
                           ex.SocketErrorCode != SocketError.ConnectionReset) Console.WriteLine(ex);
 
                       }

As I mentioned the important exceptions are ConnectionAborted and ConnectionReset, which we could use to find out weather a client socket lost connection or not. if it does we can eject it garcefully.

  if (ex.SocketErrorCode == SocketError.ConnectionAborted ||
                           ex.SocketErrorCode == SocketError.ConnectionReset)
                       {
                           socketConnections[i].Close();
                           socketConnections.RemoveAt(i);
                       }

when it comes to client-side we can start connecting to a server machine instantly but right after that we set the socket to unblocked socket so that we could iteratively manage the sending and reception of the data within a while loop.

  while(true){
  
  // read input section
  
  if(anyKeyPressed){
  if(anyKeyPressed == Key.Enter){
      socket.Send(ASCII.encode(msgString))
  }
      else{
          msgString += anyKeyPressed
      }
  }
  
  // get and display output section
  
  bytes[] reciveBytes;
  
  int dataSize = socket.Recive(reciveBytes)
  print(reciveBytes.toString())
  }

I struggled a bit at first to understand and implement these, once did it looks really simple and straight forward. To note this one key thing learned is that the fact that the size of the data stream can be send from the client and server can send it to the clients while it broadcasts the data.


socket.Send(sendData, sendData.Length, SocketFlags.None);




Recent Posts

See All
Week 13 : El Fin...

Class 39 : We join in the class on Sunday. There's is nothing much to teach from our tutors side. all of us guys are doing our curricular...

 
 
 
Week 12 : Ending it rightly...

Class 36 : It was a Sunday. a busy morning. I showed up for the class, the tutor asked us let him know if we need any support with the...

 
 
 

Comments


  • LinkedIn
  • YouTube

© 2017 by Alwin Joshy. Proudly created with Wix.com.

bottom of page