EzDevInfo.com

network-programming interview questions

Top network-programming frequently asked interview questions

What is the Difference Between read() and recv() , and Between send() and write()?

what is the difference between read() and recv() , and between send() and write() in socket programming ? performance and speed and other behavior.


Source: (StackOverflow)

Creating a web server in pure C [closed]

Im doing a little project on my university which now involves in creating a webserver only using C. I know a little about HTTP 1.1 and I've created a webserver in C# before.

However, I'd like to see how you would either begin with socket programming and using multi threads in C, or how you would otherwise create a webserver in C.

There's not really a lot of requirements, I'd just like to get started with a little socket, listening to port 80, sending out some respons text to the webserver / telnet-client. And maybe some code-structure advice would be of help too. The biggest requirement is probably that its for Linux.


Source: (StackOverflow)

Advertisements

java.net.URLEncoder.encode(String) is deprecated, what should I use instead?

I get the following warning when using java.net.URLEncoder.encode:

warning: [deprecation] encode(java.lang.String)
         in java.net.URLEncoder has been deprecated

What should I be using instead?


Source: (StackOverflow)

How is TeamViewer so fast?

Sorry about the length, it's kinda necessary.

Introduction

I'm developing a remote desktop software (just for fun) in C# 4.0 for Windows Vista/7. I've gotten through basic obstacles: I have a robust UDP messaging system, relatively clean program design, I've got a mirror driver (the free DFMirage mirror driver from DemoForge) up and running, and I've implemented NAT traversal for all NAT types except Symmetric NATs (present in corporate firewall situations).

Regarding screen transfer/sharing, thanks to the mirror driver, I'm automatically notified of changed screen regions and I can simply marshal the mirror driver's ever-changing screen bitmap to my own bitmap. Then I compress the screen region as a PNG and send it off from the server to my client. Things are looking pretty good, but it's not fast enough. It's just as slow as VNC (btw, I don't use the VNC protocol, just a custom amateur protocol).

From the slowest remote desktop software to the fastest, the list usually begins at all VNC-like implementations, then climbs up to Microsoft Windows Remote Desktop...and then...TeamViewer. Not quite sure about CrossLoop, LogMeIn - I haven't used them, but TeamViewer is insanely fast. It's quite literally live. I ran a tree command on Command Prompt and it updated with 20 ms delay. I can browse the web just a few milliseconds slower than on my laptop. Scrolling code vertically in Visual Studio has 50 ms lag time. Think about how robust TeamViewer's screen-transfer solution must be to accomplish all this.

VNCs use poll-based hooks for detecting screen change and brute force screen capturing/comparing at their worst. At their best, they use a mirror driver like DFMirage. I'm at this level. And they use something called the RFB protocol.

Microsoft Windows Remote Desktop apparently goes one step higher than VNC. I heard, from somewhere on StackOverflow, that Windows Remote Desktop doesn't send screen bitmaps, but actual drawing commands. That's quite brilliant, because it can just send simple text (draw this rectangle at this coordinate and color it with this gradient)! Remote Desktop really is pretty fast - and it's the standard way of working from home. And it uses something called the RDP protocol.

Now TeamViewer is a complete mystery to me. Apparently, they released their source code for Version 2 (TeamViewer is Version 7 as of February 2012). People have read it and said that Version 2 is useless - that it's just a few improvements over VNC with automatic NAT traversal.

But Version 7...it's ridiculously fast now. I mean, it's actually faster than Windows Remote Desktop. I've streamed DirectX 3D games with TeamViewer (at 1 fps, but Windows Remote Desktop doesn't even allow DirectX to run).

By the way, TeamViewer does all this without a mirror driver. There is an option to install one, and it gets just a bit faster.

The Question

My question is, how is TeamViewer so fast? It must not be possible. If you've got 1920 by 1080 resolution at even 24 bit depth (16 bit depth would be noticeably ugly), thats still 6,220,800 bytes raw. Even using libjpeg-turbo (one of the fastest JPG compression libraries used by large corporations), compressing it down to 30KB (let's be extremely generous), would take time to route through TeamViewer's servers (TeamViewer bypasses corporate Symmetric NATs by simply proxying traffic through their servers). And that libjpeg-turbo compression would take time to compress. High-quality JPG compression takes 175 milliseconds for a full 1920 by 1080 screenshot for me. And that number goes up if the host's computer runs an Atom processor. I simply don't understand how TeamViewer has optimized their screen transfer so well. Again, small-size images might be highly compressed, but take at least tens of milliseconds to compress. Large-size images take no time to compress, but take a long time to get through. Somehow, TeamViewer completes this entire process to get roughly 20-25 frames per second. I've used a network monitor, and TeamViewer is still lagless at speeds of 500 Kbps and 1 Mbps (VNC software lag for a few seconds at that transfer rate). During my tree Command Prompt test, TeamViewer was receiving inbound data at a rate of 1 Mbps and still running 5-6 fps. VNC and remote desktop don't do that. So, how?

The answers will be somewhat complicated and intricate, so please don't post your $0.02 if you're only going to say it's because they use UDP instead of TCP (would you believe they actually do use TCP just as successfully though).

I'm hoping there's a TeamViewer developer somewhere here on StackOverflow.

Potential Answers

Will update this once people reply.

  1. My thoughts are, first of all, that TeamViewer has very fine network control. For example, they split large packets to just under the MTU size and never waste a trip. They probably have all sorts of fancy hooks to detect screen changes along with extremely fast XOR image comparisons.

Source: (StackOverflow)

How to write a simple Bittorrent application?

How to write a simple bittorrent application. Something like a "hello world" using a bittorrent library, I mean a simplest of the application to understand the working of bittorrent. I would prefer a python or a C/C++ implementation, but it can be any language. Platform is not an issues either, but i would prefer Linux.

Recommendations for the library to follow, I have downloaded the source code for one (i think official bittorrent) from - http://sourceforge.net/projects/bittorrent/develop. But, I see a lot of other libraries at http://en.wikipedia.org/wiki/Comparison_of_BitTorrent_clients#Libraries. I would appreciate recommendations on this.

How to test an application if all you have is one laptop.


Source: (StackOverflow)

Using SO_REUSEADDR - What happens to previously open socket?

In network programming in unix, I have always set the SO_REUSEADDR option on the socket being used by server to listen to connections on. This basically says that another socket can be opened on the same port on the machine. This is useful when recovering from a crash and the socket was not properly closed - the app can be restarted and it will simply open another socket on the same port and continue listening.

My question is, what happens to the old socket? Without a doubt, all data/connections will still be received on the old socket. Does it get closed automatically by the OS?


Source: (StackOverflow)

how to make Multiplayer Game [closed]

I already knew the basic of android programming, especially the one at game. now I want to try multiplayer game. but I dont have any background about multiplayer game. so I want have a number of question :

  1. What is the basic of multiplayer programming?
  2. how is the communication method for multiplayer? should it bluetooth? or maybe client-server ? or maybe peer to peer? if the game that I want to make required 2 android handset near each other?
  3. what should I know in order to understand this multiplayer?
  4. is there any basic tutorial about how to make multiplayer game?
  5. can an android handset acted as a Server?

I don't plan to make any sophisticated game yet. may be just multiplayer tic tac toe is alright for me. the most important is that I understand the basic of multiplayer game programming. :)


Source: (StackOverflow)

How many socket connections can a web server handle?

Say if I was to get shared, virtual or dedicated hosting, I read somewhere a server/machine can only handle 64,000 TCP connections at one time, is this true? How many could any type of hosting handle regardless of bandwidth? I'm assuming HTTP works over TCP.

Would this mean only 64,000 users could connect to the website, and if I wanted to serve more I'd have to move to a web farm?


Source: (StackOverflow)

Ruby - See if a port is open

I need a quick way to find out if a given port is open with Ruby. I currently am fiddling around with this:

require 'socket'

def is_port_open?(ip, port)
  begin
    TCPSocket.new(ip, port)
  rescue Errno::ECONNREFUSED
    return false
  end
  return true
end

It works great if the port is open, but the downside of this is that occasionally it will just sit and wait for 10-20 seconds and then eventually time out, throwing a ETIMEOUT exception (if the port is closed). My question is thus:

Can this code be amended to only wait for a second (and return false if we get nothing back by then) or is there a better way to check if a given port is open on a given host?

Edit: Calling bash code is acceptable also as long as it works cross-platform (e.g., Mac OS X, *nix, and Cygwin), although I do prefer Ruby code.


Source: (StackOverflow)

What is the optimal size of a UDP packet for maximum throughput?

I need to send packets from one host to another over a potentially lossy network. In order to minimize packet latency, I'm not considering TCP/IP. But, I wish to maximize the throughput uisng UDP. What should be the optimal size of UDP packet to use?

Here are some of my considerations:

  • The MTU size of the switches in the network is 1500. If I use a large packet, for example 8192, this will cause fragmentation. Loss of one fragment will result in the loss of the entire packet, right?

  • If I use smaller packets, I'll incur the overhead of the UDP and IP header

  • If I use a really large packet, what is the largest that I can use? I read that the largest datagram size is 65507. What is the buffer size I should use to allow me to send such sizes? Would that help to bump up my throughput?

  • What are the typical maximum datagram size supported by the common OSes (eg. Windows, Linux, etc.)?

Updated:

Some of the receivers of the data are embedded systems for which TCP/IP stack is not implemented.

I know that this place is filled with people who are very adament about using what's available. But I hope to have better answers than just focusing on MTU alone.


Source: (StackOverflow)

Best architecture for an iOS application that makes many network requests?

I'm in the process of rethinking my approach to the request architecture of a large app I'm developing. I'm currently using ASIHTTPRequest to actually make requests, but since I need many different types of requests as a result of many different actions taken in different view controllers, I'm trying to work out the best system of organizing these requests.

I'm currently building singleton "requesters" that are retained by the app delegate and sit around listening for NSNotifications that signal a request needs to be made; they make the request, listen for the response, and send out a new NSNotification with the response data. This solves most of my problems, but doesn't elegantly handle failed requests or simultaneous requests to the same singleton requester.

Anyone have any success devising a clear, OO architecture for making many different types of requests in an iOS app?


Source: (StackOverflow)

What's the simplest way to call Http GET url using Delphi?

There's a web services I want to call in my application, I can use it with importing the WSDL or by just use "HTTP GET" with the URL and parameters, so I prefer the later because it's simple thing.

I know I can use indy idhttp.get, to do the job, but this is very simple thing and I don't want to add complex indy code to my application.

UPDATE: sorry if I was not clear, I meant by "not to add complex indy code", that I don't want add indy components for just this simple task, and prefer more lighter way for that.


Source: (StackOverflow)

TCPClient vs Socket in C#

I don't see much use of TCPClient, yet there is a lot of Socket? What is the major difference between them and when would you use each?

I understand that .NET Socket is written on top of WINSOCK, and TCPClient is a wrapper over Socket class. Thus TCPClient is way up the chain, and possibly inefficient. Correct me if I am wrong.

Thanks


Source: (StackOverflow)

example.com is OK for domain example, what is example IP Address?

We use the example.com often for domain name reference. I wonder if any IP address used as an example IP address to mention in references like tutorial or notes?. I knew the loop back interface 127.0.0.1 can be used but it has been known for local system always.

Can example.com IP Address used for this purpose legally?


Source: (StackOverflow)

Does epoll(), do its job in O(1)?

Wikipedia says

unlike the older system calls, which operate at O(n), epoll operates in O(1) [2]).

http://en.wikipedia.org/wiki/Epoll

However, the source code at fs/eventpoll.c on Linux-2.6.38, seems it is implemented with an RB tree for searching, which has O(logN)

/*
 * Search the file inside the eventpoll tree. The RB tree operations
 * are protected by the "mtx" mutex, and ep_find() must be called with
 * "mtx" held.
 */
static struct epitem *ep_find(struct eventpoll *ep, struct file *file, int fd)
{

In fact, I couldn't see any man page saying the complexity of epoll() is O(1). Why is it known as O(1)?


Source: (StackOverflow)